首页 > 解决方案 > 为什么BLE连接失败

问题描述

我有一个到 NINA B1 模块的 Android BLE 应用程序连接。大多数时候我可以进行 BLE 连接,但有时不能。我似乎无法弄清楚为什么有时连接会失败。

我正在添加两个 Logcat 输出,一个用于连接到 BLE,另一个用于连接到 BLE 失败。在建立 BLE 连接之前,两个日志猫都被清除了。

下面是BLE连接成功的时间。

2021-05-17 09:15:12.311 15138-15138/com.example.ble D/BluetoothAdapter: stopLeScan()
2021-05-17 09:15:12.313 15138-15138/com.example.ble D/BluetoothAdapter: getLeState() returning 12
2021-05-17 09:15:12.313 15138-15138/com.example.ble D/BluetoothAdapter: scan not started yet
2021-05-17 09:15:12.315 15138-15138/com.example.ble D/BluetoothDevice: getName: name = 043002aaa
2021-05-17 09:15:12.316 15138-15138/com.example.ble D/BluetoothDevice: getName: name = 043002aaa
2021-05-17 09:15:12.318 15138-15138/com.example.ble D/BluetoothDevice: getName: name = 043002aaa
2021-05-17 09:15:12.319 15138-15138/com.example.ble D/BluetoothDevice: getName: name = 043002aaa
2021-05-17 09:15:12.321 15138-15138/com.example.ble D/BluetoothDevice: getName: name = 043002aaa
2021-05-17 09:15:12.323 15138-15138/com.example.ble D/BluetoothDevice: getName: name = 043002aaa
2021-05-17 09:15:12.342 15138-15138/com.example.ble V/ActivityThread: SVC-Creating service CreateServiceData{token=android.os.BinderProxy@621fcbe className=com.example.ble.BTLE_Services packageName=com.example.ble intent=null}
2021-05-17 09:15:12.348 15138-15138/com.example.ble D/ActivityThread: SVC-Calling onStartCommand: com.example.ble.BTLE_Services@6130c6c, flags=0, startId=1
2021-05-17 09:15:12.463 15138-15138/com.example.ble V/ActivityThread: SVC-Creating service CreateServiceData{token=android.os.BinderProxy@f9eecb1 className=com.example.ble.Service_BTLE_GATT packageName=com.example.ble intent=null}
2021-05-17 09:15:12.469 15138-15138/com.example.ble D/ActivityThread: SVC-Calling onStartCommand: com.example.ble.Service_BTLE_GATT@49580e9, flags=0, startId=1
2021-05-17 09:15:12.577 15138-15138/com.example.ble D/BluetoothGatt: connect() - device: CC:F9:57:90:41:4C, auto: false
2021-05-17 09:15:12.577 15138-15138/com.example.ble D/BluetoothGatt: registerApp()
2021-05-17 09:15:12.578 15138-15138/com.example.ble D/BluetoothGatt: registerApp() - UUID=fe08736c-d7a1-4ec0-864f-342adbcbff00
2021-05-17 09:15:12.581 15138-15138/com.example.ble D/Service_BTLE_GATT: Trying to create a new connection.
2021-05-17 09:15:12.581 15138-15151/com.example.ble D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7
2021-05-17 09:15:14.413 15138-15150/com.example.ble D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=7 device=CC:F9:57:90:41:4C
2021-05-17 09:15:14.417 15138-15150/com.example.ble I/Service_BTLE_GATT: Connected to GATT server.
2021-05-17 09:15:14.417 15138-15150/com.example.ble D/BluetoothGatt: discoverServices() - device: CC:F9:57:90:41:4C
2021-05-17 09:15:14.420 15138-15150/com.example.ble I/Service_BTLE_GATT: Attempting to start service discovery:true
2021-05-17 09:15:14.464 15138-15138/com.example.ble D/WindowClient: Add to mViews: android.widget.LinearLayout{2bc42a5 V.E...... ......I. 0,0-0,0}, this = android.view.WindowManagerGlobal@b28bca
2021-05-17 09:15:14.472 15138-15167/com.example.ble D/OpenGLRenderer: CanvasContext() 0x7e8639e000
2021-05-17 09:15:14.475 15138-15138/com.example.ble D/ViewRootImpl[Toast]: hardware acceleration is enabled, this = ViewRoot{af3bad2 Toast,ident = 3}
2021-05-17 09:15:14.483 15138-15150/com.example.ble D/BluetoothGatt: onSearchComplete() = Device=CC:F9:57:90:41:4C Status=0
2021-05-17 09:15:14.507 15138-15167/com.example.ble D/OpenGLRenderer: CanvasContext() 0x7e8639e000 initialize window=0x7e719b1000, title=Toast
2021-05-17 09:15:14.507 15138-15138/com.example.ble D/Surface: Surface::allocateBuffers(this=0x7e719b1000)
                                                                                                                                              
2021-05-17 09:15:14.507 15138-15167/com.example.ble D/Surface: Surface::connect(this=0x7e719b1000,api=1)
2021-05-17 09:15:14.508 15138-15167/com.example.ble W/libEGL: [ANDROID_RECORDABLE] format: 1
2021-05-17 09:15:14.510 15138-15167/com.example.ble D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000
2021-05-17 09:15:14.520 15138-15138/com.example.ble D/BluetoothGatt: readCharacteristic() - uuid: 2456e1b9-26e2-8f83-e744-f34f01e9d703
2021-05-17 09:15:14.522 15138-15138/com.example.ble D/BluetoothGatt: setCharacteristicNotification() - uuid: 2456e1b9-26e2-8f83-e744-f34f01e9d703 enable: true
2021-05-17 09:15:14.531 15138-15167/com.example.ble D/OpenGLRenderer: CacheTexture 3 upload: x, y, width height = 156, 0, 20, 58
2021-05-17 09:15:14.532 15138-15167/com.example.ble D/GraphicBuffer: register, handle(0x7e6ee33980) (w:397 h:95 s:400 f:0x1 u:0x000b00)
2021-05-17 09:15:14.539 15138-15167/com.example.ble D/GraphicBuffer: register, handle(0x7e6ee34d00) (w:397 h:95 s:400 f:0x1 u:0x000b00)
2021-05-17 09:15:14.648 15138-15143/com.example.ble I/art: Do full code cache collection, code=202KB, data=230KB
2021-05-17 09:15:14.650 15138-15143/com.example.ble I/art: After code cache collection, code=182KB, data=168KB
2021-05-17 09:15:14.701 15138-15151/com.example.ble D/BluetoothGatt: onCharacteristicRead() - Device=CC:F9:57:90:41:4C handle=25 Status=0
2021-05-17 09:15:14.726 15138-15138/com.example.ble D/BluetoothGatt: writeDescriptor() - uuid: 00002902-0000-1000-8000-00805f9b34fb
2021-05-17 09:15:15.052 15138-15150/com.example.ble D/BluetoothGatt: onDescriptorWrite() - Device=CC:F9:57:90:41:4C handle=26
2021-05-17 09:15:16.463 15138-15167/com.example.ble D/Surface: Surface::disconnect(this=0x7e719b1000,api=1)
2021-05-17 09:15:16.470 15138-15167/com.example.ble D/GraphicBuffer: unregister, handle(0x7e6ee33980) (w:397 h:95 s:400 f:0x1 u:0x000b00)
2021-05-17 09:15:16.470 15138-15167/com.example.ble D/GraphicBuffer: unregister, handle(0x7e6ee34d00) (w:397 h:95 s:400 f:0x1 u:0x000b00)
2021-05-17 09:15:16.470 15138-15167/com.example.ble D/Surface: Surface::disconnect(this=0x7e719b1000,api=1)
2021-05-17 09:15:16.478 15138-15138/com.example.ble D/WindowClient: Remove from mViews: android.widget.LinearLayout{2bc42a5 V.E...... ........ 0,0-397,95}, this = android.view.WindowManagerGlobal@b28bca

以下是 BLE 连接不成功的情况。

2021-05-17 09:19:22.731 15422-15422/com.example.ble D/BluetoothAdapter: stopLeScan()
2021-05-17 09:19:22.732 15422-15422/com.example.ble D/BluetoothAdapter: getLeState() returning 12
2021-05-17 09:19:22.733 15422-15422/com.example.ble D/BluetoothAdapter: scan not started yet
2021-05-17 09:19:22.735 15422-15422/com.example.ble D/BluetoothDevice: getName: name = 043002aaa
2021-05-17 09:19:22.738 15422-15422/com.example.ble D/BluetoothDevice: getName: name = 043002aaa
2021-05-17 09:19:22.742 15422-15422/com.example.ble D/BluetoothDevice: getName: name = 043002aaa
2021-05-17 09:19:22.744 15422-15422/com.example.ble D/BluetoothDevice: getName: name = 043002aaa
2021-05-17 09:19:22.748 15422-15422/com.example.ble D/BluetoothDevice: getName: name = 043002aaa
2021-05-17 09:19:22.751 15422-15422/com.example.ble D/BluetoothDevice: getName: name = 043002aaa
2021-05-17 09:19:22.821 15422-15422/com.example.ble V/ActivityThread: SVC-Creating service CreateServiceData{token=android.os.BinderProxy@430c779 className=com.example.ble.BTLE_Services packageName=com.example.ble intent=null}
2021-05-17 09:19:22.832 15422-15422/com.example.ble D/ActivityThread: SVC-Calling onStartCommand: com.example.ble.BTLE_Services@6eb11f, flags=0, startId=1
2021-05-17 09:19:22.857 15422-15422/com.example.ble V/ActivityThread: SVC-Creating service CreateServiceData{token=android.os.BinderProxy@35a8358 className=com.example.ble.Service_BTLE_GATT packageName=com.example.ble intent=null}
2021-05-17 09:19:22.861 15422-15422/com.example.ble D/ActivityThread: SVC-Calling onStartCommand: com.example.ble.Service_BTLE_GATT@f2a6570, flags=0, startId=1
2021-05-17 09:19:22.882 15422-15422/com.example.ble D/BluetoothGatt: connect() - device: CC:F9:57:90:41:4C, auto: false
2021-05-17 09:19:22.882 15422-15422/com.example.ble D/BluetoothGatt: registerApp()
2021-05-17 09:19:22.883 15422-15422/com.example.ble D/BluetoothGatt: registerApp() - UUID=6f17fddb-9ffd-421b-a3b0-59d415dd51ae
2021-05-17 09:19:22.886 15422-15422/com.example.ble D/Service_BTLE_GATT: Trying to create a new connection.
2021-05-17 09:19:22.887 15422-15435/com.example.ble D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7
2021-05-17 09:19:23.857 15422-15435/com.example.ble D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=7 device=CC:F9:57:90:41:4C
2021-05-17 09:19:23.857 15422-15435/com.example.ble I/Service_BTLE_GATT: Disconnected from GATT server.
                                                                                                                   
                                                                                                                   
2021-05-17 09:19:23.903 15422-15422/com.example.ble D/WindowClient: Add to mViews: android.widget.LinearLayout{128689c V.E...... ......I. 0,0-0,0}, this = android.view.WindowManagerGlobal@b28bca
2021-05-17 09:19:23.905 15422-15447/com.example.ble D/OpenGLRenderer: CanvasContext() 0x7e8639e000
2021-05-17 09:19:23.906 15422-15422/com.example.ble D/ViewRootImpl[Toast]: hardware acceleration is enabled, this = ViewRoot{994ae2b Toast,ident = 3}
2021-05-17 09:19:23.911 15422-15422/com.example.ble D/BluetoothGatt: close()
2021-05-17 09:19:23.911 15422-15422/com.example.ble D/BluetoothGatt: unregisterApp() - mClientIf=7
2021-05-17 09:19:23.940 15422-15422/com.example.ble D/Surface: Surface::allocateBuffers(this=0x7e719bc000)
2021-05-17 09:19:23.940 15422-15447/com.example.ble D/OpenGLRenderer: CanvasContext() 0x7e8639e000 initialize window=0x7e719bc000, title=Toast
2021-05-17 09:19:23.940 15422-15447/com.example.ble D/Surface: Surface::connect(this=0x7e719bc000,api=1)
2021-05-17 09:19:23.941 15422-15447/com.example.ble W/libEGL: [ANDROID_RECORDABLE] format: 1
2021-05-17 09:19:23.943 15422-15447/com.example.ble D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000
                                                                                                                                      
                                                                                                                                                              
2021-05-17 09:19:23.955 15422-15447/com.example.ble D/OpenGLRenderer: CacheTexture 3 upload: x, y, width height = 60, 0, 115, 253
2021-05-17 09:19:23.957 15422-15447/com.example.ble D/GraphicBuffer: register, handle(0x7e6ee32f00) (w:485 h:95 s:496 f:0x1 u:0x000b00)
2021-05-17 09:19:23.963 15422-15447/com.example.ble D/GraphicBuffer: register, handle(0x7e6ee65740) (w:485 h:95 s:496 f:0x1 u:0x000b00)
2021-05-17 09:19:24.111 15422-15427/com.example.ble I/art: Compiler allocated 8MB to compile void android.widget.LinearLayout.measureVertical(int, int)
                                                                                                              
                                                                                                                                         
                                                                                                                                   
                                                                                                                             
2021-05-17 09:19:25.902 15422-15447/com.example.ble D/Surface: Surface::disconnect(this=0x7e719bc000,api=1)
2021-05-17 09:19:25.904 15422-15447/com.example.ble D/GraphicBuffer: unregister, handle(0x7e6ee32f00) (w:485 h:95 s:496 f:0x1 u:0x000b00)
2021-05-17 09:19:25.905 15422-15447/com.example.ble D/GraphicBuffer: unregister, handle(0x7e6ee65740) (w:485 h:95 s:496 f:0x1 u:0x000b00)
2021-05-17 09:19:25.905 15422-15447/com.example.ble D/Surface: Surface::disconnect(this=0x7e719bc000,api=1)
2021-05-17 09:19:25.923 15422-15422/com.example.ble D/WindowClient: Remove from mViews: android.widget.LinearLayout{128689c V.E...... ........ 0,0-485,95}, this = android.view.WindowManagerGlobal@b28bca

您能否指出 Logcat 中的一些信息,这些信息可能对我连接失败的原因有所帮助?

马里努斯

标签: androidbluetooth-lowenergy

解决方案


推荐阅读