4

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!

dcoder
  • 12,651
  • 2
  • 20
  • 24

3 Answers3

10

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.

Community
  • 1
  • 1
dcoder
  • 12,651
  • 2
  • 20
  • 24
0

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);
Community
  • 1
  • 1
Radu
  • 2,076
  • 2
  • 20
  • 40
  • Thanks! I actually found a solution to the problem (now as an answer below). When I tried to use UUID, I kept getting an IOException of Service Discovery Failed, but I'll check your suggestion and see if this solves the exception.. – dcoder Apr 28 '12 at 18:34
  • I don't know about this....I mean sure it fixes your problem, but, how on earth do you know the other device's port without SDP? Do you just assume it is always 1 (because yo do nothing else with the other device on Bluetooth?) – Radu May 03 '12 at 08:13
  • My problem was indeed uncommon and very specific, since I need to communicate with a custom device, which is always listening on a specific port and doesn't expose a service record UUID. You're right that in most cases, the standard (and probably the recommended) way is to use SDP to find a service record the device exposes and find the port based on that record. – dcoder May 03 '12 at 09:52
  • @radu I'm trying to connect the Android app to a Linux computer. Unfortunately the Python bluetooth API connects via port number instead of a UUID. In older versions of the Android BluetoothDevice API, it used to allow RFCOMMs connection to a port number as well. However, in current Kotlin versions, that's been phased out. – Stigma Jun 23 '21 at 07:46
0

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.

user1527152
  • 946
  • 1
  • 13
  • 37