Android 蓝牙连接安全不安全
我一直在使用 Android 2.2 的蓝牙 API(API 级别 8,HTC Desire),并使用一个应用程序连接到嵌入式蓝牙设备:
device.createRfcommSocketToServiceRecord(DEV_UUID);
这按预期生成了一个配对请求,但是为了简化我想避免的连接过程配对时的用户交互因此移至 API 级别 10(HTC Desire 与 CyanogenMod 7),因此我可以使用:
device.createInsecureRfcommSocketToServiceRecord(DEV_UUID);
测试时这也按预期工作(连接时不提示用户配对),但是当我尝试创建时API 级别 10 下的安全 RfcommSocket 与之前的 2.2 一样,我收到连接拒绝异常...
java.io.IOException: Connection refused
at android.bluetooth.BluetoothSocket.connectNative(Native Method)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:204)
据我所知,这应该仍然以相同的方式工作,提示用户配对?
编辑:
刚刚使用以下代码再次尝试,结果是相同的(适用于不安全但不适用于安全),我将尝试使用库存 2.3 设备进行测试。
try {
Method m = dev.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class } );
BluetoothSocket bs = (BluetoothSocket)m.invoke(dev, devUUID);
Log.d("TEST", "Method Invoked");
bs.connect();
Log.d("TEST", "Connected to socket");
bs.close();
Log.d("TEST", "Closed Socket");
}
I have been playing around with the bluetooth API for Android 2.2 (API level 8, HTC Desire) and had an app connecting to an embedded Bluetooth device using:
device.createRfcommSocketToServiceRecord(DEV_UUID);
This generated a pairing request as expected, however to streamline the connection process I wanted to avoid the user interaction when pairing so moved to API level 10 (HTC Desire with CyanogenMod 7) so I could use:
device.createInsecureRfcommSocketToServiceRecord(DEV_UUID);
When testing this also works as expected (connecting without prompting the user to pair), however when I try to create the secure RfcommSocket under API level 10 as before with 2.2 I get a connection refused exception...
java.io.IOException: Connection refused
at android.bluetooth.BluetoothSocket.connectNative(Native Method)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:204)
As far as I can tell this should still work in the same way, prompting the user to pair?
EDIT:
Just tried again using the following code and the outcome is the same (working for insecure but not for secure), I will try and get my hands on a stock 2.3 device to test on.
try {
Method m = dev.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class } );
BluetoothSocket bs = (BluetoothSocket)m.invoke(dev, devUUID);
Log.d("TEST", "Method Invoked");
bs.connect();
Log.d("TEST", "Connected to socket");
bs.close();
Log.d("TEST", "Closed Socket");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我的应用程序中寻找类似问题的解决方案时,我从 code.google.com 找到了此博客
它将帮助所有仍在 SO 上寻找此问题解决方案的人
http://mobisocial.stanford.edu/news/2011/03/bluetooth-reflection-and-legacy-nfc/(链接不再工作)
解决方案现在变得非常简单。只需在您的项目中包含 InsecureBluetooth.java 并更改 BluetoothChatService.java 中的 2 行即可。
就是
这样!
While looking for the solution of similar problem in my app, I have found this blog from code.google.com
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/ (link not working anymore)
The solution has become very simple now. Just include InsecureBluetooth.java in your project and change 2 lines in BluetoothChatService.java.
and
Thats it !