NOTE: this is not a general solution and might not work on every phone or future version of the DKs, but it is still a great way for any prototyping purposes ![]()
Unfortunately the SDK currently only supports a small set of the Bluetooth stack. As we wanted to integrate some of our hardware gadgets (over SPP/RFCOMM) with our Dev Phone 1, we needed to digg a little deeper. So here is what you want to do to get it working.
1) Follow the great article by Stephan, to get all the necessary libraries in place. His Bluescan app also provides a good starting point both for SDK and NDK integration.
2) Adapt the NDK specific code to talk to your device.
First allocate a socket
s = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
Next you need to set who to connect to
struct sockaddr_rc addr1 = { 0 }; char dest[18] = "00:06:66:02:9D:39"; //Device ID of your Bluetooth device addr1.rc_family = AF_BLUETOOTH; addr1.rc_channel = (uint8_t) 1; str2ba( dest, &addr1.rc_bdaddr );
Now you can connect to the device
status = connect(s, (struct sockaddr *)&addr1, sizeof(addr1));
Thats pretty much it, you can start sending and receiving stuff with
if( status >= 0 ) { __android_log_write(ANDROID_LOG_INFO,"SPP_COMM","Successfully connected"); status = write(s, "L1\r\n", 4); sleep(1); len1 = recv(s,buf,sizeof(buf),0); if(len1 > 0) __android_log_write(ANDROID_LOG_INFO,"SPP_COMM",buf); else __android_log_write(ANDROID_LOG_ERROR,"SPP_COMM","Nothing was received from the device"); }
HTH
Enjoy
J.
0 Responses to “Android Bluetooth on steroids with the NDK and bluez”