Please any one help me with pairing my android phone with other discovered phone programmatically ?
Asked
Active
Viewed 2.4k times
2
-
I answered this question on this post: http://stackoverflow.com/a/22201805/1426021 – DragonT Mar 05 '14 at 16:01
3 Answers
5
Found the solution using reflection, I am doing this now as follows and it is working for me:
//For Pairing
private void pairDevice(BluetoothDevice device) {
try {
Log.d("pairDevice()", "Start Pairing...");
Method m = device.getClass().getMethod("createBond", (Class[]) null);
m.invoke(device, (Object[]) null);
Log.d("pairDevice()", "Pairing finished.");
} catch (Exception e) {
Log.e("pairDevice()", e.getMessage());
}
}
//For UnPairing
private void unpairDevice(BluetoothDevice device) {
try {
Log.d("unpairDevice()", "Start Un-Pairing...");
Method m = device.getClass().getMethod("removeBond", (Class[]) null);
m.invoke(device, (Object[]) null);
Log.d("unpairDevice()", "Un-Pairing finished.");
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}

Jonno_FTW
- 8,601
- 7
- 58
- 90

Shridutt Kothari
- 7,326
- 3
- 41
- 61
-
2You will have to find out, as i did this code a long time ago. – Shridutt Kothari Jun 19 '13 at 04:46
-
-
can u please help me for transfer file through bluetooth from one device to another.... – Denny Sharma Oct 23 '13 at 12:47
-
-
1I made a sample project using this solution to pair or unpair, find it here http://www.londatiga.net/it/programming/android/how-to-programmatically-pair-or-unpair-android-bluetooth-device/ – Lorensius W. L. T Feb 20 '14 at 15:52
-
1I try your code but when I click on searched devices its toast incorrect pin. – ParikshitSinghTomar Jun 30 '15 at 12:05
-
as said the code uses reflection, actual code might have changed – Shridutt Kothari Jul 03 '15 at 10:06
4
Can do it like:
public void pairDevice(BluetoothDevice device) {
String ACTION_PAIRING_REQUEST = "android.bluetooth.device.action.PAIRING_REQUEST";
Intent intent = new Intent(ACTION_PAIRING_REQUEST);
String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE";
intent.putExtra(EXTRA_DEVICE, device);
String EXTRA_PAIRING_VARIANT = "android.bluetooth.device.extra.PAIRING_VARIANT";
int PAIRING_VARIANT_PIN = 0;
intent.putExtra(EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivityForResult(intent,0);
}
-
3It is not working, although it launches the pairing activity but further nothing happens after entering passkey. while the same pairing activity, if lauched from settings, it works further after entering passkey... please help... – Shridutt Kothari Feb 12 '13 at 14:10
0
Did you set your permissions in the Android Manifest file?
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

gomezfx
- 147
- 2
- 8
-
Because I've never done anything with Bluetooth, I'm not sure if I can help you further. But I did find this example with source code: [http://www.basic4ppc.com/forum/basic4android-getting-started-tutorials/14768-android-bluetooth-bluetoothadmin-tutorial.html](http://www.basic4ppc.com/forum/basic4android-getting-started-tutorials/14768-android-bluetooth-bluetoothadmin-tutorial.html) – gomezfx Feb 12 '13 at 17:55
-
have already seen this link. not helpful because it dont have anything to do with bluetooth pairing. – Shridutt Kothari Feb 12 '13 at 18:00