Search in sources :

Example 61 with TransactionBuilder

use of nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder in project Gadgetbridge by Freeyourgadget.

the class WatchXPlusDeviceSupport method removeDataContentForTimestamp.

private void removeDataContentForTimestamp(int timestamp, DataType dataType) {
    byte[] command = WatchXPlusConstants.CMD_REMOVE_DATA_CONTENT;
    try {
        TransactionBuilder builder = performInitialized("removeDataContentForTimestamp");
        byte[] ts = Conversion.toByteArr32(timestamp);
        byte[] req = BLETypeConversions.join(dataType.getValue(), ts);
        builder.write(getCharacteristic(WatchXPlusConstants.UUID_CHARACTERISTIC_WRITE), buildCommand(command, WatchXPlusConstants.TASK, req));
        builder.queue(getQueue());
    } catch (IOException e) {
        LOG.warn(" Unable to remove data content ", e);
    }
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException)

Example 62 with TransactionBuilder

use of nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder in project Gadgetbridge by Freeyourgadget.

the class WatchXPlusDeviceSupport method requestBloodPressureMeasurement.

/**
 * request blood pressure measurement
 * first check if blood pressure is calibrated
 */
private void requestBloodPressureMeasurement() {
    if (!WatchXPlusDeviceCoordinator.isBPCalibrated) {
        LOG.info(" BP is NOT calibrated ");
        GB.toast("BP is not calibrated", Toast.LENGTH_LONG, GB.WARN);
        return;
    }
    try {
        TransactionBuilder builder = performInitialized("bpMeasure");
        byte[] command = WatchXPlusConstants.CMD_BLOOD_PRESSURE_MEASURE;
        builder.write(getCharacteristic(WatchXPlusConstants.UUID_CHARACTERISTIC_WRITE), buildCommand(command, WatchXPlusConstants.TASK, new byte[] { 0x01 }));
        builder.queue(getQueue());
    } catch (IOException e) {
        LOG.warn(" Unable to request BP Measure ", e);
    }
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException)

Example 63 with TransactionBuilder

use of nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder in project Gadgetbridge by Freeyourgadget.

the class InitOperation method onCharacteristicChanged.

@Override
public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    UUID characteristicUUID = characteristic.getUuid();
    if (HuamiService.UUID_CHARACTERISTIC_AUTH.equals(characteristicUUID)) {
        try {
            byte[] value = characteristic.getValue();
            huamiSupport.logMessageContent(value);
            if (value[0] == HuamiService.AUTH_RESPONSE && value[1] == HuamiService.AUTH_SEND_KEY && value[2] == HuamiService.AUTH_SUCCESS) {
                TransactionBuilder builder = createTransactionBuilder("Sending the secret key to the device");
                builder.write(characteristic, requestAuthNumber());
                huamiSupport.performImmediately(builder);
            } else if (value[0] == HuamiService.AUTH_RESPONSE && (value[1] & 0x0f) == HuamiService.AUTH_REQUEST_RANDOM_AUTH_NUMBER && value[2] == HuamiService.AUTH_SUCCESS) {
                byte[] eValue = handleAESAuth(value, getSecretKey());
                byte[] responseValue = org.apache.commons.lang3.ArrayUtils.addAll(new byte[] { (byte) (HuamiService.AUTH_SEND_ENCRYPTED_AUTH_NUMBER | cryptFlags), authFlags }, eValue);
                TransactionBuilder builder = createTransactionBuilder("Sending the encrypted random key to the device");
                builder.write(characteristic, responseValue);
                huamiSupport.setCurrentTimeWithService(builder);
                huamiSupport.performImmediately(builder);
            } else if (value[0] == HuamiService.AUTH_RESPONSE && (value[1] & 0x0f) == HuamiService.AUTH_SEND_ENCRYPTED_AUTH_NUMBER && value[2] == HuamiService.AUTH_SUCCESS) {
                TransactionBuilder builder = createTransactionBuilder("Authenticated, now initialize phase 2");
                builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext()));
                huamiSupport.enableFurtherNotifications(builder, true);
                huamiSupport.requestDeviceInfo(builder);
                huamiSupport.phase2Initialize(builder);
                huamiSupport.phase3Initialize(builder);
                huamiSupport.setInitialized(builder);
                huamiSupport.performImmediately(builder);
            } else {
                return super.onCharacteristicChanged(gatt, characteristic);
            }
        } catch (Exception e) {
            GB.toast(getContext(), "Error authenticating Huami device", Toast.LENGTH_LONG, GB.ERROR, e);
        }
        return true;
    } else {
        LOG.info("Unhandled characteristic changed: " + characteristicUUID);
        return super.onCharacteristicChanged(gatt, characteristic);
    }
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) UUID(java.util.UUID) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) SetDeviceStateAction(nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction)

Example 64 with TransactionBuilder

use of nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder in project Gadgetbridge by Freeyourgadget.

the class UpdateFirmwareOperation method sendFirmwareData.

/**
 * Method that uploads a firmware (fwbytes) to the Mi Band.
 * The firmware has to be split into chunks of 20 bytes each, and periodically a COMMAND_SYNC command has to be issued to the Mi Band.
 * <p/>
 * The Mi Band will send a notification after receiving this data to confirm if the firmware looks good to it.
 *
 * @param info
 * @return whether the transfer succeeded or not. Only a BT layer exception will cause the transmission to fail.
 * @see #handleNotificationNotif
 */
private boolean sendFirmwareData(HuamiFirmwareInfo info) {
    byte[] fwbytes = info.getBytes();
    int len = fwbytes.length;
    final int packetLength = getSupport().getMTU() - 3;
    int packets = len / packetLength;
    try {
        // going from 0 to len
        int firmwareProgress = 0;
        TransactionBuilder builder = performInitialized("send firmware packet");
        builder.write(fwCControlChar, getFirmwareStartCommand());
        for (int i = 0; i < packets; i++) {
            byte[] fwChunk = Arrays.copyOfRange(fwbytes, i * packetLength, i * packetLength + packetLength);
            builder.write(fwCDataChar, fwChunk);
            firmwareProgress += packetLength;
            int progressPercent = (int) ((((float) firmwareProgress) / len) * 100);
            if ((i > 0) && (i % 100 == 0)) {
                builder.write(fwCControlChar, new byte[] { HuamiService.COMMAND_FIRMWARE_UPDATE_SYNC });
                builder.add(new SetProgressAction(getContext().getString(R.string.updatefirmwareoperation_update_in_progress), true, progressPercent, getContext()));
            }
        }
        if (firmwareProgress < len) {
            byte[] lastChunk = Arrays.copyOfRange(fwbytes, packets * packetLength, len);
            builder.write(fwCDataChar, lastChunk);
        }
        builder.write(fwCControlChar, new byte[] { HuamiService.COMMAND_FIRMWARE_UPDATE_SYNC });
        builder.queue(getQueue());
    } catch (IOException ex) {
        LOG.error("Unable to send fw to device", ex);
        GB.updateInstallNotification(getContext().getString(R.string.updatefirmwareoperation_firmware_not_sent), false, 0, getContext());
        return false;
    }
    return true;
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException) SetProgressAction(nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetProgressAction)

Example 65 with TransactionBuilder

use of nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder in project Gadgetbridge by Freeyourgadget.

the class BFH16DeviceSupport method showNotification.

// FUNCTIONS
// ______________________________________________________________________________________________
// TODO: check
private void showNotification(byte icon, String title, String message) {
    try {
        TransactionBuilder builder = performInitialized("ShowNotification");
        byte[] titleBytes = stringToUTF8Bytes(title, 16);
        byte[] messageBytes = stringToUTF8Bytes(message, 80);
        for (int i = 1; i <= 7; i++) {
            byte[] currentPacket = new byte[20];
            currentPacket[0] = BFH16Constants.CMD_ACTION_SHOW_NOTIFICATION;
            currentPacket[1] = 7;
            currentPacket[2] = (byte) i;
            switch(i) {
                case 1:
                    currentPacket[4] = icon;
                    break;
                case 2:
                    if (titleBytes != null) {
                        System.arraycopy(titleBytes, 0, currentPacket, 3, 6);
                        System.arraycopy(titleBytes, 6, currentPacket, 10, 10);
                    }
                    break;
                default:
                    if (messageBytes != null) {
                        System.arraycopy(messageBytes, 16 * (i - 3), currentPacket, 3, 6);
                        System.arraycopy(messageBytes, 6 + 16 * (i - 3), currentPacket, 10, 10);
                    }
                    break;
            }
            builder.write(ctrlCharacteristic, currentPacket);
        }
        builder.queue(getQueue());
    } catch (IOException e) {
        LOG.warn(e.getMessage());
    }
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException)

Aggregations

TransactionBuilder (nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder)279 IOException (java.io.IOException)218 BluetoothGattCharacteristic (android.bluetooth.BluetoothGattCharacteristic)17 Alarm (nodomain.freeyourgadget.gadgetbridge.model.Alarm)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 GregorianCalendar (java.util.GregorianCalendar)11 UUID (java.util.UUID)11 Calendar (java.util.Calendar)10 SetDeviceBusyAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceBusyAction)10 ByteBuffer (java.nio.ByteBuffer)8 Prefs (nodomain.freeyourgadget.gadgetbridge.util.Prefs)8 ServerTransactionBuilder (nodomain.freeyourgadget.gadgetbridge.service.btle.ServerTransactionBuilder)7 SetDeviceStateAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction)7 GBException (nodomain.freeyourgadget.gadgetbridge.GBException)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 SetProgressAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetProgressAction)4 AlertNotificationProfile (nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertNotificationProfile)4 NewAlert (nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.NewAlert)4 GBPrefs (nodomain.freeyourgadget.gadgetbridge.util.GBPrefs)4 InvalidKeyException (java.security.InvalidKeyException)3