Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show BluetoothDevicePicker in Android?

I love this quick select dialog that pops up when you enable bluetooth. But it doesn't always show. I'd like to display this dialog programmatically.

enter image description here

The closest I can get is with:

Intent bluetoothPicker = new Intent("android.bluetooth.devicepicker.action.LAUNCH");
        bluetoothPicker.putExtra("android.bluetooth.devicepicker.extra.FILTER_TYPE", 1);
        bluetoothPicker.putExtra("android.bluetooth.devicepicker.extra.NEED_AUTH", false);
        bluetoothPicker.putExtra("android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE", "com.cake.x0a.WoBo");

However, there are some slight differences, as you can see . But the biggest problem is, when you select a device, the dialog closes without attempting to connect to the device. With error:

E/WindowManager: android.view.WindowLeaked: Activity com.android.settings.bluetooth.BluetoothScanDialog has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{428851c0 V.E..... R......D 0,0-640,855} that was originally added here at android.view.ViewRootImpl.(ViewRootImpl.java:467) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:267) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) at android.app.Dialog.show(Dialog.java:289) at com.android.settings.bluetooth.BluetoothScanDialog.initialize(BluetoothScanDialog.java:86) at com.android.settings.bluetooth.BluetoothScanDialog.onPostCreate(BluetoothScanDialog.java:103) at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1156) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2396) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471) at android.app.ActivityThread.access$900(ActivityThread.java:175) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5602) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) at dalvik.system.NativeStart.main(Native Method)

enter image description here

The good news is, it still sends the DEVICE_SELECTED intent before it closes, so I could still connect to it programmatically. But it's really dirty. I want to use the self-contained LocalBluetoothMonitor from native android, which can scan, connect, pair, all on its own without changing activities.

like image 502
x0a Avatar asked Feb 24 '26 01:02

x0a


1 Answers

It seems as if the BluetoothScanDialog can not connect to devices, just show nearby devices. It is not an error with your code.

The good thing is that if you have the DEVICE_SELECTED, it is not much code to finish the job. You should be able to use the following:

private Boolean connect(BluetoothDevice bdDevice) { 
    Boolean bool = false;
    try {
        Class cl = Class.forName("android.bluetooth.BluetoothDevice");
        Class[] par = {};
        Method method = cl.getMethod("createBond", par);
        Object[] args = {};
        bool = (Boolean) method.invoke(bdDevice);//, args);// this invoke creates the detected devices paired.
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bool.booleanValue();
};
like image 83
James Avatar answered Feb 25 '26 14:02

James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!