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

Discussion in 'B4J Tutorials' started by Erel, Nov 10, 2015.

  1. Erel

    Erel Administrator Staff Member Licensed 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:
    Code:
    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:
    Code:
    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
     

    Attached Files:

    Mark Turney, KMatle, Toley and 3 others like this.
  2. Mark Turney

    Mark Turney Active Member Licensed User

    So awesome Erel ... thanks!!
     
  3. Peter Simpson

    Peter Simpson Expert Licensed User

    Thank you @Erel, this is excellent...
     
  4. ilan

    ilan Expert Licensed 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?
     
  5. inakigarm

    inakigarm Well-Known Member Licensed 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 http://www.b4x.com/android/forum/threads/b4j-and-raspberry-pi-boards.37019/#content )
     
    Erel and ilan like this.
  6. ilan

    ilan Expert Licensed User

    Thanks @inakigarm, do you know where can i buy such a device and also what external device can i use with it?
     
  7. inakigarm

    inakigarm Well-Known Member Licensed 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 http://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/,....)
     
  8. SimonElck

    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?
     
  9. woniol

    woniol Active Member Licensed User

  10. SimonElck

    SimonElck Member Licensed User

    Thanks for the info woniol,
    I guess we will have to be a bit patient then
     
  11. fishwolf

    fishwolf Active Member Licensed User

    i have the same error with raspi2 + dogle

    Code:
    Waiting for debugger to connect...
    Program started.
    BlueCove version 
    2.1.1-SNAPSHOT on bluez
    My 
    address00: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...
     
  12. Erel

    Erel Administrator Staff Member Licensed User

  13. Samuel Ramirez

    Samuel Ramirez Member Licensed User

    it worked for me after applying the instructions related in the link shared by Erel
     
    Erel, inakigarm and Mark Turney like this.
  14. walterf25

    walterf25 Well-Known Member Licensed User

    No XML file needed? i don't see it!

    Walter
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    Which XML file are you looking for?
     
  16. walterf25

    walterf25 Well-Known Member Licensed User

    Sorry, Erel, i was looking for the JBluetooth library, found it.

    Walter
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice