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 c70dc18 commit 331f339
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/android/BLECentralPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@

import java.util.*;

import androidx.core.content.ContextCompat;

import static android.bluetooth.BluetoothDevice.DEVICE_TYPE_DUAL;
import static android.bluetooth.BluetoothDevice.DEVICE_TYPE_LE;
import static android.bluetooth.BluetoothDevice.ACTION_BOND_STATE_CHANGED;
Expand Down Expand Up @@ -114,6 +116,8 @@ public class BLECentralPlugin extends CordovaPlugin {
private static final String START_LOCATION_STATE_NOTIFICATIONS = "startLocationStateNotifications";
private static final String STOP_LOCATION_STATE_NOTIFICATIONS = "stopLocationStateNotifications";

private static final int RECEIVER_EXPORTED = 0x2; // ContextCompat.RECEIVER_EXPORTED

// callbacks
CallbackContext discoverCallback;
private CallbackContext enableBluetoothCallback;
Expand Down Expand Up @@ -660,7 +664,7 @@ public void onReceive(Context context, Intent intent) {

try {
IntentFilter intentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
webView.getContext().registerReceiver(this.stateReceiver, intentFilter);
ContextCompat.registerReceiver(webView.getContext(), stateReceiver, intentFilter, RECEIVER_EXPORTED);
} catch (Exception e) {
LOG.e(TAG, "Error registering state receiver: " + e.getMessage(), e);
}
Expand Down Expand Up @@ -707,7 +711,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);
ContextCompat.registerReceiver(webView.getContext(), this.locationStateReceiver, intentFilter, RECEIVER_EXPORTED);
} catch (Exception e) {
LOG.e(TAG, "Error registering location state receiver: " + e.getMessage(), e);
}
Expand Down Expand Up @@ -742,7 +746,7 @@ private void connect(CallbackContext callbackContext, String macAddress) {
return;
}

if (!peripherals.containsKey(macAddress) && BLECentralPlugin.this.bluetoothAdapter.checkBluetoothAddress(macAddress)) {
if (!peripherals.containsKey(macAddress) && BluetoothAdapter.checkBluetoothAddress(macAddress)) {
BluetoothDevice device = BLECentralPlugin.this.bluetoothAdapter.getRemoteDevice(macAddress);
Peripheral peripheral = new Peripheral(device);
peripherals.put(macAddress, peripheral);
Expand Down Expand Up @@ -848,7 +852,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);
ContextCompat.registerReceiver(webView.getContext(), broadCastReceiver, intentFilter, RECEIVER_EXPORTED);

callbackContext.success("OK");
} catch (Exception e) {
Expand Down Expand Up @@ -1506,7 +1510,7 @@ public void onReceive(Context context, Intent intent) {
}
}
};
ContextCompat.registerReceiver(webView.getContext(), bondStateReceiver, new IntentFilter(ACTION_BOND_STATE_CHANGED), ContextCompat.RECEIVER_EXPORTED);
ContextCompat.registerReceiver(webView.getContext(), bondStateReceiver, new IntentFilter(ACTION_BOND_STATE_CHANGED), RECEIVER_EXPORTED);
}
}

Expand Down

0 comments on commit 331f339

Please sign in to comment.