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.
Well most of replied answers here, are before March 30 2011, in this post.
While looking for the solution of similar problem in my app, I have found this blog written on March 30.
It will help all those who are still looking for this problem solution on SO
http://mobisocial.stanford.edu/news/2011/03/bluetooth-reflection-and-legacy-nfc/
The solution has become very simple now. Just include InsecureBluetooth.java in your project and change 2 lines in BluetoothChatService.java.
tmp = InsecureBluetooth.listenUsingRfcommWithServiceRecord(mAdapter, NAME, MY_UUID, true);
and
tmp = InsecureBluetooth.createRfcommSocketToServiceRecord(device, MY_UUID, true);
Thats it !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With