android蓝牙insecure,Android Bluetooth Connection Secure Insecure

本文探讨了在不同Android API级别下使用蓝牙API进行设备连接的问题。作者最初使用API level 8成功连接蓝牙设备并触发配对请求,但在升级到API level 10后尝试创建安全RfcommSocket时遇到连接被拒绝的异常。通过对比安全和非安全连接的方法,旨在找出问题所在。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值