Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.edufolly.flutterbluetoothserial;

import android.content.Context;
import android.content.IntentFilter;
import android.os.Build;
import android.content.BroadcastReceiver;
import androidx.core.content.ContextCompat;

/**
* Fixed PlatformException "One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be
* specified when a receiver isn't being registered exclusively for system broadcasts"
* on devices with Android 12 or higher
* when app builded with Flutter 3.22.0 or higher
*/
public class BluetoothContextCompat {
static public void registerReceiver(Context context, BroadcastReceiver receiver, IntentFilter filter) {
ContextCompat.registerReceiver(
context,
receiver,
filter,
ContextCompat.RECEIVER_EXPORTED
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.github.edufolly.flutterbluetoothserial.le.BluetoothConnectionLE;
import io.github.edufolly.flutterbluetoothserial.BluetoothContextCompat;

public class FlutterBluetoothSerialPlugin implements FlutterPlugin, ActivityAware {
// Plugin
Expand Down Expand Up @@ -329,7 +330,7 @@ public void onAttachedToEngine(@NonNull FlutterPlugin.FlutterPluginBinding bindi
public void onListen(Object o, EventSink eventSink) {
stateSink = eventSink;

activeContext.registerReceiver(stateReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
BluetoothContextCompat.registerReceiver(activeContext, stateReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}

@Override
Expand Down Expand Up @@ -805,7 +806,7 @@ public void onReceive(Context context, Intent intent) {

final IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
//filter.setPriority(pairingRequestReceiverPriority + 1);
activeContext.registerReceiver(bondStateBroadcastReceiver, filter);
BluetoothContextCompat.registerReceiver(activeContext, bondStateBroadcastReceiver, filter);

if (!device.createBond()) {
result.error("bond_error", "error starting bonding process", null);
Expand All @@ -823,7 +824,7 @@ public void onReceive(Context context, Intent intent) {
FlutterBluetoothSerialPlugin.this.isPairingRequestHandlerSet = true;
final IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
//filter.setPriority(pairingRequestReceiverPriority);
activeContext.registerReceiver(pairingRequestReceiver, filter);
BluetoothContextCompat.registerReceiver(activeContext, pairingRequestReceiver, filter);
break;

case "pairingRequestHandlingDisable":
Expand Down Expand Up @@ -873,7 +874,7 @@ public void onReceive(Context context, Intent intent) {
IntentFilter intent = new IntentFilter();
intent.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
intent.addAction(BluetoothDevice.ACTION_FOUND);
activeContext.registerReceiver(discoveryReceiver, intent);
BluetoothContextCompat.registerReceiver(activeContext, discoveryReceiver, intent);

bluetoothAdapter.startDiscovery();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Arrays;
import java.util.UUID;

import io.github.edufolly.flutterbluetoothserial.BluetoothContextCompat;

/**
* wrap BLE communication into socket like class
* - connect, disconnect and write as methods,
Expand Down Expand Up @@ -152,9 +154,9 @@ void connect(SerialListener listener) throws IOException {
throw new IOException("already connected");
canceled = false;
this.listener = listener;
context.registerReceiver(disconnectBroadcastReceiver, new IntentFilter(Constants.INTENT_ACTION_DISCONNECT));
BluetoothContextCompat.registerReceiver(context, disconnectBroadcastReceiver, new IntentFilter(Constants.INTENT_ACTION_DISCONNECT));
Log.d(TAG, "connect "+device);
context.registerReceiver(pairingBroadcastReceiver, pairingIntentFilter);
BluetoothContextCompat.registerReceiver(context, pairingBroadcastReceiver, pairingIntentFilter);
if (Build.VERSION.SDK_INT < 23) {
Log.d(TAG, "connectGatt");
gatt = device.connectGatt(context, false, this);
Expand Down