Search in sources :

Example 11 with BluetoothGattCharacteristic

use of android.bluetooth.BluetoothGattCharacteristic 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 fwbytes
     * @return whether the transfer succeeded or not. Only a BT layer exception will cause the transmission to fail.
     * @see MiBandSupport#handleNotificationNotif
     */
private boolean sendFirmwareData(byte[] fwbytes) {
    int len = fwbytes.length;
    final int packetLength = 20;
    int packets = len / packetLength;
    BluetoothGattCharacteristic characteristicControlPoint = getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT);
    BluetoothGattCharacteristic characteristicFWData = getCharacteristic(MiBandService.UUID_CHARACTERISTIC_FIRMWARE_DATA);
    try {
        // going from 0 to len
        int firmwareProgress = 0;
        TransactionBuilder builder = performInitialized("send firmware packet");
        if (prefs.getBoolean("mi_low_latency_fw_update", true)) {
            getSupport().setLowLatency(builder);
        }
        for (int i = 0; i < packets; i++) {
            byte[] fwChunk = Arrays.copyOfRange(fwbytes, i * packetLength, i * packetLength + packetLength);
            builder.write(characteristicFWData, fwChunk);
            firmwareProgress += packetLength;
            int progressPercent = (int) ((((float) firmwareProgress) / len) * 100);
            if ((i > 0) && (i % 50 == 0)) {
                builder.write(characteristicControlPoint, new byte[] { MiBandService.COMMAND_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(characteristicFWData, lastChunk);
            firmwareProgress = len;
        }
        builder.write(characteristicControlPoint, new byte[] { MiBandService.COMMAND_SYNC });
        builder.queue(getQueue());
    } catch (IOException ex) {
        LOG.error("Unable to send fw to MI", 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) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic) SetProgressAction(nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetProgressAction)

Example 12 with BluetoothGattCharacteristic

use of android.bluetooth.BluetoothGattCharacteristic in project Gadgetbridge by Freeyourgadget.

the class MiBand2Support method setWearLocation.

/**
     * Part of device initialization process. Do not call manually.
     *
     * @param builder
     * @return
     */
private MiBand2Support setWearLocation(TransactionBuilder builder) {
    LOG.info("Attempting to set wear location...");
    BluetoothGattCharacteristic characteristic = getCharacteristic(MiBand2Service.UUID_UNKNOWN_CHARACTERISTIC8);
    if (characteristic != null) {
        builder.notify(characteristic, true);
        int location = MiBandCoordinator.getWearLocation(getDevice().getAddress());
        switch(location) {
            case // left hand
            0:
                builder.write(characteristic, MiBand2Service.WEAR_LOCATION_LEFT_WRIST);
                break;
            case // right hand
            1:
                builder.write(characteristic, MiBand2Service.WEAR_LOCATION_RIGHT_WRIST);
                break;
        }
        // TODO: this should actually be in some kind of finally-block in the queue. It should also be sent asynchronously after the notifications have completely arrived and processed.
        builder.notify(characteristic, false);
    }
    return this;
}
Also used : BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic)

Example 13 with BluetoothGattCharacteristic

use of android.bluetooth.BluetoothGattCharacteristic in project Gadgetbridge by Freeyourgadget.

the class MiBand2Support method setFitnessGoal.

/**
     * Part of device initialization process. Do not call manually.
     *
     * @param transaction
     * @return
     */
private MiBand2Support setFitnessGoal(TransactionBuilder transaction) {
    LOG.info("Attempting to set Fitness Goal...");
    BluetoothGattCharacteristic characteristic = getCharacteristic(MiBand2Service.UUID_UNKNOWN_CHARACTERISTIC8);
    if (characteristic != null) {
        int fitnessGoal = GBApplication.getPrefs().getInt(ActivityUser.PREF_USER_STEPS_GOAL, 10000);
        byte[] bytes = ArrayUtils.addAll(MiBand2Service.COMMAND_SET_FITNESS_GOAL_START, BLETypeConversions.fromUint16(fitnessGoal));
        bytes = ArrayUtils.addAll(bytes, MiBand2Service.COMMAND_SET_FITNESS_GOAL_END);
        transaction.write(characteristic, bytes);
    } else {
        LOG.info("Unable to set Fitness Goal");
    }
    return this;
}
Also used : BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic)

Example 14 with BluetoothGattCharacteristic

use of android.bluetooth.BluetoothGattCharacteristic in project Gadgetbridge by Freeyourgadget.

the class MiBand2Support method sendCalendarEvents.

/**
     * Fetch the events from the android device calendars and set the alarms on the miband.
     * @param builder
     */
private MiBand2Support sendCalendarEvents(TransactionBuilder builder) {
    BluetoothGattCharacteristic characteristic = getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_3_CONFIGURATION);
    Prefs prefs = GBApplication.getPrefs();
    int availableSlots = prefs.getInt(MiBandConst.PREF_MIBAND_RESERVE_ALARM_FOR_CALENDAR, 0);
    if (availableSlots > 0) {
        CalendarEvents upcomingEvents = new CalendarEvents();
        List<CalendarEvents.CalendarEvent> mEvents = upcomingEvents.getCalendarEventList(getContext());
        int iteration = 0;
        for (CalendarEvents.CalendarEvent mEvt : mEvents) {
            if (iteration >= availableSlots || iteration > 2) {
                break;
            }
            int slotToUse = 2 - iteration;
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(mEvt.getBegin());
            Alarm alarm = GBAlarm.createSingleShot(slotToUse, false, calendar);
            queueAlarm(alarm, builder, characteristic);
            iteration++;
        }
        builder.queue(getQueue());
    }
    return this;
}
Also used : GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) Alarm(nodomain.freeyourgadget.gadgetbridge.model.Alarm) GBAlarm(nodomain.freeyourgadget.gadgetbridge.impl.GBAlarm) CalendarEvents(nodomain.freeyourgadget.gadgetbridge.model.CalendarEvents) Prefs(nodomain.freeyourgadget.gadgetbridge.util.Prefs) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic)

Example 15 with BluetoothGattCharacteristic

use of android.bluetooth.BluetoothGattCharacteristic in project Gadgetbridge by Freeyourgadget.

the class MiBand2Support method onSetAlarms.

@Override
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
    try {
        BluetoothGattCharacteristic characteristic = getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_3_CONFIGURATION);
        TransactionBuilder builder = performInitialized("Set alarm");
        boolean anyAlarmEnabled = false;
        for (Alarm alarm : alarms) {
            anyAlarmEnabled |= alarm.isEnabled();
            queueAlarm(alarm, builder, characteristic);
        }
        builder.queue(getQueue());
        if (anyAlarmEnabled) {
            GB.toast(getContext(), getContext().getString(R.string.user_feedback_miband_set_alarms_ok), Toast.LENGTH_SHORT, GB.INFO);
        } else {
            GB.toast(getContext(), getContext().getString(R.string.user_feedback_all_alarms_disabled), Toast.LENGTH_SHORT, GB.INFO);
        }
    } catch (IOException ex) {
        GB.toast(getContext(), getContext().getString(R.string.user_feedback_miband_set_alarms_failed), Toast.LENGTH_LONG, GB.ERROR, ex);
    }
}
Also used : Alarm(nodomain.freeyourgadget.gadgetbridge.model.Alarm) GBAlarm(nodomain.freeyourgadget.gadgetbridge.impl.GBAlarm) TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic)

Aggregations

BluetoothGattCharacteristic (android.bluetooth.BluetoothGattCharacteristic)42 BluetoothGattService (android.bluetooth.BluetoothGattService)8 TransactionBuilder (nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder)7 IOException (java.io.IOException)6 BluetoothGattDescriptor (android.bluetooth.BluetoothGattDescriptor)5 GregorianCalendar (java.util.GregorianCalendar)4 GBAlarm (nodomain.freeyourgadget.gadgetbridge.impl.GBAlarm)4 Alarm (nodomain.freeyourgadget.gadgetbridge.model.Alarm)4 Calendar (java.util.Calendar)3 HashMap (java.util.HashMap)3 Prefs (nodomain.freeyourgadget.gadgetbridge.util.Prefs)3 UUID (java.util.UUID)2 CalendarEvents (nodomain.freeyourgadget.gadgetbridge.model.CalendarEvents)2 BaseSensor (sample.ble.sensortag.sensor.BaseSensor)2 TiPeriodicalSensor (sample.ble.sensortag.sensor.ti.TiPeriodicalSensor)2 BluetoothAdapter (android.bluetooth.BluetoothAdapter)1 BluetoothDevice (android.bluetooth.BluetoothDevice)1 BluetoothGatt (android.bluetooth.BluetoothGatt)1 BluetoothManager (android.bluetooth.BluetoothManager)1 Context (android.content.Context)1