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;
}
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);
}
}
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();
}
}
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;
}
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);
}
}
}
Aggregations