dcoder
dcoder

Reputation: 13351

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

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

Answers (3)

user1527152
user1527152

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:

  1. From the Home screen, open the app drawer, then open “Settings“.
  2. Select “System“. (Skip this step on some models)
  3. Scroll down to the bottom and tap “About Phone“, “About device“, or “About tablet“.
  4. Scroll down to the bottom and tap “Status“.
  5. 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

dcoder
dcoder

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:

  1. since we're using Invoke, the first parameter is the object we're invoking the method on, the second parameter of invoke is actually the first function parameter)
  2. There is also a secure version available ('createRfcommSocket'), which accepts a bluetooth channel number as a single parameter (again, since this is invoke style, you'll need to pass the object to invoke the method on, as mentioned in -1- )
  3. I found what appears to be a link to these functions' prototypes

Good luck to all.

Upvotes: 10

Radu
Radu

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

Related Questions