Reputation: 13351
Is there any way for Android to connect to a Bluetooth device using a specific port instead of using service UUID? I know this option is available in other platforms which provide Bluetooth support (Java ME for example by specifying a "btspp://" style URL).
Thanks!
Upvotes: 4
Views: 10099
Reputation: 956
I'm using bluecove which allow me to do so with the function Connector.open()
.
I use the following url:
btspp://" + phoneID + ":" + phonePort
N.b.: Some options can be added (e.g.: authenticate=false;
or encrypt=false;
).
With phoneID
being the the being the Bluetooth address and phonePort
the port number.
How to find the Bluetooth address? From this link:
- From the Home screen, open the app drawer, then open “Settings“.
- Select “System“. (Skip this step on some models)
- Scroll down to the bottom and tap “About Phone“, “About device“, or “About tablet“.
- Scroll down to the bottom and tap “Status“.
- Scroll down and the “Bluetooth address” will be shown in the list.
How to find the port number? I haven't been able to find which port is supposed to be used yet... I used 5 and it works but I need to research why and if I want to change the phone I will need to know if I also need to change the port.
Upvotes: 0
Reputation: 13351
Ok, it's been a while, but I found a solution to the problem. I actually intended to give up and use UUID, but I kept getting a Service Discovery Failed (IO)exception, and when I tried to find a solution to the service discovery issue, I found the solution to my original question... Ain't life something?:)
Anyways, this is the link I stumbled upon, though you should note there is a mistake in the answer (they actually simply connected to port 1, instead of using a service UUID).
And after this short history lesson, here is the solution:
Using reflection, it is possible to create the Rfcomm socket connecting to a port number instead of UUID:
int bt_port_to_connect = 5; // just an example, could be any port number you wish
BluetoothDevice device = ... ; // get the bluetooth device (e.g., using bt discovery)
BluetoothSocket deviceSocket = null;
...
// IMPORTANT: we create a reference to the 'createInsecureRfcommSocket' method
// and not(!) to the 'createInsecureRfcommSocketToServiceRecord' (which is what the
// android SDK documentation publishes
Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
deviceSocket = (BluetoothSocket) m.invoke(device,bt_port_to_connect);
A few things to notice:
Good luck to all.
Upvotes: 10
Reputation: 2074
Bluetooth Android connections are exclusively done via UUID. Each Bluetooth device has a UUID for every service it runs (see Bluetooth SDP).
You just give Android the UUID to watch for and, in client mode, it will find a socket to connect to automatically (including port). In server mode, it will wait for the specified device to initiate a connection using the specified UUID. The BluetoothSocket object is also valid when connection is established (use getInput/Output Stream) See Server Socket documentation and Client Socket documentation.
If you really want to check everything, you can see what Android decodes from the other device's SDP and the UUID you provided.
Use this tutorial to get the Bluetooth interface (very easy to do). Then the code should look something like this:
IBluetooth ib =getIBluetooth();
Int otherDevicePort = ib.getRemoteServiceChannel(otherDeviceAddress, UUID);
Upvotes: 0