Android BLE:onServicesDiscovered 在 Nexus 5 或 Samsung Note 3 上永远不会触发

2024-01-02

我正在开发一个连接到自定义蓝牙设备的 Android 应用程序。许多帖子非常有帮助(例如this https://stackoverflow.com/questions/17870189/android-4-3-bluetooth-low-energy-unstable and this https://stackoverflow.com/questions/20069507/solved-gatt-callback-fails-to-register),但我遇到的问题没有通过这些建议解决。

蓝牙连接成功后,我调用BluetoothGatt.discoverServices https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#discoverServices(),在大多数设备上都能正确执行。在 Nexus 5 和 Samsung Note 3 上,我从未收到任何响应 (onServicesDiscovered),无论成功与否。这种情况发生在第一次连接尝试时,并且没有任何组合断开() https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#disconnect() or close() https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#close()似乎可以修复它。

我有传闻证据表明这是一个 Android 操作系统错误;只有我们仍然使用 4.4.2 的 Nexus 5 遇到了这个问题,升级到 4.4.3 似乎已经解决了这个问题。然而,这对于三星来说还不是一个选项,如果可能的话,我想找到一个解决方法。我知道我们过去连接到 Nexus 5 时没有出现这些问题;当我试图找出可能发生的变化时,我会很感激任何想法。

三星 Note 3 日志:

07-02 17:29:54.891 I/PeripheralManager( 8282): Connect has been called on HFEC161332B90C15C29E DF:5F:C8:DF:04:35 -43 , shouldPair = true
07-02 17:29:54.891 I/PeripheralManager( 8282): BTSTATE acquiring new gatt and connecting...
07-02 17:29:54.891 D/BluetoothGatt( 8282): connect() - device: DF:5F:C8:DF:04:35, auto: false
07-02 17:29:54.891 D/BluetoothGatt( 8282): registerApp()
07-02 17:29:54.896 D/BluetoothGatt( 8282): registerApp() - UUID=2fc3ce73-c50c-4cda-8b82-1532a5dccb14
07-02 17:29:54.896 D/BtGatt.GattService( 3434): registerClient() - UUID=2fc3ce73-c50c-4cda-8b82-1532a5dccb14
07-02 17:29:54.896 D/BtGatt.btif( 3434): btif_gattc_register_app
07-02 17:29:54.896 D/BtGatt.btif( 3434): btgattc_handle_event: Event 1000
07-02 17:29:54.896 D/BtGatt.btif( 3434): btif_gattc_upstreams_evt: Event 0
07-02 17:29:54.901 D/BtGatt.GattService( 3434): onClientRegistered() - UUID=2fc3ce73-c50c-4cda-8b82-1532a5dccb14, clientIf=5
07-02 17:29:54.901 I/BluetoothGatt( 8282): Client registered, waiting for callback
07-02 17:29:54.901 D/BluetoothGatt( 8282): onClientRegistered() - status=0 clientIf=5
07-02 17:29:54.901 D/BtGatt.GattService( 3434): clientConnect() - address=DF:5F:C8:DF:04:35, isDirect=true
07-02 17:29:54.901 D/BtGatt.btif( 3434): btif_gattc_open
07-02 17:29:54.901 D/BluetoothAdapter( 8282): stopLeScan()
07-02 17:29:54.906 D/BtGatt.btif( 3434): btgattc_handle_event: Event 1004
07-02 17:29:54.906 D/BtGatt.btif( 3434): btif_get_device_type: Device [df:5f:c8:df:04:35] type 2, addr. type 1
07-02 17:29:54.906 D/BtGatt.btif( 3434): btif_gattc_upstreams_evt: Event 2
07-02 17:29:54.906 D/BtGatt.GattService( 3434): onConnected() - clientIf=5, connId=5, address=DF:5F:C8:DF:04:35
07-02 17:29:54.906 D/BluetoothGatt( 8282): onClientConnectionState() - status=0 clientIf=5 device=DF:5F:C8:DF:04:35
07-02 17:29:54.906 I/PeripheralManager( 8282): Gatt State Connected
07-02 17:29:54.906 D/BluetoothGatt( 8282): discoverServices() - device: DF:5F:C8:DF:04:35
07-02 17:29:54.911 D/BtGatt.GattService( 3434): discoverServices() - address=DF:5F:C8:DF:04:35, connId=5
07-02 17:29:54.911 D/BtGatt.btif( 3434): btif_gattc_search_service
07-02 17:29:54.911 I/PeripheralManager( 8282): Gatt Service Discovery Started
07-02 17:29:54.911 D/BtGatt.btif( 3434): btgattc_handle_event: Event 1006
…
07-02 17:30:24.901 W/PeripheralManager( 8282): Connection time out hard
07-02 17:30:24.921 I/PeripheralManager( 8282): canceling connection

连接代码:

public void connect(final Peripheral peripheral, boolean pair) {
    Log.i(TAG,  "Connect has been called on " + peripheral.toString() + ", shouldPair = " + pair);
    this.shouldPair = pair;
    bluetoothHandler.post(new Runnable() {
         @Override
         public void run() {
            if(isConnected) {
                Log.w(TAG, "Connecting when already connected!");
                return;
            }
            if (mBluetoothAdapter == null || peripheral == null || peripheral.getAddress() == null) {
                Log.e(TAG, "BluetoothAdapter not initialized or unspecified address.");
                return;
            }
            final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(peripheral.getAddress());
            if (device == null) {
                Log.e(TAG, "Device not found.  Unable to connect.");
                return;
            }
            if(mBluetoothGatt != null) { // use existing gatt if present and not closed
                Log.i(TAG, "connecting to existing gatt...");
                mBluetoothGatt.connect();
            } else {
                Log.i(TAG, "acquiring new gatt and connecting...");
                mBluetoothGatt = device.connectGatt(context, false, mGattCallback);
            }
         }
    });

    lastConnectedPeripheral = peripheral;
}

发现服务:

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            if (newState == BluetoothGatt.STATE_CONNECTED) {
                Log.i(TAG,"Gatt State Connected");
                isBluetoothConnected = true;
                tryDiscoveringServices();
                ...

请让我知道哪些其他信息可以阐明这一点。谢谢!

编辑:更改固件后,问题减少了:连接不再是从不连接,而是只需要一分钟多一点的时间。这表明问题至少部分是由于我们特定的蓝牙设备以及 Android 操作系统的变化造成的。如果我发现如何解决这个相关问题,我会更新。


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android BLE:onServicesDiscovered 在 Nexus 5 或 Samsung Note 3 上永远不会触发 的相关文章

随机推荐