unable to use inside broadcast receiver
EarthMight opened this issue · comments
In my application inside broadcast adapter inside bluetooth on event, I am getting pair devices List and then checking every Device's name separately if any device contain specific sub String then try to connect with that using this library
code of Broadcast:
public class BrodcastBlueTooth extends BroadcastReceiver {
public BrodcastBlueTooth() {
}
@Override
public void onReceive(Context context, Intent intent) {
Bluetooth bluetooth;
bluetooth = new Bluetooth(context);
String DeviceName=null;
String action = intent.getAction();
// Log.d("BroadcastActions", "Action "+action+"received");
int state;
BluetoothDevice bluetoothDevice;
ArrayList<String> pairedDevice;
pairedDevice=new ArrayList<>();
final BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
switch(action)
{
case BluetoothAdapter.ACTION_STATE_CHANGED:
state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
if (state == BluetoothAdapter.STATE_OFF)
{
// Toast.makeText(context, "Bluetooth is off", Toast.LENGTH_SHORT).show();
Log.d("BroadcastActions", "Bluetooth is off");
}
else if (state == BluetoothAdapter.STATE_TURNING_OFF)
{
// Toast.makeText(context, "Bluetooth is turning off", Toast.LENGTH_SHORT).show();
Log.d("BroadcastActions", "Bluetooth is turning off");
}
else if(state == BluetoothAdapter.STATE_ON)
{
Toast.makeText(context, "Bluetooth is On", Toast.LENGTH_SHORT).show();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
pairedDevice.add(device.getName());
}
for(int i=0;i<pairedDevice.size();i++){
String name = pairedDevice.get(i);
if (name.contains("OBD")){
bluetooth.connectToName(name);
}else if (name.contains("obd")){
String Tname=name;
Log.d("Tname", Tname);
bluetooth.connectToName(name);
}else{
Toast.makeText(context,"No device which has obd name",Toast.LENGTH_SHORT).show();
}
}
}
Log.d("DevicePaired", String.valueOf(pairedDevice));
}
break;
}
}
}
Log:
2019-03-17 11:09:17.969 22377-22377/com.example.bluetoothlibraryexample E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bluetoothlibraryexample, PID: 22377
java.lang.RuntimeException: Unable to start receiver com.example.bluetoothlibraryexample.BrodcastBlueTooth: android.content.ReceiverCallNotAllowedException: BroadcastReceiver components are not allowed to register to receive intents
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3047)
at android.app.ActivityThread.-wrap18(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1561)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6126)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.content.ReceiverCallNotAllowedException: BroadcastReceiver components are not allowed to register to receive intents
at android.app.ReceiverRestrictedContext.registerReceiver(ContextImpl.java:104)
at android.app.ReceiverRestrictedContext.registerReceiver(ContextImpl.java:93)
at me.aflak.bluetooth.Bluetooth.onStart(Bluetooth.java:66)
at com.example.bluetoothlibraryexample.BrodcastBlueTooth.onReceive(BrodcastBlueTooth.java:50)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3040)
at android.app.ActivityThread.-wrap18(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1561)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6126)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Using same piece of code inside activity is working fine
Indeed, at its core the library is registering intents to receive bluetooth related updated. It appears you cannot do this inside a BroadcastReceiver
. You can still do this in a Service.