Problems with Reflection for createRfcommSocket

This is odd. I am trying to configure Bluetooth SPP on a Droid X Android 2.3.4. I have included several different methods, trying to open rfcomm socket, one of which is a reflection (necessary to support certain phones).

However, a strange thing happens with the Droid X. When I first connect to a Bluetooth device, my application opens and first tries to run SPP:

Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
mmSocket = (BluetoothSocket) m.invoke(mmDevice, Integer.valueOf(1));

And if he cannot find the method, he tries to execute normal

mmSocket = mmDevice.createRfcommSocketToServiceRecord(SerialPortServiceClass_UUID);

This works correctly for the first time. But if I close my program and then open it again, the reflection method will actually find the method, connection attempts will not have any errors, but in fact they do not connect.

If I close the application, turn on the Bluetooth adapter on the phone and try again, it will work correctly.

I do not understand what is happening, so any help would be very appreciated.

+2
source share
1 answer

Well, I came up with a bandai-type fix that doesn't suit me, but will work until I find the root cause or the best solution. I just make a general preference object that contains a “level” of what methods to try.

Levels are marked as 1-4, and if the level is set to the number <= to the current level, he will try to use this method if the connection is not established.

if(level<=1){
success set level = 1
fail set level = 0
}

if(level<=2){
success set level = 2
fail set level = 0
}

...


if(level<=4){
success set level = 4
fail set level = 0
}
0
source

All Articles