【问题标题】:Android 4.0 & 4.1 Bluetooth issues. Detect broken comms & dropping pairingAndroid 4.0 & 4.1 蓝牙问题。检测损坏的通讯和丢失配对
【发布时间】:2014-12-04 16:39:26
【问题描述】:

大家好

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

如果有人以前处理过这个问题并能提供一些启示,我会非常感激

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

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

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

现在;系统大部分时间都运行良好,但在极少数情况下,当 屏幕 锁定 并处于 省电 模式时,我不明白的原因,在 writeoutput-stream (蓝牙套接字)中,一切似乎都通过而没有检测到断开的连接。 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 或密码不正确”。

注意:对于 createInsecureRfcommSocket,由于此版本的其他 android 连接问题)。

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

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

谢谢一百万

【问题讨论】:

标签: android bluetooth android-bluetooth bluez spp


【解决方案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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-16
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 2015-05-18
    • 1970-01-01
    相关资源
    最近更新 更多