Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Ble delayed notification

on android BLE, I measure real time sensing data over notification from BLE device. By default, I get the notify data 1 time per second. However, I want to delay getting data from devices in a certain period. Below is a screenshot of my code.

  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();
like image 391
Horn Daneth Avatar asked Nov 27 '25 19:11

Horn Daneth


2 Answers

The notifications are being sent from the remote sensing device. Therefore, if you want to delay or change the interval at which the sensing data is received, you need to modify the code in the sensor. This is the correct and reliable way to do it. Alternatively, you can start a timer on your Android app (e.g. every 10 seconds). When you get the notifications from the remote device, don't display them. Instead, store them in global variables. Then, when the 10s timer expires, print those variables and restart the timer.

I hope this helps.

like image 156
Youssif Saeed Avatar answered Dec 01 '25 15:12

Youssif Saeed


As Youssif said, the sensor is most likely the peripheral device and the application is the central device. The data is transmitted via GATT/GAP, and depending on your GAP configuration you might be "bottlenecked" in the peripheral device.

this post might help you further Bluetooth Low Energy Notification Interval

A software sleep on the application is a hack solution, but none the less a solution :)

like image 23
Sorenp Avatar answered Dec 01 '25 15:12

Sorenp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!