6

I want to know when my bluetooth connection to the device gets disconnected. I found this to check:

    IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
    IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
    IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    this.registerReceiver(mReceiver, filter1);
    this.registerReceiver(mReceiver, filter2);
    this.registerReceiver(mReceiver, filter3);

  //The BroadcastReceiver that listens for bluetooth broadcasts
   mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
               //Device found
            }
            else if (BluetoothAdapter.ACTION_ACL_CONNECTED.equals(action)) {
               //Device is now connected
            }
            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                //Done searching
            }
            else if (BluetoothAdapter.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
               //Device is about to disconnect
            }
            else if (BluetoothAdapter.ACTION_ACL_DISCONNECTED.equals(action)) {
               //Device has disconnected
            }           
        }
    };

But the ACTION_ACL_FOUND, ACTION_ACL_DISCONNECT_REQUESTED and the ACTION_ACL_DISCONNECTED in the onReceive method can't be resolved or is not a field.

So why they cannot be resolved?

Or is there another new solution for that?

silvia_aut
  • 1,481
  • 6
  • 19
  • 33
  • Don't you mean `BluetoothDevice.ACTION_ACL_FOUND`? These fields don't exist in `BluetoothAdapter`. – Ken Wolf Oct 25 '13 at 08:16
  • I referred tho this: http://stackoverflow.com/questions/4715865/how-to-programmatically-tell-if-a-bluetooth-device-is-connected-android-2-2 – silvia_aut Oct 25 '13 at 08:18
  • Change all your references to the fields that don't work to `BluetoothDevice`. The fields don't exist in `BluetoothAdapter`. (http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html) – Ken Wolf Oct 25 '13 at 08:19
  • But the ACTION_DISOVERY_FINISHED works on BluetoothAdapter. So I just change the 3 which doesn't work? – silvia_aut Oct 25 '13 at 08:21

1 Answers1

4

Change your onReceive to:

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
           //Device found
        }
        else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
           //Device is now connected
        }
        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            //Done searching
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
           //Device is about to disconnect
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
           //Device has disconnected
        }           
    }

The 3 fields you mention do not exist in BluetoothAdapter:(http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html)

They are in BluetoothDevice: (http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#ACTION_ACL_CONNECTED)

You are also only registering to listen for 3 actions, so you don't really need the others unless you are registering for them somewhere else.

Ken Wolf
  • 23,133
  • 6
  • 63
  • 84