How to create Insecure RFCOMM Socket in Android?

我是研究僧i 提交于 2019-11-27 18:12:20

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 !

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!