Skip to content

Commit

Permalink
Merge pull request #520 from NordicSemiconductor/improvement/service-…
Browse files Browse the repository at this point in the history
…discovery

Discovering services from current thread instead of the main thread
  • Loading branch information
philips77 committed Sep 27, 2023
2 parents f7495be + 827acca commit 839cbb3
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,15 +348,13 @@ public void onReceive(final Context context, final Intent intent) {
// despite the fail bonding process.
// See: https://github.com/NordicSemiconductor/Android-BLE-Library/issues/335
if (!servicesDiscovered && !serviceDiscoveryRequested) {
post(() -> {
final BluetoothGatt bluetoothGatt = BleManagerHandler.this.bluetoothGatt;
if (!servicesDiscovered && !serviceDiscoveryRequested && bluetoothGatt != null) {
serviceDiscoveryRequested = true;
log(Log.VERBOSE, () -> "Discovering services...");
log(Log.DEBUG, () -> "gatt.discoverServices()");
bluetoothGatt.discoverServices();
}
});
final BluetoothGatt bluetoothGatt = BleManagerHandler.this.bluetoothGatt;
if (bluetoothGatt != null) {
serviceDiscoveryRequested = true;
log(Log.VERBOSE, () -> "Discovering services...");
log(Log.DEBUG, () -> "gatt.discoverServices()");
bluetoothGatt.discoverServices();
}
return;
}
} else if (previousBondState == BluetoothDevice.BOND_BONDED) {
Expand Down Expand Up @@ -401,15 +399,13 @@ public void onReceive(final Context context, final Intent intent) {
// If the device started to pair just after the connection was
// established the services were not discovered.
if (!servicesDiscovered && !serviceDiscoveryRequested) {
post(() -> {
final BluetoothGatt bluetoothGatt = BleManagerHandler.this.bluetoothGatt;
if (!servicesDiscovered && !serviceDiscoveryRequested && bluetoothGatt != null) {
serviceDiscoveryRequested = true;
log(Log.VERBOSE, () -> "Discovering services...");
log(Log.DEBUG, () -> "gatt.discoverServices()");
bluetoothGatt.discoverServices();
}
});
final BluetoothGatt bluetoothGatt = BleManagerHandler.this.bluetoothGatt;
if (bluetoothGatt != null) {
serviceDiscoveryRequested = true;
log(Log.VERBOSE, () -> "Discovering services...");
log(Log.DEBUG, () -> "gatt.discoverServices()");
bluetoothGatt.discoverServices();
}
return;
}
// On older Android versions, after executing a command on secured attribute
Expand Down Expand Up @@ -1015,10 +1011,8 @@ private boolean internalSendNotification(@Nullable final BluetoothGattCharacteri

// The onNotificationSent callback is not called before Android Lollipop.
if (result && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
post(() -> {
notifyNotificationSent(bluetoothDevice);
nextRequest(true);
});
notifyNotificationSent(bluetoothDevice);
nextRequest(true);
}
}
return result;
Expand Down

0 comments on commit 839cbb3

Please sign in to comment.