0

I want to do multiple writes using a timer. I am trying to do the following:

When I do a write I want to recieve the response (OnWriteSuccess) and when I get it, I will write again.

The first ten - twenty writes are ok but after that, I have a delay between each write of 2 - 4 seconds.

boolean envia = true;
 public void write() {


    Timer myTimer = null;
    if (myTimer == null) {
        Handler handler = null;
        if (handler == null) {
            handler = new Handler();
        }

        myTimer = new Timer();
        Handler finalHandler = handler;
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                finalHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (envia) {
                            envia = false;


                            if (buc == 256) {
                                buc = 0;
                            }


                            byte bytesToWrite[] = new byte[20];
                            for (int x = 0; x < 20; x++) {
                                bytesToWrite[x] = (byte) buc;
                                buc++;
                            }
                            if (isConnected()) {
                                connectionObservable
                                        .flatMap(rxBleConnection -> rxBleConnection.writeCharacteristic(UUID_1, bytesToWrite))
                                        .observeOn(AndroidSchedulers.mainThread())
                                        .subscribe(
                                                bytes -> onWriteSuccess(),
                                                CharacteristicOperationExampleActivity.this::onWriteFailure
                                        );
                            }
                        }
                    }
                });

            }
        }, 200, 300);

private void onWriteSuccess() {
    //noinspection ConstantConditions
    Snackbar.make(findViewById(R.id.main), "Write success", Snackbar.LENGTH_SHORT).show();
    envia = true;
    }
Nega developer
  • 259
  • 3
  • 8
  • 19
  • Do you start the writes right after the connection is established? – Dariusz Seweryn Oct 24 '17 at 07:17
  • Yes, I do it on this way. I connect, and after that I press a button to perform a multiple writes. – Nega developer Oct 24 '17 at 07:19
  • As a data point, I've queued up ~120 operations at once before, and not noticed any slowdown while processing them (Pixel XL on Nougat). Is the connection being re-established every time you write here? I'm not super familiar with the RxAndroidBle but I believe it doesn't persist connections? – stkent Oct 24 '17 at 07:28
  • No, is not re-established every time. The connection is persistent. I am currently working with Samsung galaxy s6 edge plus running with Android 6.0.1 – Nega developer Oct 24 '17 at 07:39

1 Answers1

0

The speed of BLE is determined by the Connection Interval*.

The Android OS usually establishes the connection using some quite aggressive settings to spend as small amount of time connecting (& discovering services) as possible.

What I have read and observed is that Android OS negotiates a more relaxed Connection Interval several seconds after the connection was established to preserve energy. This may be the reason why you are observing a slowdown.

What you could try to change the behaviour is to call RxBleConnection.requestConnectionPriority() which triggers Android to negotiate different Connection Interval. If the Android OS and the peripheral will agree on newly negotiated parameters the connection speed should change.
Unfortunately enough there is no callback from on the BluetoothGattCallback that confirms when exactly (or even if) the negotiation has succeeded.

*Connection Interval—BLE connection is optimised for Low Energy consumption and the actual transmission takes place in time windows. These windows are happening periodically with period equal to Connection Interval so the shorter the Connection Interval is the more transmission windows are available during a specific time frame—more transfers can be made.

Dariusz Seweryn
  • 3,212
  • 2
  • 14
  • 21