gpt4 book ai didi

android - Android 中没有提示的蓝牙发现

转载 作者:太空宇宙 更新时间:2023-11-03 11:48:53 25 4
gpt4 key购买 nike

我可以使用以下代码在没有任何提示的情况下打开/关闭蓝牙。它需要 BLUETOOTHBLUETOOTH_ADMIN 权限。

boolean isEnabled = bluetoothAdapter.isEnabled();
if (enable && !isEnabled) {
return bluetoothAdapter.enable();
} else if (!enable && isEnabled) {
return bluetoothAdapter.disable();
}

但没有找到任何方法可以在没有用户提示的情况下将蓝牙设置为可发现。它每次都会提示用户。恐怕没有“不要再问我”功能。有没有什么好方法可以让蓝牙设备被发现?我不在乎持续时间。我的设备也没有 Root 。

更多信息

我找到了 BluetoothAdapter.java 的源代码它有一个名为 setDiscoverableDuration 的公共(public)方法。但为什么我无法访问它?为什么有些公共(public)方法隐藏在Api文档中?他们是怎么做到的?所有方法都是公开的。

最佳答案

最后我找到了一种使用反射来做到这一点的方法。

Method method;
try {
method = bluetoothAdapter.getClass().getMethod("setScanMode", int.class, int.class);
method.invoke(bluetoothAdapter,BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE,120);
Log.e("invoke","method invoke successfully");
}
catch (Exception e){
e.printStackTrace();
}

警告:以上方法试图调用隐藏方法。所以将来可能会行不通。

关于android - Android 中没有提示的蓝牙发现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30245999/

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