18

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.

JJJ
  • 32,902
  • 20
  • 89
  • 102
sunil
  • 9,541
  • 18
  • 66
  • 88

2 Answers2

29

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.

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • Thanks a lot for pointing me to the right direction. If that API is available since API level 10 only then how do I connect to devices insecurely if I am developing with Android 2.1 – sunil Mar 16 '11 at 07:07
  • 3
    ATM, you have found the best compromise. Just keep in mind that the reflection solution may go away in later versions so you may want to write your code to check the version on the device with `Build.VERSION` and use the public API for 2.3 and later. This will require you to increase your target API Level to 10, but leave your minimum API Level at 5 (or 7) so it will still run on earlier devices running 2.1 or 2.2. Hope that helps! – devunwired Mar 16 '11 at 13:52
  • @Wireless, but will it work with the method createInsecureRfcommSocketToServiceRecord() in API 7? – Chatar Veer Suthar Jul 27 '11 at 07:43
  • 1
    @Veer I'm not sure what you mean since createInsecureRfcommSocketToServiceRecord() is not in API 7, it was introduced in API 10. Access prior to that is only possible via reflection, which is not guaranteed. Perhaps re-read the answer and subsequent comments. – devunwired Jul 29 '11 at 17:14
  • @Devunwired if I could upvote your answer 10 times I would. Just figured out why I could never auto-pair in my app and now I can Thanks! – JPM Jan 13 '12 at 16:33
5

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 !

Sourab Sharma
  • 2,940
  • 1
  • 25
  • 38