Quantcast
Channel: Lorenz Blog - Web and Mobile Application Tutorial, News, Tips & Trick » Android
Viewing all articles
Browse latest Browse all 19

How to Programmatically Pair or Unpair Android Bluetooth Device

$
0
0

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

The post How to Programmatically Pair or Unpair Android Bluetooth Device appeared first on Lorenz Blog - Web and Mobile Application Tutorial, News, Tips & Trick.


Viewing all articles
Browse latest Browse all 19

Latest Images

Trending Articles





Latest Images