3 Replies Latest reply on Dec 22, 2015 12:52 PM by Intel_Peter

    Bluetooth communication using Java?

    Bennett

      Has anyone been able to read and write data via Bluetooth using Java? I'm trying to communicate between an Android and Edison. I can execute the Python script SPP-loopback.py in my Java code on the Edison, and get it to loop back the data to my Android phone, but there's no way to capture the data coming from the Android (or send my own data to the Android), which is what I really need. So now I'm trying to use a Java Bluetooth library like BlueCove (BlueCove - BlueCove JSR-82 project), but I get runtime ClassNotFoundException errors, so I'm clearly doing something wrong. For reference, I'm using Eclipse. I'm new to Linux stuff, so anything not Android/Java related is very new to me. Thanks in advance!

        • 1. Re: Bluetooth communication using Java?
          Intel_Peter

          Hello Bennett,

           

           

          The steps for making an SPP connection manually without using SPP-loopbak.py are the following:

          For this test a Nexus 5 with the free app Bluetooth SPP Manager was used***

           

          Setting Edison to run SPP server

           

          vi /lib/systemd/system/bluetooth.service 
          

           

          Search for the following line:

           

          ExecStart=/usr/lib/bluez5/bluetooth/bluetoothd -d
          

           

          Change it for:

           

          ExecStart=/usr/lib/bluez5/bluetooth/bluetoothd -C
          

           

          Setting the SPP connection

           

          rfkill unblock bluetooth 
          bluetoothctl 
          agent DisplayYesNo 
          default-agent 
          scan on 
          # 
          #Discover your phone and find its MAC address 
          # 
          scan off 
          pair XX:XX:XX:XX:XX:XX 
          # 
          #XX:XX:XX:XX:XX:XX represents your phone's MAC address, when entering the commands remember to change it 
          #Say yes to everything on your Edison and hit the pair button on your phone 
          # 
          trust XX:XX:XX:XX:XX:XX 
          quit 
          sdptool add --channel=22 SP 
          rfcomm watch /dev/rfcomm0 22 > /dev/null & 
          bluetoothctl 
          discoverable on 
          quit 
          # 
          #Connect for your phone to the Edison establishing the SPP connection 
          # 
          cat /dev/rfcomm0
          

           

          If you follow these steps you should be able to catch the SPP data. Now, you can apply this procedure to your project by calling these commands from your code with system calls. In your case I'd suggest you to create a system service that does the Bluetooth configuring for you so you don't have to include it on your code, in that case I'd suggest you to read Automatically connect to a device through Bluetooth.

          Let me know if this helps.

           

          Peter.

          • 2. Re: Bluetooth communication using Java?
            Bennett

            Okay, so I'm able to connect, and send data from my phone (I'm using a Nexus 5 too) to the Edison, and the data will appear in the terminal. But how to I access it in my Java code? I'll need an instance of the connection somehow, right? I originally thought I'd be able to use the UART0 port, but I haven't been able to open that the same way I did the UART1 and UART2 ports.

            • 3. Re: Bluetooth communication using Java?
              Intel_Peter

              I believe the best option would be to store the data in files, and then opening those files in Java to be used within the code. This shouldn't be that hard, but you might have to create a method that catches the data you are interested in within a file, then closes the process, opens the file in Java and then store the data in a variable.

               

              Peter.