Reputation: 486
I am working on an Android App whose purpose is to connect to a Bluetooth device which is waiting for input and is already paired with my mobile phone.
I already have the MAC adres of the device into a string and I managed to send the Mac Adress to a new activity where I'll do the connection. The problem is I don't understand how to...
I have been trying to use http://developer.android.com/guide/topics/connectivity/bluetooth.html and it has worked so far but when it comes to the connection part I just can't understand it anymore.
It talks about an UUID but it doesn't tell me where to find it, after some research and testing I see it can't be a string and in an example i found on the net, somebody was parsing a String or INt into UUID and the numbers looked nothing like a MAC adress...
This is what I have so far and where i think the connection should be done
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
I can't find that UUID nor understand how to cast my Mac Adress to it.
And if it's all wrong, does anybody have a CLEAR and COMPLETE code example which explains HOW to connect to the device and what I should do with the MAC adress? I can't seem to find anything clear and working on the net...
Thanks in advance
Upvotes: 2
Views: 2384
Reputation: 124648
You can find a full working example in the open-source BluetoothViewer app: https://github.com/janosgyerik/bluetoothviewer
Specifically the code to connect to Bluetooth devices is here: https://github.com/janosgyerik/bluetoothviewer/blob/master/src/net/bluetoothviewer/BluetoothChatService.java
The UUID is something you generate yourself, and just save it as a constant in your app. I'm not quite sure what's the point of that, but that's just how it works. You can generate a UUID using the uuidgen
command line tool, simply run without parameters. In Mac OS X uuidgen
should be installed by default, in Debian (and probably Ubuntu) it's in the uuid-runtime
package.
I created this Android app precisely for this kind of purpose: debug connections with any Bluetooth device you want to connect to, observe and understand its protocol. I hope you'll find this helpful.
Upvotes: 1
Reputation: 1244
Not a direct answer to your question, but I designed a controller for an electronic LED display, and then wrote an android app based off the bluetooth chat example in the SDK in order to send messages to the sign with my phone
Check here:BluetoothSignController for the project, and you can see some screenshots and code examples on how the android portion works here
Hopefully that can tell you how to connect your phone to whatever you're connecting to. You don't need the MAC address, just scan and pair.
Upvotes: 1