2

I registered a service via

BluetoothServerSocket bs = BluetoothAdapter.getDefaultAdapter().listenUsingInsecureRfcommWithServiceRecord("someName" , UUID.fromString(myUuid)  );

My question is, is there any programmatic way to determine which channel is being used by this service?

============================ANOTHER SOLUTION========================
: Thanks for your anserw @Skaard-Solo ,but if I use reflection to create socket then i cannot provide uuid for this.
I made some research and I wrote some simple function that can obtain channel from BluetoothServerSocket.

public int getChannel(BluetoothServerSocket bsSocket){
    Field[] f = bsSocket.getClass().getDeclaredFields();

    int channel = -1;

    for (Field field : f) {
        if(field.getName().equals("mChannel")){
            field.setAccessible(true);
            try {
                channel = field.getInt(bsSocket);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            field.setAccessible(false);
        }
    }

    return channel;
}
Łukasz Kosiak
  • 513
  • 6
  • 22

1 Answers1

1

No (AFAIK), and that's can be a problem. Android choose a channel which is free during the socket openning.

If you really need to know the channel, you can create a rfcomm socket by reflection, and passing the channel as parameter (Kind of a hack, listenUsingRfcommOn is @hide in Android) :

BluetoothServerSocket tmp = null;
Method m;
m = mAdapter.getClass().getMethod("listenUsingRfcommOn", new Class[] { int.class });
tmp = (BluetoothServerSocket) m.invoke(mAdapter, BLUETOOTH_CHANNEL);

Check also this link, which deals with this ;)

Community
  • 1
  • 1
Skaard-Solo
  • 682
  • 6
  • 11