Android Ble延迟通知
Android Ble delayed notification
在 android BLE 上,我根据 BLE 设备的通知测量实时传感数据。默认情况下,我每秒获取 1 次通知数据。但是,我想在某个时间段内延迟从设备获取数据。下面是我的代码的屏幕截图。
setNotificationCallback(mTXCharacteristic)
.with(new TemperatureMeasurementDataCallback() {
@Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
PrintLog.i(" "+TemperatureMeasurementParser.parse(data) + " °C");
//Toast.makeText(getContext(), " "+TemperatureMeasurementParser.parse(data) + " °C", Toast.LENGTH_LONG).show();
//super.onDataReceived(device, data);
onTemperatureMeasurementReceived(device, Float.parseFloat(TemperatureMeasurementParser.parse(data)), TemperatureMeasurementCallback.UNIT_C, null, null);
}
@Override
public void onTemperatureMeasurementReceived(@NonNull final BluetoothDevice device,
final float temperature, final int unit,
@Nullable final Calendar calendar,
@Nullable final Integer type) {
mCallbacks.onTemperatureMeasurementReceived(device, temperature, unit, calendar, type);
}
public void onTemperatureMeasurementReceived(@NonNull final BluetoothDevice device,
final float temperature, final int unit,
@Nullable final Calendar calendar,
@Nullable final Integer type, @Nullable final Integer batteryLevel) {
mCallbacks.onTemperatureMeasurementReceived(device, temperature, unit, calendar, type,batteryLevel);
}
});
requestMtu(260).enqueue();
enableNotifications(mTXCharacteristic).enqueue();
//sleep(10000).enqueue();
正在从遥感设备发送通知。因此,如果要延迟或改变接收传感数据的时间间隔,需要修改传感器中的代码。这是正确可靠的方法。或者,您可以在 Android 应用程序上启动计时器(例如每 10 秒一次)。当您从远程设备收到通知时,不要显示它们。相反,将它们存储在全局变量中。然后,当 10s 计时器到期时,打印这些变量并重新启动计时器。
希望对您有所帮助。
正如 Youssif 所说,传感器很可能是外围设备,而应用程序是中央设备。数据通过 GATT/GAP 传输,根据您的 GAP 配置,您可能 "bottlenecked" 在外围设备中。
此 post 可能会进一步帮助您
一个软件sleep on the application是一个hack解决方案,但是none少一个解决方案:)
在 android BLE 上,我根据 BLE 设备的通知测量实时传感数据。默认情况下,我每秒获取 1 次通知数据。但是,我想在某个时间段内延迟从设备获取数据。下面是我的代码的屏幕截图。
setNotificationCallback(mTXCharacteristic)
.with(new TemperatureMeasurementDataCallback() {
@Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
PrintLog.i(" "+TemperatureMeasurementParser.parse(data) + " °C");
//Toast.makeText(getContext(), " "+TemperatureMeasurementParser.parse(data) + " °C", Toast.LENGTH_LONG).show();
//super.onDataReceived(device, data);
onTemperatureMeasurementReceived(device, Float.parseFloat(TemperatureMeasurementParser.parse(data)), TemperatureMeasurementCallback.UNIT_C, null, null);
}
@Override
public void onTemperatureMeasurementReceived(@NonNull final BluetoothDevice device,
final float temperature, final int unit,
@Nullable final Calendar calendar,
@Nullable final Integer type) {
mCallbacks.onTemperatureMeasurementReceived(device, temperature, unit, calendar, type);
}
public void onTemperatureMeasurementReceived(@NonNull final BluetoothDevice device,
final float temperature, final int unit,
@Nullable final Calendar calendar,
@Nullable final Integer type, @Nullable final Integer batteryLevel) {
mCallbacks.onTemperatureMeasurementReceived(device, temperature, unit, calendar, type,batteryLevel);
}
});
requestMtu(260).enqueue();
enableNotifications(mTXCharacteristic).enqueue();
//sleep(10000).enqueue();
正在从遥感设备发送通知。因此,如果要延迟或改变接收传感数据的时间间隔,需要修改传感器中的代码。这是正确可靠的方法。或者,您可以在 Android 应用程序上启动计时器(例如每 10 秒一次)。当您从远程设备收到通知时,不要显示它们。相反,将它们存储在全局变量中。然后,当 10s 计时器到期时,打印这些变量并重新启动计时器。
希望对您有所帮助。
正如 Youssif 所说,传感器很可能是外围设备,而应用程序是中央设备。数据通过 GATT/GAP 传输,根据您的 GAP 配置,您可能 "bottlenecked" 在外围设备中。
此 post 可能会进一步帮助您
一个软件sleep on the application是一个hack解决方案,但是none少一个解决方案:)