首页 > 解决方案 > 通过 ble 进行第一次数据传输后应用程序冻结

问题描述

我正在编写一个将数据传输到远程设备(也是我设计的)的应用程序。正在使用低功耗蓝牙。总共,我需要 8 次传输,传输一个接一个。每个后续传输都在前一个的 onCharacteristicWrite 事件之后开始。

问题是在第一次传输后,应用程序冻结了。此时在日志中,我可以看到以下内容:

D / OpenGLRenderer: eglDestroySurface = 0x71e6515eb0
D / ViewRootImpl @ 616c063 [MainActivity]: dispatchDetachedFromWindow
D / InputEventReceiver: channel '1737a41 com.example.example / com.example.example.MainActivity (client)' ~ Disposing input event receiver.
    channel '1737a41 com.example.example / com.example.example.MainActivity (client)' ~ NativeInputEventReceiver.
D / ViewRootImpl @ c8adac [MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1
E / ViewRootImpl: sendUserActionEvent () returned.

此时远端设备上报客户端断线,状态为0x13。(构象第一次传输后)。

屏幕变暗后,应用程序继续工作,日志如下:

V / InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@ce956af nm: com.example.example ic = null
I / InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
D / BLUETOOTH: onCharacteristicWrite

该应用程序重新连接到设备并传输另一条数据。远程设备确认收到数据。

由于数据是有时间限制的,因此这是传输结束的地方。请告诉我如何解决应用程序冻结的问题?

三星智能手机,添加“android:configChanges =” screenSize"" 不能解决问题。

PS 如果有关系,那么我将 MTU 设置为 500,但我每次传输只传输 496 个字节。

我的代码:

ch.dataCharacteristic.setValue(data.get(i));
gatt.writeCharacteristic(gatts.get(i).dataCharacteristic);

标签: androidbluetooth-lowenergy

解决方案


解决了。我必须使用:

ch.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);

如果服务器没有向记录发送响应。


推荐阅读