通过 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 = "orientation | screenSize"" 不能解决问题。

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

我的代码

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

解决方法

解决了。 我必须使用:

ch.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);

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