bluetooth – Why do BroadcastReceiver is not calling everytime?


I am new to android and learning how to connect to device with parameter.This is the issue that i’m facing “Once Bluetooth connection is established i am able to take 1 parameter, but not able connect to other functions…”.

In the below script in aashaDiscovery() function for 1st time when we are connecting to aasha with any command its asking the permission of location that’s in else part then its correctly accessing the registerReceiver(searchAasha,intentFilterActions()); the searchAasha, but if the location permission is already available it will go inside if condition in that registerReceiver(searchAasha,intentFilterActions()); searchAasha is not calling.Is there anyway to call the BroadcastReceiver searchAasha everytime when ever its needed.

Error:

2020-09-09 11:43:38.148 29209-29252/? E/bt_btif: bte_scan_filt_param_cfg_evt, 23

Script:

public String scanAasha(String aashacomand){
   aasha=aashacomand;
   mAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mAdapter!= null) {
      mAdapter.startDiscovery();
      aashaDiscovery();
   }
     if (mBluetoothChatServiceaasha.maasha.contains("B_")) {
       String a() = mBluetoothChatServiceaasha.maasha.split("_");
       return a(1) + " " + a(2);
     }
     if (mBluetoothChatServiceaasha.maasha.contains("O_")) {
       String a() = mBluetoothChatServiceaasha.maasha.split("_");
       return a(1) + " " + a(2);
     }
     if (mBluetoothChatServiceaasha.maasha.contains("T_")) {
       String a() = mBluetoothChatServiceaasha.maasha.split("_");
       return a(2);
     }
    return aashacomand;
  }

  
public void aashaDiscovery() {
int hasPermission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION);
if (hasPermission == PackageManager.PERMISSION_GRANTED) {
registerReceiver(searchAasha,intentFilterActions());
mAdapter.startDiscovery();
return;
}
else{
ActivityCompat.requestPermissions(MainActivity.this,
new String(){
android.Manifest.permission.ACCESS_COARSE_LOCATION},
AASHA_REQUEST_COARSE_LOCATION_PERMISSIONS);
registerReceiver(searchAasha,intentFilterActions());
mAdapter.startDiscovery();
return;
}
}
  
private BroadcastReceiver searchAasha = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Bundle b = intent.getExtras();
Object() lstName = b.keySet().toArray();
// ??????????????????????
for (int i = 0; i < lstName.length; i++) {
String keyName = lstName(i).toString();
Log.e(keyName, String.valueOf(b.get(keyName)));
}

if(BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getName()==null) {
return;
}
if (device.getName().contains("SPP")) {
mBluetoothChatServiceaasha.start();
mBluetoothChatServiceaasha.connect(device);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
};