Search in sources :

Example 1 with JoH.bytesToHex

use of com.eveningoutpost.dexdrip.Models.JoH.bytesToHex in project xDrip by NightscoutFoundation.

the class MiBandService method ProcessAuthCommands.

@SuppressLint("CheckResult")
private void ProcessAuthCommands(RxBleConnection connection, byte[] value) {
    if (value[0] == AUTH_RESPONSE && value[1] == AUTH_SEND_KEY && (value[2] & 0x0f) == AUTH_SUCCESS) {
        // get random key from band
        connection.writeCharacteristic(authorisation.getCharacteristicUUID(), authorisation.getAuthKeyRequest()).subscribe(val -> {
            if (d)
                UserError.Log.d(TAG, "Wrote OPCODE_AUTH_REQ1: " + JoH.bytesToHex(val));
        }, throwable -> {
            UserError.Log.e(TAG, "Could not write OPCODE_AUTH_REQ1: " + throwable);
        });
    } else if (value[0] == AUTH_RESPONSE && (value[1] & 0x0f) == AUTH_REQUEST_RANDOM_AUTH_NUMBER && value[2] == AUTH_SUCCESS) {
        byte[] tmpValue = Arrays.copyOfRange(value, 3, 19);
        try {
            byte[] authReply = authorisation.calculateAuthReply(tmpValue);
            // get random key from band
            connection.writeCharacteristic(authorisation.getCharacteristicUUID(), authReply).subscribe(val -> {
                if (d)
                    UserError.Log.d(TAG, "Wrote OPCODE_AUTH_REQ2: " + JoH.bytesToHex(val));
            }, throwable -> {
                UserError.Log.e(TAG, "Could not write OPCODE_AUTH_REQ2: " + throwable);
            });
        } catch (Exception e) {
            JoH.static_toast_long(e.getMessage());
            UserError.Log.e(TAG, (e.getMessage()));
            changeState(AUTHORIZE_FAILED);
        }
    } else if (value[0] == AUTH_RESPONSE && (value[1] & 0x0f) == AUTH_SEND_ENCRYPTED_AUTH_NUMBER && value[2] == AUTH_SUCCESS) {
        isNeedToAuthenticate = false;
        if (MiBand.getPersistentAuthMac().isEmpty()) {
            MiBand.setPersistentAuthMac(MiBand.getMac());
            MiBand.setPersistentAuthKey(JoH.bytesToHex(authorisation.getLocalKey()), MiBand.getPersistentAuthMac());
            String msg = "MiBand was successfully authenticated";
            JoH.static_toast_long(msg);
            UserError.Log.e(TAG, msg);
        }
        if (authSubscription != null) {
            authSubscription.unsubscribe();
        }
        changeNextState();
    } else if (value[0] == AUTH_RESPONSE && (((value[2] & 0x0f) == AUTH_FAIL) || (value[2] == AUTH_MIBAND4_FAIL) || (value[2] == AUTH_MIBAND4_CODE_FAIL))) {
        MiBand.setPersistentAuthKey("", MiBand.getPersistentAuthMac());
        if (authSubscription != null) {
            authSubscription.unsubscribe();
        }
        String msg = "Cannot authorize miband, please recheck Auth code";
        JoH.static_toast_long(msg);
        UserError.Log.e(TAG, msg);
        changeState(AUTHORIZE_FAILED);
    }
}
Also used : AUTHORIZE_FAILED(com.eveningoutpost.dexdrip.watch.miband.MiBandService.MiBandState.AUTHORIZE_FAILED) AUTH_REQUEST_RANDOM_AUTH_NUMBER(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_REQUEST_RANDOM_AUTH_NUMBER) AuthMessages(com.eveningoutpost.dexdrip.watch.miband.message.AuthMessages) Arrays(java.util.Arrays) com.eveningoutpost.dexdrip.xdrip(com.eveningoutpost.dexdrip.xdrip) Date(java.util.Date) TimeoutException(java.util.concurrent.TimeoutException) PendingIntent(android.app.PendingIntent) CLOSED(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.CLOSED) INIT(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.INIT) Inevitable(com.eveningoutpost.dexdrip.UtilityModels.Inevitable) BleCannotSetCharacteristicNotificationException(com.polidea.rxandroidble2.exceptions.BleCannotSetCharacteristicNotificationException) PowerManager(android.os.PowerManager) HeartRate(com.eveningoutpost.dexdrip.Models.HeartRate) StatusItem(com.eveningoutpost.dexdrip.UtilityModels.StatusItem) BleCharacteristicNotFoundException(com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException) COMMAND_ACK_FIND_PHONE_IN_PROGRESS(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.COMMAND_ACK_FIND_PHONE_IN_PROGRESS) AUTH_FAIL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_FAIL) Schedulers(io.reactivex.schedulers.Schedulers) AUTH_MIBAND4_CODE_FAIL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_MIBAND4_CODE_FAIL) AUTH_RESPONSE(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_RESPONSE) AUTH_SEND_KEY(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_SEND_KEY) ActiveBgAlert(com.eveningoutpost.dexdrip.Models.ActiveBgAlert) FeaturesControllMessage(com.eveningoutpost.dexdrip.watch.miband.message.FeaturesControllMessage) MIBAND_NOTIFY_TYPE_ALARM(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_ALARM) CLOSE(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.CLOSE) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) PrefBindingFactory(com.eveningoutpost.dexdrip.watch.PrefBindingFactory) UUID(java.util.UUID) JoH.getResourceURI(com.eveningoutpost.dexdrip.Models.JoH.getResourceURI) UNKNOWN(com.eveningoutpost.dexdrip.watch.miband.MiBand.MiBandType.UNKNOWN) JoH(com.eveningoutpost.dexdrip.Models.JoH) List(java.util.List) BleDisconnectedException(com.polidea.rxandroidble2.exceptions.BleDisconnectedException) JoH.bytesToHex(com.eveningoutpost.dexdrip.Models.JoH.bytesToHex) MIBAND_NOTIFY_TYPE_CANCEL(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_CANCEL) R(com.eveningoutpost.dexdrip.R) Subscription(com.eveningoutpost.dexdrip.utils.bt.Subscription) PREFERRED_MTU_SIZE(com.eveningoutpost.dexdrip.watch.miband.Const.PREFERRED_MTU_SIZE) AlertLevelMessage(com.eveningoutpost.dexdrip.watch.miband.message.AlertLevelMessage) JoH.msTill(com.eveningoutpost.dexdrip.Models.JoH.msTill) AUTH_SUCCESS(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_SUCCESS) Getter(lombok.Getter) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic) WakeLockTrampoline(com.eveningoutpost.dexdrip.utils.framework.WakeLockTrampoline) Constants(com.eveningoutpost.dexdrip.UtilityModels.Constants) AUTH_SEND_ENCRYPTED_AUTH_NUMBER(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_SEND_ENCRYPTED_AUTH_NUMBER) Pair(android.util.Pair) FirmwareOperations(com.eveningoutpost.dexdrip.watch.miband.Firmware.FirmwareOperations) DisplayControllMessageMiband3_4(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiband3_4) RxBleConnection(com.polidea.rxandroidble2.RxBleConnection) Intent(android.content.Intent) MediaPlayer(android.media.MediaPlayer) ArrayList(java.util.ArrayList) JoH.niceTimeScalar(com.eveningoutpost.dexdrip.Models.JoH.niceTimeScalar) SuppressLint(android.annotation.SuppressLint) Calendar(java.util.Calendar) DeviceEvent(com.eveningoutpost.dexdrip.watch.miband.message.DeviceEvent) DisplayControllMessageMiBand2(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiBand2) DisplayControllMessage(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessage) COMMAND_DISABLE_CALL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.COMMAND_DISABLE_CALL) JamBaseBluetoothSequencer(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer) AlertMessage(com.eveningoutpost.dexdrip.watch.miband.message.AlertMessage) MI_BAND4(com.eveningoutpost.dexdrip.watch.miband.MiBand.MiBandType.MI_BAND4) Sheduled(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiband3_4.NightMode.Sheduled) RxBleDeviceServices(com.polidea.rxandroidble2.RxBleDeviceServices) MI_BAND2(com.eveningoutpost.dexdrip.watch.miband.MiBand.MiBandType.MI_BAND2) JoH.emptyString(com.eveningoutpost.dexdrip.Models.JoH.emptyString) SLEEP(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.SLEEP) BluetoothGattService(android.bluetooth.BluetoothGattService) com.eveningoutpost.dexdrip.xdrip.gs(com.eveningoutpost.dexdrip.xdrip.gs) AUTH_MIBAND4_FAIL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_MIBAND4_FAIL) WatchFaceGenerator(com.eveningoutpost.dexdrip.watch.miband.Firmware.WatchFaceGenerator) AlertPlayer(com.eveningoutpost.dexdrip.UtilityModels.AlertPlayer) PoorMansConcurrentLinkedDeque(com.eveningoutpost.dexdrip.utils.framework.PoorMansConcurrentLinkedDeque) TimeUnit(java.util.concurrent.TimeUnit) MIBAND_NOTIFY_TYPE_MESSAGE(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_MESSAGE) OperationCodes(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes) UserError(com.eveningoutpost.dexdrip.Models.UserError) MIBAND_NOTIFY_TYPE_CALL(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_CALL) JoH.emptyString(com.eveningoutpost.dexdrip.Models.JoH.emptyString) TimeoutException(java.util.concurrent.TimeoutException) BleCannotSetCharacteristicNotificationException(com.polidea.rxandroidble2.exceptions.BleCannotSetCharacteristicNotificationException) BleCharacteristicNotFoundException(com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException) BleDisconnectedException(com.polidea.rxandroidble2.exceptions.BleDisconnectedException) SuppressLint(android.annotation.SuppressLint)

Example 2 with JoH.bytesToHex

use of com.eveningoutpost.dexdrip.Models.JoH.bytesToHex in project xDrip-plus by jamorham.

the class MiBandService method processFirmwareCommands.

@SuppressLint("CheckResult")
private void processFirmwareCommands(byte[] value, boolean isSeqCommand) {
    RxBleConnection connection = I.connection;
    FirmwareOperations.SequenceType seq = firmware.getSequence();
    if (d)
        UserError.Log.d(TAG, "processFirmwareCommands: " + bytesToHex(value) + ": seq:" + seq.toString());
    if (isSeqCommand) {
        switch(seq) {
            case SET_NIGHTMODE:
                {
                    if (true) {
                        isNeedToRestoreNightMode = true;
                        DisplayControllMessageMiband3_4 dispControl = new DisplayControllMessageMiband3_4();
                        Calendar sheduledCalendar = Calendar.getInstance();
                        sheduledCalendar.set(Calendar.HOUR_OF_DAY, 0);
                        sheduledCalendar.set(Calendar.MINUTE, 0);
                        Date sheduledDate = sheduledCalendar.getTime();
                        connection.writeCharacteristic(dispControl.getCharacteristicUUID(), dispControl.setNightModeCmd(Sheduled, sheduledDate, sheduledDate)).subscribe(valB -> {
                            UserError.Log.d(TAG, "Wrote nigntmode, got: " + JoH.bytesToHex(valB));
                            firmware.nextSequence();
                            processFirmwareCommands(null, true);
                        }, throwable -> {
                            UserError.Log.e(TAG, "Could not write nigntmode: " + throwable);
                            firmware.nextSequence();
                            processFirmwareCommands(null, true);
                        });
                    } else {
                        firmware.nextSequence();
                        processFirmwareCommands(null, true);
                    }
                    break;
                }
            case PREPARE_UPLOAD:
                {
                    connection.writeCharacteristic(firmware.getFirmwareCharacteristicUUID(), firmware.prepareFWUploadInitCommand()).subscribe(valB -> {
                        UserError.Log.d(TAG, "Wrote prepareFWUploadInitCommand, got: " + JoH.bytesToHex(valB));
                    }, throwable -> {
                        UserError.Log.e(TAG, "Could not write prepareFWUploadInitCommand: " + throwable);
                        resetFirmwareState(false);
                    });
                    firmware.nextSequence();
                    break;
                }
        }
        return;
    } else {
        if (value.length != 3 && value.length != 11) {
            UserError.Log.e(TAG, "Notifications should be 3 or 11 bytes long.");
            return;
        }
        boolean success = value[2] == OperationCodes.SUCCESS;
        if (value[0] == OperationCodes.RESPONSE && success) {
            try {
                switch(value[1]) {
                    case OperationCodes.COMMAND_FIRMWARE_INIT:
                        {
                            if (seq == FirmwareOperations.SequenceType.TRANSFER_FW_START) {
                                connection.writeCharacteristic(firmware.getFirmwareCharacteristicUUID(), firmware.getFirmwareStartCommand()).subscribe(valB -> {
                                    UserError.Log.d(TAG, "Wrote Start command, got: " + JoH.bytesToHex(valB));
                                }, throwable -> {
                                    UserError.Log.e(TAG, "Could not write Start command: " + throwable);
                                    resetFirmwareState(false);
                                });
                                firmware.nextSequence();
                            } else if (seq == FirmwareOperations.SequenceType.TRANSFER_SEND_WF_INFO) {
                                connection.writeCharacteristic(firmware.getFirmwareCharacteristicUUID(), firmware.sendFwInfo()).subscribe(valB -> {
                                    UserError.Log.d(TAG, "Wrote sendFwInfo, got: " + JoH.bytesToHex(valB));
                                }, throwable -> {
                                    UserError.Log.e(TAG, "Could not write firmware info: " + throwable);
                                    resetFirmwareState(false);
                                });
                                firmware.nextSequence();
                                break;
                            }
                            break;
                        }
                    case OperationCodes.COMMAND_FIRMWARE_START_DATA:
                        {
                            sendFirmwareData();
                            break;
                        }
                    case OperationCodes.COMMAND_FIRMWARE_CHECKSUM:
                        {
                            firmware.nextSequence();
                            if (firmware.getFirmwareType() == FirmwareOperations.FirmwareType.FIRMWARE) {
                            // send reboot
                            } else {
                                UserError.Log.e(TAG, "Watch Face has been installed successfully");
                                resetFirmwareState(true);
                            }
                            break;
                        }
                    case OperationCodes.COMMAND_FIRMWARE_REBOOT:
                        {
                            UserError.Log.e(TAG, "Reboot command successfully sent.");
                            resetFirmwareState(true);
                            break;
                        }
                    default:
                        {
                            resetFirmwareState(false, "Unexpected response during firmware update");
                        }
                }
            } catch (Exception ex) {
                resetFirmwareState(false);
            }
        } else {
            String errorMessage = null;
            Boolean sendBGNotification = false;
            if (value[2] == OperationCodes.LOW_BATTERY_ERROR) {
                errorMessage = "Cannot upload watchface, low battery, please charge device";
                sendBGNotification = true;
            } else if (value[2] == OperationCodes.TIMER_RUNNING) {
                errorMessage = "Cannot upload watchface, timer running on band";
            } else if (value[2] == OperationCodes.ON_CALL) {
                errorMessage = "Cannot upload watchface, call in progress";
            } else {
                errorMessage = "Unexpected notification during firmware update:" + JoH.bytesToHex(value);
            }
            resetFirmwareState(false, errorMessage);
            if (sendBGNotification) {
                emptyQueue();
                JoH.startService(MiBandService.class, "function", "update_bg_as_notification");
                changeState(SLEEP);
            }
        }
    }
}
Also used : AUTHORIZE_FAILED(com.eveningoutpost.dexdrip.watch.miband.MiBandService.MiBandState.AUTHORIZE_FAILED) AUTH_REQUEST_RANDOM_AUTH_NUMBER(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_REQUEST_RANDOM_AUTH_NUMBER) AuthMessages(com.eveningoutpost.dexdrip.watch.miband.message.AuthMessages) Arrays(java.util.Arrays) com.eveningoutpost.dexdrip.xdrip(com.eveningoutpost.dexdrip.xdrip) Date(java.util.Date) TimeoutException(java.util.concurrent.TimeoutException) PendingIntent(android.app.PendingIntent) CLOSED(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.CLOSED) INIT(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.INIT) Inevitable(com.eveningoutpost.dexdrip.UtilityModels.Inevitable) BleCannotSetCharacteristicNotificationException(com.polidea.rxandroidble2.exceptions.BleCannotSetCharacteristicNotificationException) PowerManager(android.os.PowerManager) HeartRate(com.eveningoutpost.dexdrip.Models.HeartRate) StatusItem(com.eveningoutpost.dexdrip.UtilityModels.StatusItem) BleCharacteristicNotFoundException(com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException) COMMAND_ACK_FIND_PHONE_IN_PROGRESS(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.COMMAND_ACK_FIND_PHONE_IN_PROGRESS) AUTH_FAIL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_FAIL) Schedulers(io.reactivex.schedulers.Schedulers) AUTH_MIBAND4_CODE_FAIL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_MIBAND4_CODE_FAIL) AUTH_RESPONSE(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_RESPONSE) AUTH_SEND_KEY(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_SEND_KEY) ActiveBgAlert(com.eveningoutpost.dexdrip.Models.ActiveBgAlert) FeaturesControllMessage(com.eveningoutpost.dexdrip.watch.miband.message.FeaturesControllMessage) MIBAND_NOTIFY_TYPE_ALARM(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_ALARM) CLOSE(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.CLOSE) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) PrefBindingFactory(com.eveningoutpost.dexdrip.watch.PrefBindingFactory) UUID(java.util.UUID) JoH.getResourceURI(com.eveningoutpost.dexdrip.Models.JoH.getResourceURI) UNKNOWN(com.eveningoutpost.dexdrip.watch.miband.MiBand.MiBandType.UNKNOWN) JoH(com.eveningoutpost.dexdrip.Models.JoH) List(java.util.List) BleDisconnectedException(com.polidea.rxandroidble2.exceptions.BleDisconnectedException) JoH.bytesToHex(com.eveningoutpost.dexdrip.Models.JoH.bytesToHex) MIBAND_NOTIFY_TYPE_CANCEL(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_CANCEL) R(com.eveningoutpost.dexdrip.R) Subscription(com.eveningoutpost.dexdrip.utils.bt.Subscription) PREFERRED_MTU_SIZE(com.eveningoutpost.dexdrip.watch.miband.Const.PREFERRED_MTU_SIZE) AlertLevelMessage(com.eveningoutpost.dexdrip.watch.miband.message.AlertLevelMessage) JoH.msTill(com.eveningoutpost.dexdrip.Models.JoH.msTill) AUTH_SUCCESS(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_SUCCESS) Getter(lombok.Getter) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic) WakeLockTrampoline(com.eveningoutpost.dexdrip.utils.framework.WakeLockTrampoline) Constants(com.eveningoutpost.dexdrip.UtilityModels.Constants) AUTH_SEND_ENCRYPTED_AUTH_NUMBER(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_SEND_ENCRYPTED_AUTH_NUMBER) Pair(android.util.Pair) FirmwareOperations(com.eveningoutpost.dexdrip.watch.miband.Firmware.FirmwareOperations) DisplayControllMessageMiband3_4(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiband3_4) RxBleConnection(com.polidea.rxandroidble2.RxBleConnection) Intent(android.content.Intent) MediaPlayer(android.media.MediaPlayer) ArrayList(java.util.ArrayList) JoH.niceTimeScalar(com.eveningoutpost.dexdrip.Models.JoH.niceTimeScalar) SuppressLint(android.annotation.SuppressLint) Calendar(java.util.Calendar) DeviceEvent(com.eveningoutpost.dexdrip.watch.miband.message.DeviceEvent) DisplayControllMessageMiBand2(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiBand2) DisplayControllMessage(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessage) COMMAND_DISABLE_CALL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.COMMAND_DISABLE_CALL) JamBaseBluetoothSequencer(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer) AlertMessage(com.eveningoutpost.dexdrip.watch.miband.message.AlertMessage) MI_BAND4(com.eveningoutpost.dexdrip.watch.miband.MiBand.MiBandType.MI_BAND4) Sheduled(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiband3_4.NightMode.Sheduled) RxBleDeviceServices(com.polidea.rxandroidble2.RxBleDeviceServices) MI_BAND2(com.eveningoutpost.dexdrip.watch.miband.MiBand.MiBandType.MI_BAND2) JoH.emptyString(com.eveningoutpost.dexdrip.Models.JoH.emptyString) SLEEP(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.SLEEP) BluetoothGattService(android.bluetooth.BluetoothGattService) com.eveningoutpost.dexdrip.xdrip.gs(com.eveningoutpost.dexdrip.xdrip.gs) AUTH_MIBAND4_FAIL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_MIBAND4_FAIL) WatchFaceGenerator(com.eveningoutpost.dexdrip.watch.miband.Firmware.WatchFaceGenerator) AlertPlayer(com.eveningoutpost.dexdrip.UtilityModels.AlertPlayer) PoorMansConcurrentLinkedDeque(com.eveningoutpost.dexdrip.utils.framework.PoorMansConcurrentLinkedDeque) TimeUnit(java.util.concurrent.TimeUnit) MIBAND_NOTIFY_TYPE_MESSAGE(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_MESSAGE) OperationCodes(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes) UserError(com.eveningoutpost.dexdrip.Models.UserError) MIBAND_NOTIFY_TYPE_CALL(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_CALL) FirmwareOperations(com.eveningoutpost.dexdrip.watch.miband.Firmware.FirmwareOperations) DisplayControllMessageMiband3_4(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiband3_4) Calendar(java.util.Calendar) RxBleConnection(com.polidea.rxandroidble2.RxBleConnection) JoH.emptyString(com.eveningoutpost.dexdrip.Models.JoH.emptyString) Date(java.util.Date) TimeoutException(java.util.concurrent.TimeoutException) BleCannotSetCharacteristicNotificationException(com.polidea.rxandroidble2.exceptions.BleCannotSetCharacteristicNotificationException) BleCharacteristicNotFoundException(com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException) BleDisconnectedException(com.polidea.rxandroidble2.exceptions.BleDisconnectedException) SuppressLint(android.annotation.SuppressLint)

Example 3 with JoH.bytesToHex

use of com.eveningoutpost.dexdrip.Models.JoH.bytesToHex in project xDrip-plus by jamorham.

the class MiBandService method ProcessAuthCommands.

@SuppressLint("CheckResult")
private void ProcessAuthCommands(RxBleConnection connection, byte[] value) {
    if (value[0] == AUTH_RESPONSE && value[1] == AUTH_SEND_KEY && (value[2] & 0x0f) == AUTH_SUCCESS) {
        // get random key from band
        connection.writeCharacteristic(authorisation.getCharacteristicUUID(), authorisation.getAuthKeyRequest()).subscribe(val -> {
            if (d)
                UserError.Log.d(TAG, "Wrote OPCODE_AUTH_REQ1: " + JoH.bytesToHex(val));
        }, throwable -> {
            UserError.Log.e(TAG, "Could not write OPCODE_AUTH_REQ1: " + throwable);
        });
    } else if (value[0] == AUTH_RESPONSE && (value[1] & 0x0f) == AUTH_REQUEST_RANDOM_AUTH_NUMBER && value[2] == AUTH_SUCCESS) {
        byte[] tmpValue = Arrays.copyOfRange(value, 3, 19);
        try {
            byte[] authReply = authorisation.calculateAuthReply(tmpValue);
            // get random key from band
            connection.writeCharacteristic(authorisation.getCharacteristicUUID(), authReply).subscribe(val -> {
                if (d)
                    UserError.Log.d(TAG, "Wrote OPCODE_AUTH_REQ2: " + JoH.bytesToHex(val));
            }, throwable -> {
                UserError.Log.e(TAG, "Could not write OPCODE_AUTH_REQ2: " + throwable);
            });
        } catch (Exception e) {
            JoH.static_toast_long(e.getMessage());
            UserError.Log.e(TAG, (e.getMessage()));
            changeState(AUTHORIZE_FAILED);
        }
    } else if (value[0] == AUTH_RESPONSE && (value[1] & 0x0f) == AUTH_SEND_ENCRYPTED_AUTH_NUMBER && value[2] == AUTH_SUCCESS) {
        isNeedToAuthenticate = false;
        if (MiBand.getPersistentAuthMac().isEmpty()) {
            MiBand.setPersistentAuthMac(MiBand.getMac());
            MiBand.setPersistentAuthKey(JoH.bytesToHex(authorisation.getLocalKey()), MiBand.getPersistentAuthMac());
            String msg = "MiBand was successfully authenticated";
            JoH.static_toast_long(msg);
            UserError.Log.e(TAG, msg);
        }
        if (authSubscription != null) {
            authSubscription.unsubscribe();
        }
        changeNextState();
    } else if (value[0] == AUTH_RESPONSE && (((value[2] & 0x0f) == AUTH_FAIL) || (value[2] == AUTH_MIBAND4_FAIL) || (value[2] == AUTH_MIBAND4_CODE_FAIL))) {
        MiBand.setPersistentAuthKey("", MiBand.getPersistentAuthMac());
        if (authSubscription != null) {
            authSubscription.unsubscribe();
        }
        String msg = "Cannot authorize miband, please recheck Auth code";
        JoH.static_toast_long(msg);
        UserError.Log.e(TAG, msg);
        changeState(AUTHORIZE_FAILED);
    }
}
Also used : AUTHORIZE_FAILED(com.eveningoutpost.dexdrip.watch.miband.MiBandService.MiBandState.AUTHORIZE_FAILED) AUTH_REQUEST_RANDOM_AUTH_NUMBER(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_REQUEST_RANDOM_AUTH_NUMBER) AuthMessages(com.eveningoutpost.dexdrip.watch.miband.message.AuthMessages) Arrays(java.util.Arrays) com.eveningoutpost.dexdrip.xdrip(com.eveningoutpost.dexdrip.xdrip) Date(java.util.Date) TimeoutException(java.util.concurrent.TimeoutException) PendingIntent(android.app.PendingIntent) CLOSED(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.CLOSED) INIT(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.INIT) Inevitable(com.eveningoutpost.dexdrip.UtilityModels.Inevitable) BleCannotSetCharacteristicNotificationException(com.polidea.rxandroidble2.exceptions.BleCannotSetCharacteristicNotificationException) PowerManager(android.os.PowerManager) HeartRate(com.eveningoutpost.dexdrip.Models.HeartRate) StatusItem(com.eveningoutpost.dexdrip.UtilityModels.StatusItem) BleCharacteristicNotFoundException(com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException) COMMAND_ACK_FIND_PHONE_IN_PROGRESS(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.COMMAND_ACK_FIND_PHONE_IN_PROGRESS) AUTH_FAIL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_FAIL) Schedulers(io.reactivex.schedulers.Schedulers) AUTH_MIBAND4_CODE_FAIL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_MIBAND4_CODE_FAIL) AUTH_RESPONSE(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_RESPONSE) AUTH_SEND_KEY(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_SEND_KEY) ActiveBgAlert(com.eveningoutpost.dexdrip.Models.ActiveBgAlert) FeaturesControllMessage(com.eveningoutpost.dexdrip.watch.miband.message.FeaturesControllMessage) MIBAND_NOTIFY_TYPE_ALARM(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_ALARM) CLOSE(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.CLOSE) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) PrefBindingFactory(com.eveningoutpost.dexdrip.watch.PrefBindingFactory) UUID(java.util.UUID) JoH.getResourceURI(com.eveningoutpost.dexdrip.Models.JoH.getResourceURI) UNKNOWN(com.eveningoutpost.dexdrip.watch.miband.MiBand.MiBandType.UNKNOWN) JoH(com.eveningoutpost.dexdrip.Models.JoH) List(java.util.List) BleDisconnectedException(com.polidea.rxandroidble2.exceptions.BleDisconnectedException) JoH.bytesToHex(com.eveningoutpost.dexdrip.Models.JoH.bytesToHex) MIBAND_NOTIFY_TYPE_CANCEL(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_CANCEL) R(com.eveningoutpost.dexdrip.R) Subscription(com.eveningoutpost.dexdrip.utils.bt.Subscription) PREFERRED_MTU_SIZE(com.eveningoutpost.dexdrip.watch.miband.Const.PREFERRED_MTU_SIZE) AlertLevelMessage(com.eveningoutpost.dexdrip.watch.miband.message.AlertLevelMessage) JoH.msTill(com.eveningoutpost.dexdrip.Models.JoH.msTill) AUTH_SUCCESS(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_SUCCESS) Getter(lombok.Getter) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic) WakeLockTrampoline(com.eveningoutpost.dexdrip.utils.framework.WakeLockTrampoline) Constants(com.eveningoutpost.dexdrip.UtilityModels.Constants) AUTH_SEND_ENCRYPTED_AUTH_NUMBER(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_SEND_ENCRYPTED_AUTH_NUMBER) Pair(android.util.Pair) FirmwareOperations(com.eveningoutpost.dexdrip.watch.miband.Firmware.FirmwareOperations) DisplayControllMessageMiband3_4(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiband3_4) RxBleConnection(com.polidea.rxandroidble2.RxBleConnection) Intent(android.content.Intent) MediaPlayer(android.media.MediaPlayer) ArrayList(java.util.ArrayList) JoH.niceTimeScalar(com.eveningoutpost.dexdrip.Models.JoH.niceTimeScalar) SuppressLint(android.annotation.SuppressLint) Calendar(java.util.Calendar) DeviceEvent(com.eveningoutpost.dexdrip.watch.miband.message.DeviceEvent) DisplayControllMessageMiBand2(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiBand2) DisplayControllMessage(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessage) COMMAND_DISABLE_CALL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.COMMAND_DISABLE_CALL) JamBaseBluetoothSequencer(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer) AlertMessage(com.eveningoutpost.dexdrip.watch.miband.message.AlertMessage) MI_BAND4(com.eveningoutpost.dexdrip.watch.miband.MiBand.MiBandType.MI_BAND4) Sheduled(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiband3_4.NightMode.Sheduled) RxBleDeviceServices(com.polidea.rxandroidble2.RxBleDeviceServices) MI_BAND2(com.eveningoutpost.dexdrip.watch.miband.MiBand.MiBandType.MI_BAND2) JoH.emptyString(com.eveningoutpost.dexdrip.Models.JoH.emptyString) SLEEP(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.SLEEP) BluetoothGattService(android.bluetooth.BluetoothGattService) com.eveningoutpost.dexdrip.xdrip.gs(com.eveningoutpost.dexdrip.xdrip.gs) AUTH_MIBAND4_FAIL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_MIBAND4_FAIL) WatchFaceGenerator(com.eveningoutpost.dexdrip.watch.miband.Firmware.WatchFaceGenerator) AlertPlayer(com.eveningoutpost.dexdrip.UtilityModels.AlertPlayer) PoorMansConcurrentLinkedDeque(com.eveningoutpost.dexdrip.utils.framework.PoorMansConcurrentLinkedDeque) TimeUnit(java.util.concurrent.TimeUnit) MIBAND_NOTIFY_TYPE_MESSAGE(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_MESSAGE) OperationCodes(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes) UserError(com.eveningoutpost.dexdrip.Models.UserError) MIBAND_NOTIFY_TYPE_CALL(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_CALL) JoH.emptyString(com.eveningoutpost.dexdrip.Models.JoH.emptyString) TimeoutException(java.util.concurrent.TimeoutException) BleCannotSetCharacteristicNotificationException(com.polidea.rxandroidble2.exceptions.BleCannotSetCharacteristicNotificationException) BleCharacteristicNotFoundException(com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException) BleDisconnectedException(com.polidea.rxandroidble2.exceptions.BleDisconnectedException) SuppressLint(android.annotation.SuppressLint)

Example 4 with JoH.bytesToHex

use of com.eveningoutpost.dexdrip.Models.JoH.bytesToHex in project xDrip-plus by jamorham.

the class MiBandService method authPhase.

@SuppressLint("CheckResult")
private void authPhase() {
    extendWakeLock(30000);
    RxBleConnection connection = I.connection;
    if (d)
        UserError.Log.d(TAG, "Authorizing");
    if (I.connection == null) {
        if (d)
            UserError.Log.d(TAG, "Cannot enable as connection is null!");
        return;
    }
    String authKey = MiBand.getPersistentAuthKey();
    if (MiBand.getMibandType() == MI_BAND4) {
        if (authKey.isEmpty()) {
            authKey = MiBand.getAuthKey();
            if (authKey.isEmpty()) {
                authKey = AuthMessages.getAuthCodeFromFilesSystem(MiBand.getMac());
            }
            if (!AuthMessages.isValidAuthKey(authKey)) {
                JoH.static_toast_long("Wrong miband authorization key, please recheck a key and try to reconnect again");
                changeState(AUTHORIZE_FAILED);
                return;
            } else {
                MiBand.setAuthKey(authKey);
            }
        }
    }
    if (!AuthMessages.isValidAuthKey(authKey)) {
        authKey = "";
    }
    if (d)
        UserError.Log.d(TAG, "authKey: " + authKey);
    authorisation = new AuthMessages(MiBand.getMibandType(), authKey);
    if (d)
        UserError.Log.d(TAG, "localKey: " + JoH.bytesToHex(authorisation.getLocalKey()));
    authSubscription = new Subscription(connection.setupNotification(authorisation.getCharacteristicUUID()).timeout(20, // WARN
    TimeUnit.SECONDS).doOnNext(notificationObservable -> {
        if (d)
            UserError.Log.d(TAG, "Notification for auth enabled");
        if (MiBand.isAuthenticated()) {
            // get random key from band
            connection.writeCharacteristic(authorisation.getCharacteristicUUID(), authorisation.getAuthKeyRequest()).subscribe(val -> {
                if (d)
                    UserError.Log.d(TAG, "Wrote getAuthKeyRequest: " + JoH.bytesToHex(val));
            }, throwable -> {
                UserError.Log.e(TAG, "Could not getAuthKeyRequest: " + throwable);
            });
        } else {
            connection.writeCharacteristic(authorisation.getCharacteristicUUID(), authorisation.getAuthCommand()).subscribe(characteristicValue -> {
                UserError.Log.d(TAG, "Wrote getAuthCommand, got: " + JoH.bytesToHex(characteristicValue));
            }, throwable -> {
                UserError.Log.e(TAG, "Could not write getAuthCommand: " + throwable);
            });
        }
    }).flatMap(notificationObservable -> notificationObservable).subscribe(bytes -> {
        // incoming notifications
        if (d)
            UserError.Log.d(TAG, "Received auth notification bytes: " + bytesToHex(bytes));
        ProcessAuthCommands(connection, bytes);
    // changeNextState();
    }, throwable -> {
        UserError.Log.d(TAG, "Throwable in Record Notification: " + throwable);
        if (throwable instanceof BleCharacteristicNotFoundException) {
            // maybe legacy - ignore for now but needs better handling
            UserError.Log.d(TAG, "Characteristic not found for notification");
        } else if (throwable instanceof BleCannotSetCharacteristicNotificationException) {
            UserError.Log.e(TAG, "Problems setting notifications - disconnecting");
        } else if (throwable instanceof BleDisconnectedException) {
            UserError.Log.d(TAG, "Disconnected while enabling notifications");
        } else if (throwable instanceof TimeoutException) {
            // check if it is normal timeout
            if (!MiBand.isAuthenticated()) {
                String errorText = "MiBand authentication failed due to authentication timeout. When your Mi Band vibrates and blinks, tap it a few times in a row.";
                UserError.Log.d(TAG, errorText);
                JoH.static_toast_long(errorText);
            }
        }
        if (authSubscription != null) {
            authSubscription.unsubscribe();
        }
        changeState(CLOSE);
    }));
}
Also used : AuthMessages(com.eveningoutpost.dexdrip.watch.miband.message.AuthMessages) AUTHORIZE_FAILED(com.eveningoutpost.dexdrip.watch.miband.MiBandService.MiBandState.AUTHORIZE_FAILED) AUTH_REQUEST_RANDOM_AUTH_NUMBER(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_REQUEST_RANDOM_AUTH_NUMBER) AuthMessages(com.eveningoutpost.dexdrip.watch.miband.message.AuthMessages) Arrays(java.util.Arrays) com.eveningoutpost.dexdrip.xdrip(com.eveningoutpost.dexdrip.xdrip) Date(java.util.Date) TimeoutException(java.util.concurrent.TimeoutException) PendingIntent(android.app.PendingIntent) CLOSED(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.CLOSED) INIT(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.INIT) Inevitable(com.eveningoutpost.dexdrip.UtilityModels.Inevitable) BleCannotSetCharacteristicNotificationException(com.polidea.rxandroidble2.exceptions.BleCannotSetCharacteristicNotificationException) PowerManager(android.os.PowerManager) HeartRate(com.eveningoutpost.dexdrip.Models.HeartRate) StatusItem(com.eveningoutpost.dexdrip.UtilityModels.StatusItem) BleCharacteristicNotFoundException(com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException) COMMAND_ACK_FIND_PHONE_IN_PROGRESS(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.COMMAND_ACK_FIND_PHONE_IN_PROGRESS) AUTH_FAIL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_FAIL) Schedulers(io.reactivex.schedulers.Schedulers) AUTH_MIBAND4_CODE_FAIL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_MIBAND4_CODE_FAIL) AUTH_RESPONSE(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_RESPONSE) AUTH_SEND_KEY(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_SEND_KEY) ActiveBgAlert(com.eveningoutpost.dexdrip.Models.ActiveBgAlert) FeaturesControllMessage(com.eveningoutpost.dexdrip.watch.miband.message.FeaturesControllMessage) MIBAND_NOTIFY_TYPE_ALARM(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_ALARM) CLOSE(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.CLOSE) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) PrefBindingFactory(com.eveningoutpost.dexdrip.watch.PrefBindingFactory) UUID(java.util.UUID) JoH.getResourceURI(com.eveningoutpost.dexdrip.Models.JoH.getResourceURI) UNKNOWN(com.eveningoutpost.dexdrip.watch.miband.MiBand.MiBandType.UNKNOWN) JoH(com.eveningoutpost.dexdrip.Models.JoH) List(java.util.List) BleDisconnectedException(com.polidea.rxandroidble2.exceptions.BleDisconnectedException) JoH.bytesToHex(com.eveningoutpost.dexdrip.Models.JoH.bytesToHex) MIBAND_NOTIFY_TYPE_CANCEL(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_CANCEL) R(com.eveningoutpost.dexdrip.R) Subscription(com.eveningoutpost.dexdrip.utils.bt.Subscription) PREFERRED_MTU_SIZE(com.eveningoutpost.dexdrip.watch.miband.Const.PREFERRED_MTU_SIZE) AlertLevelMessage(com.eveningoutpost.dexdrip.watch.miband.message.AlertLevelMessage) JoH.msTill(com.eveningoutpost.dexdrip.Models.JoH.msTill) AUTH_SUCCESS(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_SUCCESS) Getter(lombok.Getter) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic) WakeLockTrampoline(com.eveningoutpost.dexdrip.utils.framework.WakeLockTrampoline) Constants(com.eveningoutpost.dexdrip.UtilityModels.Constants) AUTH_SEND_ENCRYPTED_AUTH_NUMBER(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_SEND_ENCRYPTED_AUTH_NUMBER) Pair(android.util.Pair) FirmwareOperations(com.eveningoutpost.dexdrip.watch.miband.Firmware.FirmwareOperations) DisplayControllMessageMiband3_4(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiband3_4) RxBleConnection(com.polidea.rxandroidble2.RxBleConnection) Intent(android.content.Intent) MediaPlayer(android.media.MediaPlayer) ArrayList(java.util.ArrayList) JoH.niceTimeScalar(com.eveningoutpost.dexdrip.Models.JoH.niceTimeScalar) SuppressLint(android.annotation.SuppressLint) Calendar(java.util.Calendar) DeviceEvent(com.eveningoutpost.dexdrip.watch.miband.message.DeviceEvent) DisplayControllMessageMiBand2(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiBand2) DisplayControllMessage(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessage) COMMAND_DISABLE_CALL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.COMMAND_DISABLE_CALL) JamBaseBluetoothSequencer(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer) AlertMessage(com.eveningoutpost.dexdrip.watch.miband.message.AlertMessage) MI_BAND4(com.eveningoutpost.dexdrip.watch.miband.MiBand.MiBandType.MI_BAND4) Sheduled(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiband3_4.NightMode.Sheduled) RxBleDeviceServices(com.polidea.rxandroidble2.RxBleDeviceServices) MI_BAND2(com.eveningoutpost.dexdrip.watch.miband.MiBand.MiBandType.MI_BAND2) JoH.emptyString(com.eveningoutpost.dexdrip.Models.JoH.emptyString) SLEEP(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.SLEEP) BluetoothGattService(android.bluetooth.BluetoothGattService) com.eveningoutpost.dexdrip.xdrip.gs(com.eveningoutpost.dexdrip.xdrip.gs) AUTH_MIBAND4_FAIL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_MIBAND4_FAIL) WatchFaceGenerator(com.eveningoutpost.dexdrip.watch.miband.Firmware.WatchFaceGenerator) AlertPlayer(com.eveningoutpost.dexdrip.UtilityModels.AlertPlayer) PoorMansConcurrentLinkedDeque(com.eveningoutpost.dexdrip.utils.framework.PoorMansConcurrentLinkedDeque) TimeUnit(java.util.concurrent.TimeUnit) MIBAND_NOTIFY_TYPE_MESSAGE(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_MESSAGE) OperationCodes(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes) UserError(com.eveningoutpost.dexdrip.Models.UserError) MIBAND_NOTIFY_TYPE_CALL(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_CALL) BleCannotSetCharacteristicNotificationException(com.polidea.rxandroidble2.exceptions.BleCannotSetCharacteristicNotificationException) BleCharacteristicNotFoundException(com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException) BleDisconnectedException(com.polidea.rxandroidble2.exceptions.BleDisconnectedException) RxBleConnection(com.polidea.rxandroidble2.RxBleConnection) JoH.emptyString(com.eveningoutpost.dexdrip.Models.JoH.emptyString) Subscription(com.eveningoutpost.dexdrip.utils.bt.Subscription) TimeoutException(java.util.concurrent.TimeoutException) SuppressLint(android.annotation.SuppressLint)

Example 5 with JoH.bytesToHex

use of com.eveningoutpost.dexdrip.Models.JoH.bytesToHex 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);
        }
    }));
}
Also used : Arrays(java.util.Arrays) CLOSED(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.CLOSED) Inevitable(com.eveningoutpost.dexdrip.UtilityModels.Inevitable) SET_TIME(com.eveningoutpost.dexdrip.watch.thinjam.BlueJayService.ThinJamState.SET_TIME) Pref(com.eveningoutpost.dexdrip.UtilityModels.Pref) OPCODE_BULK_R_XFER_0(com.eveningoutpost.dexdrip.watch.thinjam.Const.OPCODE_BULK_R_XFER_0) BleGattCharacteristicException(com.polidea.rxandroidble2.exceptions.BleGattCharacteristicException) BroadcastSnooze(com.eveningoutpost.dexdrip.UtilityModels.BroadcastSnooze) SetTimeTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.SetTimeTx) BaseTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.BaseTx) ERROR_INVALID(com.eveningoutpost.dexdrip.watch.thinjam.Const.ERROR_INVALID) OPCODE_IDENTIFY(com.eveningoutpost.dexdrip.watch.thinjam.Const.OPCODE_IDENTIFY) ReplyProcessor(com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor) ThinJamAudioStream(com.eveningoutpost.dexdrip.watch.thinjam.io.ThinJamAudioStream) FirmwareDownload(com.eveningoutpost.dexdrip.watch.thinjam.firmware.FirmwareDownload) OPCODE_SHOW_QRCODE(com.eveningoutpost.dexdrip.watch.thinjam.Const.OPCODE_SHOW_QRCODE) OPCODE_RESET_ALL(com.eveningoutpost.dexdrip.watch.thinjam.Const.OPCODE_RESET_ALL) JoH(com.eveningoutpost.dexdrip.Models.JoH) GlucoseTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.GlucoseTx) THINJAM_NOTIFY_TYPE_LOW_ALERT(com.eveningoutpost.dexdrip.watch.thinjam.Const.THINJAM_NOTIFY_TYPE_LOW_ALERT) OPCODE_EASY_AUTH(com.eveningoutpost.dexdrip.watch.thinjam.Const.OPCODE_EASY_AUTH) JoH.bytesToHex(com.eveningoutpost.dexdrip.Models.JoH.bytesToHex) PROTOTYPE(com.eveningoutpost.dexdrip.watch.thinjam.BlueJayService.ThinJamState.PROTOTYPE) CalibrationState(com.eveningoutpost.dexdrip.G5Model.CalibrationState) Home(com.eveningoutpost.dexdrip.Home) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic) WakeLockTrampoline(com.eveningoutpost.dexdrip.utils.framework.WakeLockTrampoline) Unitized(com.eveningoutpost.dexdrip.UtilityModels.Unitized) THINJAM_NOTIFY_TYPE_TEXTBOX1(com.eveningoutpost.dexdrip.watch.thinjam.Const.THINJAM_NOTIFY_TYPE_TEXTBOX1) BulkUpRequestTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.BulkUpRequestTx) CRITICAL(com.eveningoutpost.dexdrip.UtilityModels.StatusItem.Highlight.CRITICAL) GOOD(com.eveningoutpost.dexdrip.UtilityModels.StatusItem.Highlight.GOOD) SCHEDULE_ITEMS(com.eveningoutpost.dexdrip.watch.thinjam.BlueJayService.ThinJamState.SCHEDULE_ITEMS) HexDump(com.eveningoutpost.dexdrip.ImportedLibraries.usbserial.util.HexDump) ArrayList(java.util.ArrayList) BlueJay.versionSufficient(com.eveningoutpost.dexdrip.watch.thinjam.BlueJay.versionSufficient) JoH.tsl(com.eveningoutpost.dexdrip.Models.JoH.tsl) Helper(com.eveningoutpost.dexdrip.utils.bt.Helper) SetTxIdTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.SetTxIdTx) DefineWindowTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.DefineWindowTx) THINJAM_NOTIFY_TYPE_CANCEL(com.eveningoutpost.dexdrip.watch.thinjam.Const.THINJAM_NOTIFY_TYPE_CANCEL) JamBaseBluetoothSequencer(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer) ERROR_MISC(com.eveningoutpost.dexdrip.watch.thinjam.Const.ERROR_MISC) ObservableField(android.databinding.ObservableField) RxBleDeviceServices(com.polidea.rxandroidble2.RxBleDeviceServices) THINJAM_BULK(com.eveningoutpost.dexdrip.watch.thinjam.Const.THINJAM_BULK) lombok.val(lombok.val) THINJAM_NOTIFY_TYPE_DIALOG(com.eveningoutpost.dexdrip.watch.thinjam.Const.THINJAM_NOTIFY_TYPE_DIALOG) AlertPlayer(com.eveningoutpost.dexdrip.UtilityModels.AlertPlayer) OPCODE_GET_STATUS_3(com.eveningoutpost.dexdrip.watch.thinjam.Const.OPCODE_GET_STATUS_3) UserError(com.eveningoutpost.dexdrip.Models.UserError) OPCODE_GET_STATUS_1(com.eveningoutpost.dexdrip.watch.thinjam.Const.OPCODE_GET_STATUS_1) OPCODE_GET_STATUS_2(com.eveningoutpost.dexdrip.watch.thinjam.Const.OPCODE_GET_STATUS_2) PersistentStore(com.eveningoutpost.dexdrip.UtilityModels.PersistentStore) Treatments(com.eveningoutpost.dexdrip.Models.Treatments) BitmapTools(com.eveningoutpost.dexdrip.watch.thinjam.utils.BitmapTools) com.eveningoutpost.dexdrip.xdrip(com.eveningoutpost.dexdrip.xdrip) FirmwareInfo(com.eveningoutpost.dexdrip.watch.thinjam.firmware.FirmwareInfo) RequiredArgsConstructor(lombok.RequiredArgsConstructor) INIT(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.INIT) FEATURE_TJ_DISP_A(com.eveningoutpost.dexdrip.watch.thinjam.Const.FEATURE_TJ_DISP_A) IBinder(android.os.IBinder) BytesGenerator(com.eveningoutpost.dexdrip.utils.BytesGenerator) ERROR_OUT_OF_RANGE(com.eveningoutpost.dexdrip.watch.thinjam.Const.ERROR_OUT_OF_RANGE) BleCannotSetCharacteristicNotificationException(com.polidea.rxandroidble2.exceptions.BleCannotSetCharacteristicNotificationException) NotifyTx.getPacketStreamForNotification(com.eveningoutpost.dexdrip.watch.thinjam.messages.NotifyTx.getPacketStreamForNotification) PowerManager(android.os.PowerManager) StatusItem(com.eveningoutpost.dexdrip.UtilityModels.StatusItem) BleCharacteristicNotFoundException(com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException) BlueJay.shouldSendReadings(com.eveningoutpost.dexdrip.watch.thinjam.BlueJay.shouldSendReadings) Locale(java.util.Locale) Schedulers(io.reactivex.schedulers.Schedulers) DEXCOM_PERIOD(com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder.DEXCOM_PERIOD) DISCOVER(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.DISCOVER) BlueJayFirmware.parse(com.eveningoutpost.dexdrip.watch.thinjam.firmware.BlueJayFirmware.parse) BAD(com.eveningoutpost.dexdrip.UtilityModels.StatusItem.Highlight.BAD) THINJAM_OTA(com.eveningoutpost.dexdrip.watch.thinjam.Const.THINJAM_OTA) CLOSE(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.CLOSE) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) NORMAL(com.eveningoutpost.dexdrip.UtilityModels.StatusItem.Highlight.NORMAL) Expose(com.google.gson.annotations.Expose) UUID(java.util.UUID) ENABLE_NOTIFICATIONS(com.eveningoutpost.dexdrip.watch.thinjam.BlueJayService.ThinJamState.ENABLE_NOTIFICATIONS) THINJAM_NOTIFY_TYPE_HIGH_ALERT(com.eveningoutpost.dexdrip.watch.thinjam.Const.THINJAM_NOTIFY_TYPE_HIGH_ALERT) List(java.util.List) BleDisconnectedException(com.polidea.rxandroidble2.exceptions.BleDisconnectedException) NOTICE(com.eveningoutpost.dexdrip.UtilityModels.StatusItem.Highlight.NOTICE) R(com.eveningoutpost.dexdrip.R) Subscription(com.eveningoutpost.dexdrip.utils.bt.Subscription) Ob1G5CollectionService(com.eveningoutpost.dexdrip.Services.Ob1G5CollectionService) ERROR_OK(com.eveningoutpost.dexdrip.watch.thinjam.Const.ERROR_OK) BulkUpTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.BulkUpTx) ThinJamActivity(com.eveningoutpost.dexdrip.ui.activities.ThinJamActivity) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) JoH.msTill(com.eveningoutpost.dexdrip.Models.JoH.msTill) Setter(lombok.Setter) LOG_TESTS(com.eveningoutpost.dexdrip.watch.thinjam.BlueJayService.ThinJamState.LOG_TESTS) Getter(lombok.Getter) AuthReqTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.AuthReqTx) Constants(com.eveningoutpost.dexdrip.UtilityModels.Constants) PushRx(com.eveningoutpost.dexdrip.watch.thinjam.messages.PushRx) Pair(android.util.Pair) Intent(android.content.Intent) HOUR_IN_MS(com.eveningoutpost.dexdrip.UtilityModels.Constants.HOUR_IN_MS) RBulkUpTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.RBulkUpTx) StandbyTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.StandbyTx) JoH.niceTimeScalar(com.eveningoutpost.dexdrip.Models.JoH.niceTimeScalar) Binder(android.os.Binder) SuppressLint(android.annotation.SuppressLint) OTAUI(com.eveningoutpost.dexdrip.watch.thinjam.BlueJayService.ThinJamState.OTAUI) SlidingWindowConstraint(com.eveningoutpost.dexdrip.utils.time.SlidingWindowConstraint) TJ_BitmapType(com.eveningoutpost.dexdrip.watch.thinjam.utils.BitmapTools.TJ_BitmapType) SLEEP(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.SLEEP) BluetoothGattService(android.bluetooth.BluetoothGattService) com.eveningoutpost.dexdrip.xdrip.gs(com.eveningoutpost.dexdrip.xdrip.gs) BlueJayFirmware.split(com.eveningoutpost.dexdrip.watch.thinjam.firmware.BlueJayFirmware.split) JoH.msSince(com.eveningoutpost.dexdrip.Models.JoH.msSince) JoH.threadSleep(com.eveningoutpost.dexdrip.Models.JoH.threadSleep) BackFillTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.BackFillTx) TimeUnit(java.util.concurrent.TimeUnit) SECOND_IN_MS(com.eveningoutpost.dexdrip.UtilityModels.Constants.SECOND_IN_MS) THINJAM_WRITE(com.eveningoutpost.dexdrip.watch.thinjam.Const.THINJAM_WRITE) ResetPersistTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.ResetPersistTx) RUN_QUEUE(com.eveningoutpost.dexdrip.watch.thinjam.BlueJayService.ThinJamState.RUN_QUEUE) SEND_QUEUE(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.SEND_QUEUE) SetGammaEtc(com.eveningoutpost.dexdrip.watch.thinjam.messages.SetGammaEtc) MINUTE_IN_MS(com.eveningoutpost.dexdrip.UtilityModels.Constants.MINUTE_IN_MS) lombok.val(lombok.val) BleCannotSetCharacteristicNotificationException(com.polidea.rxandroidble2.exceptions.BleCannotSetCharacteristicNotificationException) BleCharacteristicNotFoundException(com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException) BleDisconnectedException(com.polidea.rxandroidble2.exceptions.BleDisconnectedException) Subscription(com.eveningoutpost.dexdrip.utils.bt.Subscription)

Aggregations

SuppressLint (android.annotation.SuppressLint)8 BluetoothGattCharacteristic (android.bluetooth.BluetoothGattCharacteristic)8 BluetoothGattService (android.bluetooth.BluetoothGattService)8 Intent (android.content.Intent)8 PowerManager (android.os.PowerManager)8 Pair (android.util.Pair)8 BgReading (com.eveningoutpost.dexdrip.Models.BgReading)8 JoH (com.eveningoutpost.dexdrip.Models.JoH)8 JoH.bytesToHex (com.eveningoutpost.dexdrip.Models.JoH.bytesToHex)8 JoH.msTill (com.eveningoutpost.dexdrip.Models.JoH.msTill)8 JoH.niceTimeScalar (com.eveningoutpost.dexdrip.Models.JoH.niceTimeScalar)8 UserError (com.eveningoutpost.dexdrip.Models.UserError)8 R (com.eveningoutpost.dexdrip.R)8 JamBaseBluetoothSequencer (com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer)8 CLOSE (com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.CLOSE)8 CLOSED (com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.CLOSED)8 INIT (com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.INIT)8 SLEEP (com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.SLEEP)8 AlertPlayer (com.eveningoutpost.dexdrip.UtilityModels.AlertPlayer)8 Constants (com.eveningoutpost.dexdrip.UtilityModels.Constants)8