Android: How to pair bluetooth devices programmatically?
Asked Answered
G

3

2

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

Gemoets answered 12/2, 2013 at 14:1 Comment(1)
I answered this question on this post: https://mcmap.net/q/393092/-how-to-programmatically-pair-a-bluetooth-device-on-androidElastance
G
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());
    }
}
Gemoets answered 14/2, 2013 at 8:14 Comment(7)
You will have to find out, as i did this code a long time ago.Gemoets
what is Method class ?Jimmie
can u please help me for transfer file through bluetooth from one device to another....Below
for file transfer see sample in android SDK.Gemoets
I made a sample project using this solution to pair or unpair, find it here londatiga.net/it/programming/android/…Mortify
I try your code but when I click on searched devices its toast incorrect pin.Bodyguard
as said the code uses reflection, actual code might have changedGemoets
F
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);


    }
Faun answered 12/2, 2013 at 14:6 Comment(1)
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...Gemoets
T
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" />
Tampere answered 12/2, 2013 at 14:42 Comment(2)
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.htmlTampere
have already seen this link. not helpful because it dont have anything to do with bluetooth pairing.Gemoets

© 2022 - 2024 — McMap. All rights reserved.