11

大家好

我知道 Android 的蓝牙堆栈(bluez)在 4.2 上被替换了。尽管他们可能已经修复了很多以前的问题,但由于需要支持旧版本,我仍然需要与他们战斗。

如果有人以前处理过这个问题并能提供一些启示,我将不胜感激。

问题 #1 - 无法检测到损坏的通讯(4.0 和 4.1 Android,Bluez 蓝牙堆栈)

蓝牙应用程序连接到我们自己的自定义 SPP 设备(我们使用标准UUID)。它使用在它自己的进程上运行的蓝牙服务。这个应用程序需要运行几个小时才能完成蓝牙工作。

省电/屏幕锁定期间,当数据通过蓝牙无线电进入时,应用程序保持活动状态,并且我定期检查设置的警报,我请求 CPU 时间重新连接并继续工作(如有必要)

现在; 系统大部分时间都运行良好,但是,在某些罕见的情况下,当屏幕锁定并处于省电模式时,由于我不明白的原因,在写入输出(蓝牙套接字)时,一切似乎在没有检测到断开连接的情况下通过。spp 设备仍然声明连接和配对有效,但没有收到任何信息。

在 Android 端,日志显示对BluetoothSocket.cpp::writeNative的本机调用(假设它与bluez蓝牙堆栈直接相关)似乎只是将字节正确写入蓝牙无线电而没有报告任何类型的错误。

写入输出流的代码片段:

public void write(byte[] bytes) {
            try {
                Log.d(LOGGER.TAG_BLUETOOTH," bluetooth bytes to write : "+bytes);
                mmOutStream.write(bytes);
                mmOutStream.flush();
                Log.d(LOGGER.TAG_BLUETOOTH," bluetooth bytes written : "+bytes);
            } catch (IOException e) { 
                e.printStackTrace();
            }
        }

日志猫:

D/com.our.app.bluetooth(8711):字节发送:[B@41e0bcf8

D/com.our.app.bluetooth(8711):要写入的蓝牙字节数:[B@41e0bcf8

V/BluetoothSocket.cpp(8711):writeNative

D/com.our.app.bluetooth(8711):蓝牙字节写入:[B@41e0bcf8

问题- 假设除了应用程序级别的检查和心跳之外,应该在这种情况下的套接字 I/O 操作中检测到​​损坏的通信是否正确?还是蓝牙收音机在省电期间会掉线?

问题 #2 - 突然从配对列表中删除。

在 Android 4.0 和 4.1 中,设备在某些情况下会莫名其妙地从配对列表中删除。即使这种情况很少见,而且仅在某些特定设备中才有……这种情况会阻止手机轻松重新配对和连接。

我确实注意到SPP设备配对正确,但有时,android 设备会显示消息“无法与设备 X 配对,PIN 或密码不正确”。

注意:对于 < 4.2 的 android 版本,我们确实使用不安全的通信(createInsecureRfcommSocket,由于此版本的其他 android 连接问题)。

问题- 在会话期间应多久刷新一次此 PIN/密码?

这很可能是我们 SPP 设备中的一个错误,但如果不是,有什么想法吗?

谢谢一百万

4

1 回答 1

1

这是在nexus 7上运行的android 4.4.2

private boolean refreshDeviceCache(BluetoothGatt gatt){
try {
    BluetoothGatt localBluetoothGatt = gatt;
    Method localMethod = localBluetoothGatt.getClass().getMethod("refresh", new Class[0]);
    if (localMethod != null) {
       boolean bool = ((Boolean) localMethod.invoke(localBluetoothGatt, new Object[0])).booleanValue();
        return bool;
     }
} 
catch (Exception localException) {
    Log.e(TAG, "An exception occured while refreshing device");
}
return false;}


public boolean connect(final String address) {
       if (mBluetoothAdapter == null || address == null) {
        Log.w(TAG,"BluetoothAdapter not initialized or unspecified address.");
            return false;
    }
        // Previously connected device. Try to reconnect.
        if (mBluetoothGatt != null) {
            Log.d(TAG,"Trying to use an existing mBluetoothGatt for connection.");
          if (mBluetoothGatt.connect()) {
                return true;
           } else {
            return false;
           }
    }

    final BluetoothDevice device = mBluetoothAdapter
            .getRemoteDevice(address);
    if (device == null) {
        Log.w(TAG, "Device not found.  Unable to connect.");
        return false;
    }

    // We want to directly connect to the device, so we are setting the
    // autoConnect
    // parameter to false.
    mBluetoothGatt = device.connectGatt(MyApp.getContext(), false, mGattCallback));
    refreshDeviceCache(mBluetoothGatt);
    Log.d(TAG, "Trying to create a new connection.");
    return true;
于 2015-07-15T08:46:13.453 回答