0

I have an Android app which uses Bluetooth. In order to have always bluetooth, I already implemented a Service to have it on always even if the app is in the background. Since there's going to be a lot of code dedicated for Bluetooth, I decided to create a ListActivity for all the methods and so on related with Bluetooth. Every time I start the service, a new object of this bluetooth class is created.

Now, with this detail in mind, my question is: if I do the adapter.Discovery in this object, how do I deal with the Broadcast receiver? For the receiver I have:

   private final BroadcastReceiver BEReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        int a = 1;
        a++;
};

I put this in the service and in the object to do different tests. For adding the receiver I have:

            IntentFilter BEFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
            mycontext.registerReceiver(BEReceiver, BEFilter);

I added these lines in the beggining of the service to call it locally, but it doesn't work. Then, I created BEFilter inside the object for bluetooth and here I was putting the BEFilter of the bluetooth object. Neither it worked. I also added those lines inside the constructor of the bluetooth object to do it locally, but it neither worked. What should I do? I need to add devices while adapter is discovered.

In the manifest i have:

   <intent-filter>
                <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
            <action android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" />
            <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
            <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
     </intent-filter>

for the bluetooth object, the service and the main activity where the service is created.

Myproblem is that BEReceiver is never called and I have no error in the debug.

In the manifest I also have:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>  <!-- BLE needs this! -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><!-- BLE needs this! -->
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Learning from masters
  • 2,032
  • 3
  • 29
  • 42

1 Answers1

0

Android Broadcast Receiver bluetooth events catching

It seems that what I was looking for it was:

IntentFilter filter2 = new IntentFilter();
filter2.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter2.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter2.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
registerReceiver(mBroadcastReceiver2, filter2);

with this, when the adapter is scanning is trigered.

Learning from masters
  • 2,032
  • 3
  • 29
  • 42