3

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 ?

yanish
  • 257
  • 2
  • 15

3 Answers3

6

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);
mbo
  • 4,611
  • 2
  • 34
  • 54
Neo
  • 1,359
  • 14
  • 34
  • Hi . the only actions I get is ACL_CONNECTED followed by ACL_DISCONNECTED right after – yanish Nov 19 '15 at 11:35
  • again I for some reason don't get the STATE_CHANGED action , only ACL_CONNECTED followed by ACL_DISCONNECTED – yanish Nov 19 '15 at 11:56
  • you misunderstood me . I Log the actions I get in the onReceive and the only actions I receive is the ones I mentioned. meaning the if in your answer is never true – yanish Nov 19 '15 at 12:25
  • 1
    oh sorry then. do you add filter to your broadcast receiver.like this IntentFilter filter = new IntentFilter( BluetoothAdapter.ACTION_BOND_STATE_CHANGED); registerReceiver(mReceiver, filter); – Neo Nov 19 '15 at 12:29
  • 1
    In the intent filter, I believe BluetoothAdapter.ACTION_BOND_STATE_CHANGED should actually be BluetoothDevice.ACTION_BOND_STATE_CHANGED – superbeef150 Nov 25 '19 at 15:49
  • You forgot to handle the `BluetoothDevice.BOND_BONDING` state before. The immediately check of the bond state within the BluetoothDevice instance will result in `BluetoothDevice.BOND_NONE`. So first extract those info `val bondState = intent.getIntExtra(EXTRA_BOND_STATE, BOND_NONE)`. You can only trust those value. – drindt Sep 14 '20 at 12:38
0

did you add the permission in the manifest ??

<uses-permission android:name="android.permission.BLUETOOTH" />
S. Alawadi
  • 144
  • 1
  • 6
  • ok then print the action that you retrive and make sure the action name that you retrived is equal ACTION_FOUND – S. Alawadi Nov 19 '15 at 09:04
  • and make sure that you have registerd your broadcast ,, example : IntentFilter filter1 = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); registerReceiver(mBroadcastReceiver1, filter1); – S. Alawadi Nov 19 '15 at 09:05
0

From android 6.0 and greater you need to check additional permission ACCESS_COARSE_LOCATION in order to fetch list. without this you onReceive method with ACTION_FOUND will not call.

Amit Bhati
  • 1,405
  • 12
  • 20