B4J Tutorial [IoT][RPi] - Raspberry Pi Bluetooth Tutorial

Using the new jBluetooth library it is possible to communicate with Raspberry Pi devices over Bluetooth.

Bluetooth can be very useful in places where there is no local network.

The configuration is not so simple but it works and it seems to be stable.
I've used a Broadcamm dongle.

Lets start with a video that shows an Android device connects to the RPi with PiFace. The Android device controls the leds and monitors the buttons state.
Note that the PiFace extension is only used for demonstration purposes.
(Make sure to set the video setting to HD.)


Configuration

The first step is to install the drivers required.
1. Follow these instructions: https://www.raspberrypi.org/learning/robo-butler/bluetooth-setup/
At this point the Bluetooth should work when you test it with: hcitool scan
2. Run this command:
B4X:
sudo apt-get install libbluetooth-dev
3. Download bluecove_rpi_jars.zip that is attached to this post and copy the jars to the additional libraries folder.
Note that bluecove-gpl is licensed under the GPL license. The other jars are licensed under the Apache 2 license.
The jars are based on this build: http://lukealderton.co.uk/blog/posts/2015/january/raspberry-pi-bluetooth-using-bluecove-on-raspbian/

4. Run the attached example and monitor the logs. You can enable debug logs with this line:
B4X:
SetSystemProperty("bluecove.debug", "true")

Assuming that everything works properly then you should see the MAC address in the logs.
You can use this address to connect from the Android device
 

Attachments

  • bluecove_rpi_jars.zip
    449.8 KB · Views: 1,506
  • RPi_Bluetooth_B4J.zip
    1.4 KB · Views: 1,349
  • RPi_Bluetooth_B4A.zip
    9.7 KB · Views: 1,179

Peter Simpson

Expert
Licensed User
Longtime User
Thank you @Erel, this is excellent...
 

ilan

Expert
Licensed User
Longtime User
sorry for my ignorance but why is it in B4J forum? you are using a b4a app to communicate with the Raspberry Pi device?!

and i have another question, what model are you using in this video and for what can this device be used? can i connect to it different external devices and
turn them on/off?
 

inakigarm

Well-Known Member
Licensed User
Longtime User
There's an B4J aplication running on RPi to listen/send commands over the bluetooth connection for setting on/off the leds on RPi and send the button state to the phone app (remember you can run B4J on linux systems too - the RPi OS-)

The video displays the RPi with a PIFace Shield (with leds, buttons, relays and other interfaces); and the answer is YES, you can connect to different external devices ;)

Search the forum for more info (begin with https://www.b4x.com/android/forum/threads/b4j-and-raspberry-pi-boards.37019/#content )
 

inakigarm

Well-Known Member
Licensed User
Longtime User
Well, I don't have (yet) a RPi ; at the moment, I'm involved on Arduino for now

You could fint it on ebay, amazon, etc... (for ex: look at @KMatle post on https://www.b4x.com/android/forum/threads/raspberry-kit.60126/#post-379084)

With RPi (and Arduino,etc..) you can interface to digital or analog devices; these devices lets you design (DIY) Domotic control, etc... First think about a need you want to solve and then, the correct device to do it

(you can look for a example projects on https://www.raspberrypi.org/magpi/, https://www.adafruit.com/, http://www.instructables.com/,....)
 

SimonElck

Member
Licensed User
Trying this with the RPi3 which of course doesnot need a dongle as it has bluetooth on board.
I think I installed everything as described, but I get this error:

bluetootherror.PNG

Any ideas please?
 

fishwolf

Well-Known Member
Licensed User
Longtime User
i have the same error with raspi2 + dogle

B4X:
Waiting for debugger to connect...
Program started.
BlueCove version 2.1.1-SNAPSHOT on bluez
My address: 00:11:22:98:76:54
javax.bluetooth.ServiceRegistrationException: Can not open SDP session. [2] No such file or directory
    at com.intel.bluetooth.BluetoothStackBlueZ.openSDPSessionImpl(Native Method)
    at com.intel.bluetooth.BluetoothStackBlueZ.getSDPSession(BluetoothStackBlueZ.java:518)
    at com.intel.bluetooth.BluetoothStackBlueZ.registerSDPRecord(BluetoothStackBlueZ.java:543)
    at com.intel.bluetooth.BluetoothStackBlueZ.rfServerOpen(BluetoothStackBlueZ.java:607)
    at com.intel.bluetooth.BluetoothRFCommConnectionNotifier.<init>(BluetoothRFCommConnectionNotifier.java:42)
    at com.intel.bluetooth.MicroeditionConnector.openImpl(MicroeditionConnector.java:389)
    at com.intel.bluetooth.MicroeditionConnector.open(MicroeditionConnector.java:156)
    at javax.microedition.io.Connector.open(Connector.java:83)
    at anywheresoftware.b4j.objects.Bluetooth$3.run(Bluetooth.java:192)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Error connecting...
 

walterf25

Expert
Licensed User
Longtime User
Using the new jBluetooth library it is possible to communicate with Raspberry Pi devices over Bluetooth.

Bluetooth can be very useful in places where there is no local network.

The configuration is not so simple but it works and it seems to be stable.
I've used a Broadcamm dongle.

Lets start with a video that shows an Android device connects to the RPi with PiFace. The Android device controls the leds and monitors the buttons state.
Note that the PiFace extension is only used for demonstration purposes.
(Make sure to set the video setting to HD.)


Configuration

The first step is to install the drivers required.
1. Follow these instructions: https://www.raspberrypi.org/learning/robo-butler/bluetooth-setup/
At this point the Bluetooth should work when you test it with: hcitool scan
2. Run this command:
B4X:
sudo apt-get install libbluetooth-dev
3. Download bluecove_rpi_jars.zip that is attached to this post and copy the jars to the additional libraries folder.
Note that bluecove-gpl is licensed under the GPL license. The other jars are licensed under the Apache 2 license.
The jars are based on this build: http://lukealderton.co.uk/blog/posts/2015/january/raspberry-pi-bluetooth-using-bluecove-on-raspbian/

4. Run the attached example and monitor the logs. You can enable debug logs with this line:
B4X:
SetSystemProperty("bluecove.debug", "true")

Assuming that everything works properly then you should see the MAC address in the logs.
You can use this address to connect from the Android device
No XML file needed? i don't see it!

Walter
 
Top