use of com.eveningoutpost.dexdrip.watch.thinjam.Const.THINJAM_WRITE in project xDrip by NightscoutFoundation.
the class BlueJayService method enableNotifications.
private void enableNotifications() {
UserError.Log.d(TAG, "enableNotifications called()");
if (I.isNotificationEnabled) {
UserError.Log.d(TAG, "Notifications already enabled");
changeNextState();
return;
}
if (notificationSubscription != null) {
notificationSubscription.unsubscribe();
}
JoH.threadSleep(500);
UserError.Log.d(TAG, "Requesting to enable notifications");
if (I.connection == null) {
UserError.Log.d(TAG, "Connection went away before we could enable notifications");
return;
}
notificationSubscription = new Subscription(I.connection.setupNotification(THINJAM_WRITE).observeOn(// needed?
Schedulers.newThread()).doOnNext(notificationObservable -> {
UserError.Log.d(TAG, "Notifications enabled");
I.connection.writeCharacteristic(THINJAM_BULK, new byte[] { 0x55 });
// Debug sleep to make sure notifications are actually enabled???
JoH.threadSleep(500);
I.isNotificationEnabled = true;
changeNextState();
}).flatMap(notificationObservable -> notificationObservable).observeOn(Schedulers.newThread()).subscribe(bytes -> {
// incoming notifications
UserError.Log.d(TAG, "Received notification bytes: " + JoH.bytesToHex(bytes));
val pushRx = PushRx.parse(bytes);
if (pushRx != null) {
UserError.Log.d(TAG, "Received PushRX: " + pushRx.toS());
getInfo().processPushRx(pushRx);
processPushRxActions(pushRx);
} else if (bytes[0] == 0x06) {
// TODO move this to parsed response
final int replyParam = ((int) bytes[1]) & 0xff;
if (replyParam == 0) {
UserError.Log.d(TAG, "Bulk up success reply marker received! - removing queue head item");
// removes first item from the queue which should be the one we just processed!
commandQueue.poll();
UserError.Log.d(TAG, "Scheduling immediate run of queue");
// remove timeout retry task
Inevitable.kill("tj-next-queue");
Inevitable.task("tj-next-queue", 0, this::processQueue);
} else {
revisedOffset = replyParam;
// race condition on display
UserError.Log.d(TAG, "Bulk up failure at: " + revisedOffset);
}
} else if (bytes[0] == (byte) 0xFE) {
audioStreamCallBack(bytes);
} else {
if (ThinJamActivity.isD()) {
notificationString.append(new String(bytes));
Inevitable.task("tj update notifi", 250, new Runnable() {
@Override
public void run() {
stringObservableField.set(notificationString.toString());
}
});
}
}
}, throwable -> {
UserError.Log.d(TAG, "Throwable in Record Notification: " + throwable);
I.isNotificationEnabled = false;
if (throwable instanceof BleCharacteristicNotFoundException) {
// maybe legacy - ignore for now but needs better handling
UserError.Log.d(TAG, "Characteristic not found for notification");
debug.processTestSuite("logcharerror");
tryGattRefresh(getI().connection);
}
if (throwable instanceof BleCannotSetCharacteristicNotificationException) {
UserError.Log.e(TAG, "Problems setting notifications - disconnecting");
changeState(CLOSE);
}
if (throwable instanceof BleDisconnectedException) {
UserError.Log.d(TAG, "Disconnected while enabling notifications");
changeState(CLOSE);
}
}));
}
use of com.eveningoutpost.dexdrip.watch.thinjam.Const.THINJAM_WRITE in project xDrip-plus by jamorham.
the class BlueJayService method enableNotifications.
private void enableNotifications() {
UserError.Log.d(TAG, "enableNotifications called()");
if (I.isNotificationEnabled) {
UserError.Log.d(TAG, "Notifications already enabled");
changeNextState();
return;
}
if (notificationSubscription != null) {
notificationSubscription.unsubscribe();
}
JoH.threadSleep(500);
UserError.Log.d(TAG, "Requesting to enable notifications");
if (I.connection == null) {
UserError.Log.d(TAG, "Connection went away before we could enable notifications");
return;
}
notificationSubscription = new Subscription(I.connection.setupNotification(THINJAM_WRITE).observeOn(// needed?
Schedulers.newThread()).doOnNext(notificationObservable -> {
UserError.Log.d(TAG, "Notifications enabled");
I.connection.writeCharacteristic(THINJAM_BULK, new byte[] { 0x55 });
// Debug sleep to make sure notifications are actually enabled???
JoH.threadSleep(500);
I.isNotificationEnabled = true;
changeNextState();
}).flatMap(notificationObservable -> notificationObservable).observeOn(Schedulers.newThread()).subscribe(bytes -> {
// incoming notifications
UserError.Log.d(TAG, "Received notification bytes: " + JoH.bytesToHex(bytes));
val pushRx = PushRx.parse(bytes);
if (pushRx != null) {
UserError.Log.d(TAG, "Received PushRX: " + pushRx.toS());
getInfo().processPushRx(pushRx);
processPushRxActions(pushRx);
} else if (bytes[0] == 0x06) {
// TODO move this to parsed response
final int replyParam = ((int) bytes[1]) & 0xff;
if (replyParam == 0) {
UserError.Log.d(TAG, "Bulk up success reply marker received! - removing queue head item");
// removes first item from the queue which should be the one we just processed!
commandQueue.poll();
UserError.Log.d(TAG, "Scheduling immediate run of queue");
// remove timeout retry task
Inevitable.kill("tj-next-queue");
Inevitable.task("tj-next-queue", 0, this::processQueue);
} else {
revisedOffset = replyParam;
// race condition on display
UserError.Log.d(TAG, "Bulk up failure at: " + revisedOffset);
}
} else if (bytes[0] == (byte) 0xFE) {
audioStreamCallBack(bytes);
} else {
if (ThinJamActivity.isD()) {
notificationString.append(new String(bytes));
Inevitable.task("tj update notifi", 250, new Runnable() {
@Override
public void run() {
stringObservableField.set(notificationString.toString());
}
});
}
}
}, throwable -> {
UserError.Log.d(TAG, "Throwable in Record Notification: " + throwable);
I.isNotificationEnabled = false;
if (throwable instanceof BleCharacteristicNotFoundException) {
// maybe legacy - ignore for now but needs better handling
UserError.Log.d(TAG, "Characteristic not found for notification");
debug.processTestSuite("logcharerror");
tryGattRefresh(getI().connection);
}
if (throwable instanceof BleCannotSetCharacteristicNotificationException) {
UserError.Log.e(TAG, "Problems setting notifications - disconnecting");
changeState(CLOSE);
}
if (throwable instanceof BleDisconnectedException) {
UserError.Log.d(TAG, "Disconnected while enabling notifications");
changeState(CLOSE);
}
}));
}
Aggregations