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 (http://bluecove.org/ 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!
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
Search for the following line:
Change it for:
Setting the SPP connection
rfkill unblock bluetooth
# Discover your phone and find its MAC address
# 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
sdptool add --channel=22 SP
rfcomm watch /dev/rfcomm0 22 > /dev/null &
# Connect for your phone to the Edison establishing the SPP connection
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 .
Let me know if this helps.
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.
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.