Monday, 31 July 2017

Install Intel TinyB Java bluetooth library on Raspberry PI

The TinyB project is a bluetooth LE project for the Intel Edison. It is a project that communicates to bluez through DBUS. This library save you time writing your own JNI classes to interface with DBUS.
On the Intel Edison OS you are by default the root user so there is some change for raspberry pi and additional packages you need.

First upgrade your raspberry PI to the latest raspbian software which is Debian 9 Stretch at the moment. This will give you the latest Bluetooth software.

Install this software before building TinyB
sudo apt-get install openjdk-8-jdk  libglib2.0-dev cmake bluez

Export the variable on the command line or add it to your ~/.bashrc and source it afterwards :  source  ~/.bashrc
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-armhf/ 

Read the guide from the github page.
https://github.com/intel-iot-devkit/tinyb

You will need to tweak it for the raspbian OS. Clone the project then change in to the project directory.
mkdir build

cd build

sudo -E cmake -DBUILDJAVA=ON -DCMAKE_INSTALL_PREFIX=/usr ..

You need the -E parameter in there so sudo will use the JAVA_HOME you defined. If you do not use it then it the build will not be able to find the JNI headers because it will not have a JAVA_HOME to check.  JNI is in JAVA_HOME under

This is the error you will receive when not using -E in he sudo command to use your JAVA_HOME.
CMake Error at /usr/share/cmake-3.7/Modules/FindPackageHandleStandardArgs.cmake:138 (message):
  Could NOT find JNI (missing: JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY
  JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH)
Call Stack (most recent call first):
  /usr/share/cmake-3.7/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-3.7/Modules/FindJNI.cmake:305 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
  java/jni/CMakeLists.txt:1 (find_package)

-- Configuring incomplete, errors occurred!
See also "/home/pi/tinyb/build/CMakeFiles/CMakeOutput.log".
See also "/home/pi/tinyb/build/CMakeFiles/CMakeError.log". 


It should complete without errors.

The make command will take about 10 minutes to complete on the raspberry PI.
sudo make

Install the libraries on to the file system
sudo make install
It will install the jar file under
Generating JNI headers..
[  6%] Built target tinybjar
[ 32%] Built target tinyb
[ 54%] Built target javatinyb
[ 60%] Built target HelloTinyB
[ 66%] Built target AsyncTinyB
[ 72%] Built target Notification
[ 76%] Built target esstinyb
[ 80%] Built target asynctinyb
[ 84%] Built target checkinit
[ 88%] Built target hellotinyb
[ 92%] Built target notifications
[ 96%] Built target list_mfg
[100%] Built target uuid
Install the project...
-- Install configuration: ""
-- Installing: /usr/lib/arm-linux-gnueabihf/../lib/java/tinyb.jar
-- Installing: /usr/lib/arm-linux-gnueabihf/libjavatinyb.so.0.5.0-11-g992d9c3.0.5.0-11-g992d9c3.0.5.0-11-g992d9c3
-- Installing: /usr/lib/arm-linux-gnueabihf/libjavatinyb.so.0.5.0-11-g992d9c3
-- Installing: /usr/lib/arm-linux-gnueabihf/libjavatinyb.so
-- Set runtime path of "/usr/lib/arm-linux-gnueabihf/libjavatinyb.so.0.5.0-11-g992d9c3.0.5.0-11-g992d9c3.0.5.0-11-g992d9c3" to ""
-- Installing: /usr/include/tinyb.hpp
-- Installing: /usr/include/tinyb
-- Installing: /usr/include/tinyb/BluetoothEvent.hpp
-- Installing: /usr/include/tinyb/BluetoothUUID.hpp
-- Installing: /usr/include/tinyb/BluetoothException.hpp
-- Installing: /usr/include/tinyb/BluetoothAdapter.hpp
-- Installing: /usr/include/tinyb/BluetoothGattService.hpp
-- Installing: /usr/include/tinyb/BluetoothManager.hpp
-- Installing: /usr/include/tinyb/BluetoothGattCharacteristic.hpp
-- Installing: /usr/include/tinyb/BluetoothGattDescriptor.hpp
-- Installing: /usr/include/tinyb/BluetoothObject.hpp
-- Installing: /usr/include/tinyb/BluetoothDevice.hpp
-- Installing: /usr/lib/pkgconfig/tinyb.pc
-- Installing: /usr/lib/arm-linux-gnueabihf/libtinyb.so.0.5.0-11-g992d9c3.0.5.0-11-g992d9c3.0.5.0-11-g992d9c3
-- Installing: /usr/lib/arm-linux-gnueabihf/libtinyb.so.0.5.0-11-g992d9c3
-- Installing: /usr/lib/arm-linux-gnueabihf/libtinyb.so

Execute the example code to test your installation. It should prompt you include a device address.
sudo java -cp examples/java/HelloTinyB.jar:/usr/lib/lib/java/tinyb.jar HelloTinyB
Run with <device_address> argument



As an extra piece. If you want to install them locally instead of globally like it is done aboive and you get either of these errors. DO NOT DO THIS IF YOU DONE THE SETUP ABOVE.
java.lang.UnsatisfiedLinkError: no javatinyb in java.library.path
java.lang.UnsatisfiedLinkError: no tinyb in java.library.path

It is looking for these 2 files
libjavatinyb.so 
libtinyb.so
locate what directory they are in then add them in the parameter
In my build directory i ran it like this. The 2 files are located in 2 different directories. This got it working for me.
java -Djava.library.path=/usr/local/lib/:./java/jni/ -cp examples/java/HelloTinyB.jar:./java/tinyb.jar HelloTinyB