gpt4 book ai didi

android - 如何在 Android 中显示蓝牙设备选择器?

转载 作者:太空狗 更新时间:2023-10-29 13:47:45 29 4
gpt4 key购买 nike

我喜欢启用蓝牙时弹出的快速选择对话框。但它并不总是显示出来。我想以编程方式显示此对话框。

enter image description here

我能得到的最接近的是:

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

但是,如您所见,存在一些细微差别。但最大的问题是,当您选择一个设备时,对话框会关闭而不会尝试连接到该设备。有错误:

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

好消息是,它在关闭前仍会发送 DEVICE_SELECTED Intent ,因此我仍然可以通过编程方式连接到它。但是真的很脏。我想使用原生 android 中的自包含 LocalBluetoothMonitor,它可以自行扫描、连接、配对,而无需更改 Activity 。

最佳答案

BluetoothScanDialog 似乎无法连接到设备,只能显示附近的设备。这不是您的代码错误。

好处是,如果您有 DEVICE_SELECTED,则无需太多代码即可完成这项工作。您应该能够使用以下内容:

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

关于android - 如何在 Android 中显示蓝牙设备选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51585530/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com