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;
}