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.
(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
[ad#ad-720×90]







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
Hi Ivan, i never encountered that issue. Have you tried to check the bond state before making the connection (device.getBondState())? Is the state BONDED_NONE though it is listed on paired devices?
That is the funny thing.. When I check state is bonded but when I try to open bluetooth socket pairing dialog come… :S
Hmm, that’s weird..hehe
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?
Well it doesn’t work here either, tried in tablets with 4.0 and 4.1.
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.
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
Hey, evTen i have d same problem.. could u get any solution for it?
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
sorry I was wrong… Please delete this comment.
Hi Lorensius,
I want to pair a device that only supports 4 digit PINs ( an ion Icade see: http://forum.xda-developers.com/showthread.php?t=2267109 ).
Is it possible, to force android to use a 4 digit PIN instead of the random generated 6 digit PIN?
If yes, do you know how your code has to be modified to accomplish this?
Thanks!
Best
Matthias
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.
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!
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!
Excellent, this is exactly what I’ve been looking for, thanks.
Hey Bro i want this code plz send me this code on the mail is.
Thanks to this sir Lorenz 😉
Thumbs up!
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 ?
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.
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
Thanks for the tutorial. It was very helpful.
–Mitch
http://codingwithmitch.com/
https://www.youtube.com/channel/UCoNZZLhPuuRteu02rh7bzsw
Hi
how to automatically pair the bluetooth device without popup dialog
Hi
How to do android pairing bluetooth devices without dialog with pin