Connect to a specific bluetooth port on a bluetooth device using Android

Is there any Android way to connect to a bluetooth device using a specific port instead of using the service UUID? I know this option is available on other platforms that support Bluetooth (Java ME, for example, by specifying a "btspp: //" style url).

Thank!

+3


source to share


2 answers


Ok, it took a while, but I found a solution to the problem. I was actually going to give up and use the UUID, but I kept getting a Service Discovery (IO) exception, and when I tried to find a solution to the service discovery problem, I found a solution to my original question ... Ain't life something ?: )

This is the link I came across anyway , although you should notice that there is a bug in the answer (they are actually just connected to port 1 instead of using the UUID service).

And after this short history lesson, here's the solution:

Using reflection, one can create an Rfcomm socket connecting a port number rather than a 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 notes:

  • since we are using Invoke, the first parameter is the object we are calling the method on, the second parameter to invoke is actually the first parameter of the function)
  • There is also a secure version ("createRfcommSocket") that takes the bluetooth channel number as one parameter (again, since this calls the style, you need to pass an object to call the method, as mentioned in -1-)
  • I found what seems to be a link to the prototypes of these functions

Good luck everyone.

+8


source


Bluetooth Android connections are made exclusively via UUID. Every Bluetooth device has a UUID for every service running (see Bluetooth SDP).

You just give Android the UUID to view and in client mode it will find the socket to automatically connect (including the 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 the connection is established (use getInput / Output Stream) See Server Socket documentation and Socket documentation .




If you really want to check everything, you can see what Android decodes from another SDP device and the UUID you provided.

Use this tutorial to get a bluetooth interface (very easy to do). Then the code should look something like this:

IBluetooth ib =getIBluetooth();
Int otherDevicePort = ib.getRemoteServiceChannel(otherDeviceAddress, UUID);

      

0


source







All Articles