Skip to content

Commit

Permalink
Mark broadcast receivers as exported for Android v34 (#1020)
Browse files Browse the repository at this point in the history
  • Loading branch information
peitschie committed Jul 13, 2024
1 parent 7925381 commit 3236669
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/android/BLECentralPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ public void onReceive(Context context, Intent intent) {

try {
IntentFilter intentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
webView.getContext().registerReceiver(this.stateReceiver, intentFilter);
registerSystemReceiverCompat(this.stateReceiver, intentFilter);
} catch (Exception e) {
LOG.e(TAG, "Error registering state receiver: " + e.getMessage(), e);
}
Expand Down Expand Up @@ -712,7 +712,7 @@ public void onReceive(Context context, Intent intent) {
try {
IntentFilter intentFilter = new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION);
intentFilter.addAction(Intent.ACTION_PROVIDER_CHANGED);
webView.getContext().registerReceiver(this.locationStateReceiver, intentFilter);
registerSystemReceiverCompat(this.locationStateReceiver, intentFilter);
} catch (Exception e) {
LOG.e(TAG, "Error registering location state receiver: " + e.getMessage(), e);
}
Expand Down Expand Up @@ -853,7 +853,7 @@ public void onReceive(Context context, Intent intent) {

IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
webView.getContext().registerReceiver(broadCastReceiver, intentFilter);
registerSystemReceiverCompat(broadCastReceiver, intentFilter);

callbackContext.success("OK");
} catch (Exception e) {
Expand Down Expand Up @@ -1164,7 +1164,7 @@ public void onScanResult(int callbackType, ScanResult result) {
if (!alreadyReported) {
Boolean isConnectable = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
isConnectable = result.isConnectable();
isConnectable = result.isConnectable();
}

Peripheral peripheral = new Peripheral(device, result.getRssi(), result.getScanRecord().getBytes(), isConnectable);
Expand Down Expand Up @@ -1511,7 +1511,7 @@ public void onReceive(Context context, Intent intent) {
}
}
};
webView.getContext().registerReceiver(bondStateReceiver, new IntentFilter(ACTION_BOND_STATE_CHANGED));
registerSystemReceiverCompat(bondStateReceiver, new IntentFilter(ACTION_BOND_STATE_CHANGED));
}
}

Expand All @@ -1521,4 +1521,14 @@ private void removeBondStateListener() {
bondStateReceiver = null;
}
}

@SuppressLint("UnspecifiedRegisterReceiverFlag")
private void registerSystemReceiverCompat(BroadcastReceiver receiver, IntentFilter filter) {
Context context = webView.getContext();
if (Build.VERSION.SDK_INT >= 34 /*14*/) {
context.registerReceiver(receiver, filter, Context.RECEIVER_EXPORTED);
} else {
context.registerReceiver(receiver, filter);
}
}
}

0 comments on commit 3236669

Please sign in to comment.