Android 安卓4.0及;4.1蓝牙问题。检测中断的通信&;下降配对

Android 安卓4.0及;4.1蓝牙问题。检测中断的通信&;下降配对,android,bluetooth,android-bluetooth,bluez,spp,Android,Bluetooth,Android Bluetooth,Bluez,Spp,大家好 据我所知,Android的bluetooth stack(bluez)在4.2上被替换。尽管他们可能已经修复了很多以前的问题,但由于需要支持旧版本,我仍然需要和他们斗争 如果有人曾经处理过这个问题,并且能够提供一些信息,我将非常感激 问题#1-无法检测到断开的通信(4.0和4.1 Android、Bluez蓝牙堆栈) 蓝牙应用程序连接到我们自己的定制SPP设备(我们使用标准的UUID)。它使用在自己的进程上运行的蓝牙服务。需要此应用程序运行数小时才能执行蓝牙工作 在省电/屏幕锁定期间,当

大家好

据我所知,Android的bluetooth stack(bluez)在4.2上被替换。尽管他们可能已经修复了很多以前的问题,但由于需要支持旧版本,我仍然需要和他们斗争

如果有人曾经处理过这个问题,并且能够提供一些信息,我将非常感激

问题#1-无法检测到断开的通信(4.0和4.1 Android、Bluez蓝牙堆栈) 蓝牙应用程序连接到我们自己的定制SPP设备(我们使用标准的UUID)。它使用在自己的进程上运行的蓝牙服务。需要此应用程序运行数小时才能执行蓝牙工作

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

现在;系统大部分时间工作正常,但在一些罕见的情况下,当屏幕锁定并处于省电模式时,由于我不理解的原因,写入输出流(蓝牙插座)时,一切似乎都顺利进行,没有检测到断开的连接。spp设备仍然声明连接和配对有效,但未接收任何内容

在Android端,日志显示了对BluetoothSocket.cpp::WriteEnable(假设它与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):可写性

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

问题-除了应用程序级检查和心跳外,在插座I/O操作时(如本例中)应检测到中断的通信,这是正确的吗?还是蓝牙收音机在省电期间会停止工作

问题#2突然从配对列表中删除。 在Android 4.0和4.1中,在某些情况下,设备会无法解释地从配对列表中删除。即使这种情况也很少见,而且只在某些特定设备中出现……这种情况会阻止手机重新配对和轻松连接

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

注意:对于android版本<4.2,我们确实使用了不安全的通信(CreateSecurerCommSocket,因为该版本存在其他android连接问题)

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

这很可能是我们的SPP设备中的一个bug,但很可能不是,有什么想法吗


非常感谢

这是Nexus7上的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;

您需要刷新缓存,请看这里-但我只是在所有版本上使用不安全套接字