Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BroadcastReceiver not called on bluetooth pairing

Very simple. the only code I have is this :

        final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)){
                mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Log.d(TAG,"Device "+mDevice.getName()+" Was Found");
            }
        }
    };

but this is not called when I pair a device . why ?

like image 643
yanish Avatar asked Nov 30 '25 03:11

yanish


1 Answers

Hi you need to register you broadcast receiver for following filters

ACTION_BOND_STATE_CHANGED

and then in onReceive

add them like this

    if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){
            mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (mDevice .getBondState() == BluetoothDevice.BOND_BONDED) {
            //means device paired
        }

   }

Read more Here Bluetooth Device Page

int     BOND_BONDED     //Indicates the remote device is bonded (paired).
int     BOND_BONDING    //Indicates bonding (pairing) is in progress with the remote device.
int     BOND_NONE   //Indicates the remote device is not bonded (paired). 

Edit: after last Comment

you also need to add

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mReceiver, filter);
like image 113
Neo Avatar answered Dec 02 '25 16:12

Neo