From 7ec68cd69a50ed476d7d500a3a04093d3f086479 Mon Sep 17 00:00:00 2001 From: Michael Meisel Date: Tue, 20 Jan 2026 15:44:03 -0800 Subject: [PATCH 1/2] Make pairing BroadcastReceiver optional --- .../no/nordicsemi/android/ble/BleManager.java | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/ble/src/main/java/no/nordicsemi/android/ble/BleManager.java b/ble/src/main/java/no/nordicsemi/android/ble/BleManager.java index d22e2c86..82e16cf9 100644 --- a/ble/src/main/java/no/nordicsemi/android/ble/BleManager.java +++ b/ble/src/main/java/no/nordicsemi/android/ble/BleManager.java @@ -180,10 +180,12 @@ public BleManager(@NonNull final Context context, @NonNull final Handler handler this.requestHandler = getGattCallback(); this.requestHandler.init(this, handler); - ContextCompat.registerReceiver(context, mPairingRequestBroadcastReceiver, - // BluetoothDevice.ACTION_PAIRING_REQUEST - new IntentFilter("android.bluetooth.device.action.PAIRING_REQUEST"), - ContextCompat.RECEIVER_EXPORTED); + if (isPairingSupported()) { + ContextCompat.registerReceiver(context, mPairingRequestBroadcastReceiver, + // BluetoothDevice.ACTION_PAIRING_REQUEST + new IntentFilter("android.bluetooth.device.action.PAIRING_REQUEST"), + ContextCompat.RECEIVER_EXPORTED); + } } /** @@ -251,6 +253,16 @@ protected boolean isOptionalServiceSupported(@NonNull final BluetoothGatt gatt) return requestHandler.isOptionalServiceSupported(gatt); } + /** + * This method should return true if your want to be able to pair with + * the gatt device. If you return false, the pairing request BroadcastReceiver will + * not be registered. + */ + protected boolean isPairingSupported() { + // Don't call super.isPairingSupported() when overriding this method. + return true; + } + /** * In this method the manager should get references to server characteristics and descriptors * that will use. The method is called after the service discovery of a remote device has @@ -301,15 +313,17 @@ protected void onManagerReady() { * Closes and releases resources. When the device disconnected with link loss and * {@link ConnectRequest#shouldAutoConnect()} returned true you have to call this method to * close the connection. If you intend to reuse this instance for multiple connections, - * do not call this method after {@link #disconnect()}. If you are done with this instance, + * do not call this method after {@link #disconnect()}. If you are done with this instance, * call this method from the {@link #disconnect()} completion handler, e.g. - * disconnect().then { close() }. + * disconnect().then { close() }. */ public void close() { - try { - context.unregisterReceiver(mPairingRequestBroadcastReceiver); - } catch (final Exception e) { - // The receiver must have been already unregistered before. + if (isPairingSupported()) { + try { + context.unregisterReceiver(mPairingRequestBroadcastReceiver); + } catch (final Exception e) { + // The receiver must have been already unregistered before. + } } if (serverManager != null) { serverManager.removeManager(this); From 1eede98165ba1ad5a97eb4ec1f2b379384aebda8 Mon Sep 17 00:00:00 2001 From: Michael Meisel Date: Thu, 22 Jan 2026 10:28:26 -0800 Subject: [PATCH 2/2] Only check isPairingSupported() once --- .../java/no/nordicsemi/android/ble/BleManager.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/ble/src/main/java/no/nordicsemi/android/ble/BleManager.java b/ble/src/main/java/no/nordicsemi/android/ble/BleManager.java index 82e16cf9..2fcbcfd4 100644 --- a/ble/src/main/java/no/nordicsemi/android/ble/BleManager.java +++ b/ble/src/main/java/no/nordicsemi/android/ble/BleManager.java @@ -318,12 +318,10 @@ protected void onManagerReady() { * disconnect().then { close() }. */ public void close() { - if (isPairingSupported()) { - try { - context.unregisterReceiver(mPairingRequestBroadcastReceiver); - } catch (final Exception e) { - // The receiver must have been already unregistered before. - } + try { + context.unregisterReceiver(mPairingRequestBroadcastReceiver); + } catch (final Exception e) { + // The receiver must have been already unregistered before. } if (serverManager != null) { serverManager.removeManager(this);