Search in sources :

Example 36 with BluetoothGattCharacteristic

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

the class AbstractBTLEDeviceSupport method gattServicesDiscovered.

private void gattServicesDiscovered(List<BluetoothGattService> discoveredGattServices) {
    if (discoveredGattServices == null) {
        logger.warn("No gatt services discovered: null!");
        return;
    }
    Set<UUID> supportedServices = getSupportedServices();
    Map<UUID, BluetoothGattCharacteristic> newCharacteristics = new HashMap<>();
    for (BluetoothGattService service : discoveredGattServices) {
        if (supportedServices.contains(service.getUuid())) {
            logger.debug("discovered supported service: " + BleNamesResolver.resolveServiceName(service.getUuid().toString()) + ": " + service.getUuid());
            List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
            if (characteristics == null || characteristics.isEmpty()) {
                logger.warn("Supported LE service " + service.getUuid() + "did not return any characteristics");
                continue;
            }
            HashMap<UUID, BluetoothGattCharacteristic> intmAvailableCharacteristics = new HashMap<>(characteristics.size());
            for (BluetoothGattCharacteristic characteristic : characteristics) {
                intmAvailableCharacteristics.put(characteristic.getUuid(), characteristic);
                logger.info("    characteristic: " + BleNamesResolver.resolveCharacteristicName(characteristic.getUuid().toString()) + ": " + characteristic.getUuid());
            }
            newCharacteristics.putAll(intmAvailableCharacteristics);
            synchronized (characteristicsMonitor) {
                mAvailableCharacteristics = newCharacteristics;
            }
        } else {
            logger.debug("discovered unsupported service: " + BleNamesResolver.resolveServiceName(service.getUuid().toString()) + ": " + service.getUuid());
        }
    }
}
Also used : HashMap(java.util.HashMap) BluetoothGattService(android.bluetooth.BluetoothGattService) UUID(java.util.UUID) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic)

Example 37 with BluetoothGattCharacteristic

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

the class BtLEAction method toString.

public String toString() {
    BluetoothGattCharacteristic characteristic = getCharacteristic();
    String uuid = characteristic == null ? "(null)" : characteristic.getUuid().toString();
    return getCreationTime() + ": " + getClass().getSimpleName() + " on characteristic: " + uuid;
}
Also used : BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic)

Example 38 with BluetoothGattCharacteristic

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

the class MiBand2Support method enableFurtherNotifications.

public MiBand2Support enableFurtherNotifications(TransactionBuilder builder, boolean enable) {
    builder.notify(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_3_CONFIGURATION), enable);
    builder.notify(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_6_BATTERY_INFO), enable);
    builder.notify(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_10_BUTTON), enable);
    BluetoothGattCharacteristic heartrateCharacteristic = getCharacteristic(GattCharacteristic.UUID_CHARACTERISTIC_HEART_RATE_MEASUREMENT);
    if (heartrateCharacteristic != null) {
        builder.notify(heartrateCharacteristic, enable);
    }
    return this;
}
Also used : BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic)

Example 39 with BluetoothGattCharacteristic

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

the class MiBand2Support method setHeartrateSleepSupport.

/**
     * Part of device initialization process. Do not call manually.
     *
     * @param builder
     */
private MiBand2Support setHeartrateSleepSupport(TransactionBuilder builder) {
    BluetoothGattCharacteristic characteristicHRControlPoint = getCharacteristic(GattCharacteristic.UUID_CHARACTERISTIC_HEART_RATE_CONTROL_POINT);
    final boolean enableHrSleepSupport = MiBandCoordinator.getHeartrateSleepSupport(getDevice().getAddress());
    if (characteristicHRControlPoint != null) {
        builder.notify(characteristicHRControlPoint, true);
        if (enableHrSleepSupport) {
            LOG.info("Enabling heartrate sleep support...");
            builder.write(characteristicHRControlPoint, MiBand2Service.COMMAND_ENABLE_HR_SLEEP_MEASUREMENT);
        } else {
            LOG.info("Disabling heartrate sleep support...");
            builder.write(characteristicHRControlPoint, MiBand2Service.COMMAND_DISABLE_HR_SLEEP_MEASUREMENT);
        }
        // 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(characteristicHRControlPoint, false);
    }
    return this;
}
Also used : BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic)

Example 40 with BluetoothGattCharacteristic

use of android.bluetooth.BluetoothGattCharacteristic in project BleLiteLib4android by afunx.

the class MainActivity method tapMe.

private void tapMe() {
    Log.i(TAG, "tapMe()");
    final String bleAddr = "24:0A:C4:00:02:BC";
    final UUID UUID_WIFI_SERVICE = UUID.fromString("0000ffff-0000-1000-8000-00805f9b34fb");
    final UUID UUID_CONFIGURE_CHARACTERISTIC = UUID.fromString("0000ff01-0000-1000-8000-00805f9b34fb");
    final Context context = MainActivity.this;
    final BleGattClientProxy proxy = mProxy;
    new Thread() {

        @Override
        public void run() {
            int count = 0;
            while (!mIsStop) {
                Log.e(TAG, "connect and close count: " + (++count));
                proxy.connect(bleAddr, 20000);
                BluetoothGattService gattService = proxy.discoverService(UUID_WIFI_SERVICE, 5000);
                proxy.requestMtu(64, 2000);
                if (gattService != null) {
                    BluetoothGattCharacteristic characteristic = proxy.discoverCharacteristic(gattService, UUID_CONFIGURE_CHARACTERISTIC);
                    if (characteristic != null) {
                        proxy.writeCharacteristic(characteristic, "ssid:wifi-11".getBytes(), 5000);
                        byte[] msgRead = proxy.readCharacteristic(characteristic, 5000);
                        System.out.println("BH msgRead: " + Arrays.toString(msgRead));
                        BleGattClientProxy.OnCharacteristicNotificationListener listener = new BleGattClientProxy.OnCharacteristicNotificationListener() {

                            @Override
                            public void onCharacteristicNotification(byte[] msg) {
                                System.out.println("BH ********************onCharacteristicNotification() msg: " + Arrays.toString(msg));
                            }
                        };
                        proxy.registerCharacteristicNotification(characteristic, listener);
                        proxy.writeCharacteristic(characteristic, "passwd:sumof1+1=2".getBytes(), 5000);
                        proxy.writeCharacteristic(characteristic, "confirm:".getBytes(), 5000);
                        proxy.unregisterCharacteristicNotification(characteristic.getUuid());
                        System.out.println("BH ********************Sleep 20 seconds********************");
                        try {
                            Thread.sleep(20 * 1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        mIsStop = true;
                    }
                }
                proxy.close();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();
}
Also used : Context(android.content.Context) BleGattClientProxy(com.afunx.ble.blelitelib.proxy.BleGattClientProxy) BluetoothGattService(android.bluetooth.BluetoothGattService) UUID(java.util.UUID) 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