Android BLE应用接收ESP32数据时崩溃求助(附日志)
Android BLE应用崩溃与Gatt写入失败问题
我正在开发Android BLE应用,接收ESP32设备数据时应用崩溃,提示Input channel destroyed: 'ClientS', fd=172。作为BLE开发新手,我需要实现从ESP32获取数据并在UI展示的功能,同时还遇到Gatt特征写入失败的问题,详细日志如下:
详细日志
488 set: Unset optional value from type SMPTE2086 2023-09-29 15:40:39.867 19329-19358 IMGMapper com.eapro.myproject E set:488 set: Unset optional value from type CTA861_3 2023-09-29 15:40:39.890 19329-19358 IMGMapper com.eapro.myproject E set:488 set: Unset optional value from type SMPTE2086 2023-09-29 15:40:39.890 19329-19358 IMGMapper com.eapro.myproject E set:488 set: Unset optional value from type CTA861_3 2023-09-29 15:40:39.900 19329-19358 IMGMapper com.eapro.myproject E set:488 set: Unset optional value from type SMPTE2086 2023-09-29 15:40:39.900 19329-19358 IMGMapper com.eapro.myproject E set:488 set: Unset optional value from type CTA861_3 2023-09-29 15:40:39.914 19329-19358 IMGMapper com.eapro.myproject E set:488 set: Unset optional value from type SMPTE2086 2023-09-29 15:40:39.914 19329-19358 IMGMapper com.eapro.myproject E set:488 set: Unset optional value from type CTA861_3 2023-09-29 15:40:39.929 19329-19329 ViewRootIm...[Connects] com.eapro.myproject I MSG_WINDOW_FOCUS_CHANGED 0 0 2023-09-29 15:40:39.976 19329-19329 DecorView com.eapro.myproject I [INFO] isPopOver=false config=true 2023-09-29 15:40:39.976 19329-19329 DecorView com.eapro.myproject I updateCaptionType: isFloating=false isApplication=true hasWindowDecorCaption=false this=DecorView@5b3e97b[] 2023-09-29 15:40:39.976 19329-19329 DecorView com.eapro.myproject D setCaptionType = 0, this = DecorView@5b3e97b[] 2023-09-29 15:40:39.981 19329-19329 DecorView com.eapro.myproject I getCurrentDensityDpi: from real metrics. densityDpi=300 msg=resources_loaded 2023-09-29 15:40:39.982 19329-19329 DecorView com.eapro.myproject I setWindowBackground: isPopOver=false color=ffffffff d=android.graphics.drawable.ColorDrawable@af0d7f1 2023-09-29 15:40:40.144 19329-19339 InputTransport com.eapro.myproject D Input channel destroyed: 'ClientS', fd=172 2023-09-29 15:40:40.144 19329-19339 InputTransport com.eapro.myproject D Input channel destroyed: 'ClientS', fd=119 2023-09-29 15:40:40.187 19329-19337 eapro.myproject com.eapro.myproject I Compiler allocated 4177KB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int) 2023-09-29 15:40:40.196 19329-19329 <no-tag> com.eapro.myproject D javedesp32 2023-09-29 15:40:40.199 19329-19329 <no-tag> com.eapro.myproject D oldhaiesp32 2023-09-29 15:40:41.225 19329-19329 DetailBlewrite fail com.eapro.myproject D BleException { code=102, description='gatt writeCharacteristic fail'} 2023-09-29 15:40:41.225 19329-19329 Tag com.eapro.myproject D msg0 2023-09-29 15:40:41.230 19329-19329 DetailBlewrite fail com.eapro.myproject D BleException { code=102, description='gatt writeCharacteristic fail'} 2023-09-29 15:40:41.230 19329-19329 Tag com.eapro.myproject D msg0 2023-09-29 15:40:41.736 19329-19329 BluetoothGatt com.eapro.myproject D setCharacteristicNotification() - uuid: beb5483e-36e1-4688-b7f5-ea07361b26a8 enable: true 2023-09-29 15:40:42.748 19329-19329 BluetoothGatt com.eapro.myproject D configureMTU() - device: D4D4DA_2 mtu: 200 2023-09-29 15:40:42.750 19329-19329 DetailBleonCreate com.eapro.myproject D Device id not connected 2023-09-29 15:40:42.754 19329-19329 DetailBle com.eapro.myproject D Connected
问题排查与解决建议
1. 应用崩溃(Input channel destroyed)
Input channel destroyed是窗口销毁的伴随日志,并非崩溃直接原因,需排查以下点:
- UI更新线程问题:BLE回调(如
onCharacteristicChanged)运行在Binder线程,直接更新UI会触发异常,必须通过runOnUiThread或Handler切换到主线程更新。 - 生命周期资源释放:在Activity的
onDestroy方法中调用bluetoothGatt.close(),释放BLE连接资源,避免内存泄漏导致的异常。 - 捕获完整崩溃堆栈:当前日志未包含崩溃的异常堆栈,需在Logcat中筛选
Crash类型日志,定位具体崩溃原因。
2. Gatt特征写入失败(code=102)
该错误通常由连接状态、特征属性或操作时机问题导致:
- 确认连接与服务发现流程:日志中出现连接状态矛盾信息,需确保在
onConnectionStateChange回调确认连接成功后,调用discoverServices(),待onServicesDiscovered回调返回后,再执行特征写入操作。 - 检查特征写入属性:通过
characteristic.getProperties()验证目标特征是否包含BluetoothGattCharacteristic.PROPERTY_WRITE或PROPERTY_WRITE_NO_RESPONSE属性,不支持写入的特征会导致失败。 - MTU配置时机:日志中调用了
configureMTU(200),需在onMtuChanged回调确认MTU协商完成后,再执行大尺寸数据写入,否则会导致写入失败。 - 避免频繁写入:短时间内连续写入会阻塞BLE队列,需等待前一次写入的
onCharacteristicWrite回调返回后,再发起下一次写入请求。
内容的提问来源于stack exchange,提问作者Javed Quantum
相关产品推荐
相关产品推荐

