【发布时间】:2012-10-10 12:00:20
【问题描述】:
我正在从事一个需要通过蓝牙连接到打印机的项目。打印机制造商声明只有具有 SPP(串行端口配置文件)的安卓手机才能与打印机连接。
这是我最初打开连接的方式:
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //SPP long UUID
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid);
从 JellyBean 开始,使用 UUID 是使用 Android 公共 API 打开 RFCOMM 连接的唯一方法。之前在不需要 UUID 的 BlackBerry 和 JavaME 中使用 SPP 连接,我发现这有点奇怪。 UUID 是关于服务发现的,即使用 SDP 查询设备中存在的服务。我真的不需要启动发现,因为我已经提前配对了我的打印机,而且我知道它支持 SPP。然而,这正是BluetoothDevice.createRfcommSocketToServiceRecord 方法和不安全版本所做的。这是 SPP 堆栈,我们可以在其中看到 SDP 是同一层的不同协议,因此应该可以在不首先启动发现的情况下使用 RFCOMM:
-----------------------------------
| My Application |
-----------------------------------
| Serial Port Emulation |
| or other API |
-----------------------------------
| RFCOMM | SDP |
-----------------------------------
| LMP | L2PCAP |
-----------------------------------
| Baseband |
-----------------------------------
我开始在一些旧的 HTC 设备上测试我的应用程序,没有出现任何问题。后来在三星手机上测试,多台设备无法打开连接。 根据制造商和第 3 方规范,这些手机据称不支持 SPP 配置文件(编辑:第 3 方规范将 SPP 列为支持,但制造商规范不够准确 )。抛出了一个 IOException (Service Discovery failed),我按照这个问题所示的方法:
Service discovery failed exception using Bluetooth on Android
那里提出的解决方案是使用反射黑客,如下:
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
BluetoothSocket socket = socket = (BluetoothSocket) m.invoke(device, 1);
黑客对我有用。令人惊讶的是,BluetoothDevice 类中的这个方法是公共的,但它通过 @hide 注释从 API 中删除。这是 JellyBean 的源代码:
/**
* Create an RFCOMM {@link BluetoothSocket} ready to start a secure
* outgoing connection to this remote device on given channel.
* <p>The remote device will be authenticated and communication on this
* socket will be encrypted.
* <p> Use this socket only if an authenticated socket link is possible.
* Authentication refers to the authentication of the link key to
* prevent man-in-the-middle type of attacks.
* For example, for Bluetooth 2.1 devices, if any of the devices does not
* have an input and output capability or just has the ability to
* display a numeric key, a secure socket connection is not possible.
* In such a case, use {#link createInsecureRfcommSocket}.
* For more details, refer to the Security Model section 5.2 (vol 3) of
* Bluetooth Core Specification version 2.1 + EDR.
* <p>Use {@link BluetoothSocket#connect} to initiate the outgoing
* connection.
* <p>Valid RFCOMM channels are in range 1 to 30.
* <p>Requires {@link android.Manifest.permission#BLUETOOTH}
*
* @param channel RFCOMM channel to connect to
* @return a RFCOMM BluetoothServerSocket ready for an outgoing connection
* @throws IOException on error, for example Bluetooth not available, or
* insufficient permissions
* @hide
*/
public BluetoothSocket createRfcommSocket(int channel) throws IOException {
return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, channel,
null);
}
我不明白为什么会以这种方式从 API 中删除公共方法。但是让这个appart,这个方法和官方支持的使用UUID的方法都是薄信封,用不同的参数调用相同的BluetoothSocket构造函数:
public BluetoothSocket createRfcommSocketToServiceRecord(UUID uuid) throws IOException {
return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, -1,
new ParcelUuid(uuid));
}
进一步挖掘源代码,我意识到两者都打开了 RFCOMM 连接,但 UUID 方法会启动发现,而隐藏的方法则不会。
总而言之,反射黑客在我测试过的每台设备(从 OS 2.2 到 4.1)中都能完美运行。 编辑:“故障”设备确实支持 SPP,只是他们自定义的 BT 堆栈实现弄乱了发现过程;还有其他错误,例如在 ICS 中显示已配对设备的配对对话框。使用反射调用这个隐藏的 API 可以解决所有这些错误或不同制造商引入的不同行为。
我应该将 hack 保留在生产代码中吗?有没有办法通过公共 API 实现相同的功能?
提前致谢。
【问题讨论】:
标签: android reflection bluetooth