Search in sources :

Example 6 with BluetoothGatt

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

the class BleGattClientProxyImpl method __registerCharacteristicNotification.

private boolean __registerCharacteristicNotification(BluetoothGattCharacteristic characteristic, OnCharacteristicNotificationListener listener) {
    final BluetoothGatt bluetoothGatt = getBluetoothGatt();
    if (bluetoothGatt == null) {
        BleLiteLog.w(TAG, "__registerCharacteristicNotification() fail for bluetoothGatt is null");
        return false;
    }
    boolean isSetCharacteristicNotificationSuc = bluetoothGatt.setCharacteristicNotification(characteristic, true);
    if (isSetCharacteristicNotificationSuc) {
        registerListener(characteristic.getUuid(), listener);
    }
    BleLiteLog.i(TAG, "__registerCharacteristicNotification() characteristic's uuid: " + characteristic.getUuid() + ", register suc: " + isSetCharacteristicNotificationSuc);
    return isSetCharacteristicNotificationSuc;
}
Also used : BluetoothGatt(android.bluetooth.BluetoothGatt)

Example 7 with BluetoothGatt

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

the class BleConnectCompat method connectGatt.

public BluetoothGatt connectGatt(BluetoothDevice remoteDevice, boolean autoConnect, BluetoothGattCallback bluetoothGattCallback) {
    if (remoteDevice == null) {
        return null;
    }
    /**
         * Issue that caused a race condition mentioned below was fixed in 7.0.0_r1
         * https://android.googlesource.com/platform/frameworks/base/+/android-7.0.0_r1/core/java/android/bluetooth/BluetoothGatt.java#649
         * compared to
         * https://android.googlesource.com/platform/frameworks/base/+/android-6.0.1_r72/core/java/android/bluetooth/BluetoothGatt.java#739
         * issue: https://android.googlesource.com/platform/frameworks/base/+/d35167adcaa40cb54df8e392379dfdfe98bcdba2%5E%21/#F0
         */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || !autoConnect) {
        return connectGattCompat(bluetoothGattCallback, remoteDevice, false);
    }
    try {
        BleLiteLog.v(TAG, "Trying to connectGatt using reflection.");
        Object iBluetoothGatt = getIBluetoothGatt(getIBluetoothManager());
        if (iBluetoothGatt == null) {
            BleLiteLog.v(TAG, "Couldn't get iBluetoothGatt object");
            return connectGattCompat(bluetoothGattCallback, remoteDevice, true);
        }
        BluetoothGatt bluetoothGatt = createBluetoothGatt(iBluetoothGatt, remoteDevice);
        if (bluetoothGatt == null) {
            BleLiteLog.v(TAG, "Couldn't create BluetoothGatt object");
            return connectGattCompat(bluetoothGattCallback, remoteDevice, true);
        }
        boolean connectedSuccessfully = connectUsingReflection(bluetoothGatt, bluetoothGattCallback, true);
        if (!connectedSuccessfully) {
            BleLiteLog.v(TAG, "Connection using reflection failed, closing gatt");
            bluetoothGatt.close();
        }
        return bluetoothGatt;
    } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException | NoSuchFieldException exception) {
        BleLiteLog.v(TAG, "Error during reflection");
        return connectGattCompat(bluetoothGattCallback, remoteDevice, true);
    }
}
Also used : BluetoothGatt(android.bluetooth.BluetoothGatt) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 8 with BluetoothGatt

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

the class BleConnector method connect.

/**
     * connect to the specific bluetooth device
     */
public synchronized void connect() {
    if (mIsClosed) {
        BleLiteLog.w(TAG, "connect() has been closed already, do nothing");
        return;
    }
    if (mBluetoothGatt == null) {
        BleLiteLog.i(TAG, "connect() connect...");
        BleConnectCompat connectCompat = new BleConnectCompat(mAppContext);
        BluetoothGatt bluetoothGatt = connectCompat.connectGatt(mBluetoothDevice, false, mBluetoothGattCallback);
        mBluetoothGatt = bluetoothGatt;
    } else {
        BleLiteLog.i(TAG, "connect() reconnect...");
        mBluetoothGatt.connect();
    }
}
Also used : BluetoothGatt(android.bluetooth.BluetoothGatt)

Example 9 with BluetoothGatt

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

the class MiBandSupport method sendUserInfo.

/**
     * Part of device initialization process. Do not call manually.
     *
     * @param builder
     * @return
     */
private MiBandSupport sendUserInfo(TransactionBuilder builder) {
    LOG.debug("Writing User Info!");
    // Use a custom action instead of just builder.write() because mDeviceInfo
    // is set by handleDeviceInfo *after* this action is created.
    builder.add(new BtLEAction(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_USER_INFO)) {

        @Override
        public boolean expectsResult() {
            return true;
        }

        @Override
        public boolean run(BluetoothGatt gatt) {
            // at this point, mDeviceInfo should be set
            return new WriteAction(getCharacteristic(), MiBandCoordinator.getAnyUserInfo(getDevice().getAddress()).getData(mDeviceInfo)).run(gatt);
        }
    });
    return this;
}
Also used : ConditionalWriteAction(nodomain.freeyourgadget.gadgetbridge.service.btle.actions.ConditionalWriteAction) WriteAction(nodomain.freeyourgadget.gadgetbridge.service.btle.actions.WriteAction) BluetoothGatt(android.bluetooth.BluetoothGatt) BtLEAction(nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEAction)

Example 10 with BluetoothGatt

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

the class BtLEQueue method disconnect.

public void disconnect() {
    synchronized (mGattMonitor) {
        LOG.debug("disconnect()");
        BluetoothGatt gatt = mBluetoothGatt;
        if (gatt != null) {
            mBluetoothGatt = null;
            LOG.info("Disconnecting BtLEQueue from GATT device");
            gatt.disconnect();
            gatt.close();
            setDeviceConnectionState(State.NOT_CONNECTED);
        }
    }
}
Also used : BluetoothGatt(android.bluetooth.BluetoothGatt)

Aggregations

BluetoothGatt (android.bluetooth.BluetoothGatt)12 BluetoothGattService (android.bluetooth.BluetoothGattService)2 TargetApi (android.annotation.TargetApi)1 BluetoothAdapter (android.bluetooth.BluetoothAdapter)1 BluetoothDevice (android.bluetooth.BluetoothDevice)1 BluetoothGattCharacteristic (android.bluetooth.BluetoothGattCharacteristic)1 BluetoothGattDescriptor (android.bluetooth.BluetoothGattDescriptor)1 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 BleCloseOperation (com.afunx.ble.blelitelib.operation.BleCloseOperation)1 BleReadCharacteristicOperation (com.afunx.ble.blelitelib.operation.BleReadCharacteristicOperation)1 BleRequestMtuOperation (com.afunx.ble.blelitelib.operation.BleRequestMtuOperation)1 BleWriteCharacteristicOperation (com.afunx.ble.blelitelib.operation.BleWriteCharacteristicOperation)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ByteBuffer (java.nio.ByteBuffer)1 BtLEAction (nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEAction)1 ConditionalWriteAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.ConditionalWriteAction)1 WriteAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.WriteAction)1