I am looking at way of connecting over rfcomm socket insecurely. I was able to find the way mentioned below
Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, 1);
This for the time being is doing what I want. Even the documentation over here says that we need to use createInsecureRfcommSocketToServiceRecord for insecure connections. But there is no such method. The only way I found out was using reflection as shown above. And even in that the method that is passed in createInsecureRfcommSocket and not createInsecureRfcommSocketToServiceRecord. I just wanted to know how reliable is this way. If I mention createInsecureRfcommSocketToServiceRecord in method the connection never happens.
createInsecureRfcommSocketToServiceRecord()
was included starting with Android API Level 10, so the documentation will encourage you to use it since the docs always follow the latest version of the API. If you are targeting an API lower than 10 (a.k.a. 2.3.3 or Gingerbread), then that method is not publicly accessible to you.
The method you are calling via reflection createInsecureRfcommSocket()
is a private method inside BluetoothDevice
that has been present since roughly Android 2.0. The problem with calling hidden methods is that they aren't guaranteed to be there on all devices, or in the future...so you're gambling a bit. My guess is your method will probably work most of the time on most 2.0+ devices, since the services required to implement its public cousin createRfcommSocketToServiceRecord()
are so similar at the stack layer.
Bottom line, if you want guaranteed universal compatibility with your Bluetooth implementation, you'll have to target 2.3.3 (API Level 10) with your application. With a public API now exposed for insecure RFCOMM, it's hard to say whether it's more or less likely for the underlying private implementation to change.