2

Please any one help me with pairing my android phone with other discovered phone programmatically ?

Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61

3 Answers3

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
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);


    }
  • 3
    It 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