How to Programmatically Pair or Unpair Android Bluetooth Device

In bluetooth wireless communication, if two devices want to connect and share data, they have to be paired first. To be paired means the two devices are aware of each other’s existence and trusted each other. 

Using Android Bluetooth API, we can use createBond method to pair with a device or removeBond to unpair. This  is an asynchronous call so that it will return immediately. To catch the pairing process, we have to register a BroadcastReceiver with ACTION_BOND_STATE_CHANGED intent to catch the process.

android bluetooth pair  android bluetooth device list
(apk and source code at the bottom if this post)

How to pair

private void pairDevice(BluetoothDevice device) {
        try {
            Method method = device.getClass().getMethod("createBond", (Class[]) null);
            method.invoke(device, (Object[]) null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

How to Unpair

private void unpairDevice(BluetoothDevice device) {
        try {
            Method method = device.getClass().getMethod("removeBond", (Class[]) null);
            method.invoke(device, (Object[]) null);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

The receiver to catch the pairing process:

private final BroadcastReceiver mPairReceiver = new BroadcastReceiver() {
	    public void onReceive(Context context, Intent intent) {
	        String action = intent.getAction();

	        if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
	        	 final int state 		= intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
	        	 final int prevState	= intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR);

	        	 if (state == BluetoothDevice.BOND_BONDED && prevState == BluetoothDevice.BOND_BONDING) {
	        		 showToast("Paired");
	        	 } else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDED){
	        		 showToast("Unpaired");
	        	 }

	        }
	    }
	};

Register receiver

IntentFilter intent = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mPairReceiver, intent);

For each changed state, the intent will carry extra fields EXTRA_BOND_STATE and EXTRA_PREVIOUS_BOND_STATE. Using the value from the  extra fields we can determine the state of pairing process.

State paired if current state is BOND_BONDED and previous state is BOND_BONDING.
State unpaired if current state is BOND_NONE and previous state is BOND_BONDED.

Download the sample APK and source code from github

facebooktwittergoogle_plusredditpinterestlinkedinmailby feather

Comments

  1. Ivan says

    I have problem with some devices that they are listed as bonded (paired) but when I try to connect (open bluetooth socket) pairing dialog popup anyway.. Did u experienced similar behaviour? Mostly affected devices are with newer versions of android (let”s say 4.1 and later..).. Thanks

  2. Jorge Cunha says

    Hi, when I click in “pair” button appears an pop-up to put the PIN, it´s possible to put the pin in the code? in one variable?

  3. Miguel says

    I created a button that is on the side of pair button .. and I want that button to connect to the corresponding device.

    How ? Please.

    Thanks.

  4. Tle says

    Hi,

    I tried to follow your code to pair with selected bluetooth device in listview. But it is not working.
    I don’t know how to call pairDevice() in :

    Listview.setOnItemClickListener( new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView parent, View view,
    int position, long id)
    {
    // TODO Auto-generated method stub
    //I want to pair device over here
    }
    });

    Plz help, Thanks

  5. Ammy says

    I did not get any Activity Screen of button like pair/unpair device etc.
    I only got a blank activity and my bluetooth is automatically on when app is opening

  6. Adi says

    Hi Lorensius, i would to thank you for this great tutorial. But can you help me expanding this project where it can connect the mobile phone with the listed pair device and send serial data? If you can i would be very grateful to you. I need to complete this for my Final Year Project.

  7. Danny H says

    Hi Lorensius,

    Excellent example you have here! Unfortunately, I seem to be running into some troubles when pairing with an Arduino Bluetooth module. Your software seems to pair properly with the module form the Android side of things, but the module does not seem to acknowledge this. I was wondering if you could give some tips on how to send and receive data via Bluetooth rather than just pair devices.

    Thanks!

  8. Dani says

    Hi, first, a great contribution. I have a question, you can connect to a device with a default PIN (eg 1234) and that the user does not see the next window “Bluetooth pairing request”?

    Thanks!

  9. harsh mehta says

    i want send a enable bluetooth request to all nearby available devices. the reason being is .. i am working on a project called bluetooth proximity marketing and i can’t trigger devices unless these devices have enabled their bluetooth .. so, any code which can be incorporated with this bluetooth Proximity marketing device. which can send the request to enable their bluetooth remotely .. is it possible ?

  10. Alexander says

    Hi, Lorensius .

    I need to connect two devices w/o PIN or any other thing, which requires user’s attantion. One program tryes to connect to same program on other device. I’ve implemented this methods of programmatically pairing, so my programm now not ask the PIN, but still ask the confirmation for pairing.. How i can solve this? Does way to connect w/o PIN exists?

    Thanks.
    Alexander.

  11. issam says

    could you help me connecting device via bluetooth without user confirmation
    help me please i’m realy in a bad situation of not finding it out for my licence project

Leave a Reply

Your email address will not be published. Required fields are marked *