蓝牙没有发现在Android中提示 [英] Bluetooth discovery without prompt in Android

查看:166
本文介绍了蓝牙没有发现在Android中提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够打开/关闭蓝牙而不使用以下code任何提示。它要求蓝牙 BLUETOOTH_ADMIN 的权限。

I am able to turn on/off Bluetooth without any prompt using the following code. It requires BLUETOOTH and BLUETOOTH_ADMIN permissions.

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

但没有发现任何方式将蓝牙可发现,无用户提示。它的连接,以提示用户每一次。有没有不要再问我功能,我害怕。有没有使蓝牙设备发现什么好的办法?我不关心的时间。也是我的设备没有扎根。

But didn't find any way to set Bluetooth discoverable without user prompt. It's wired to prompt every time to user. There is no "don't ask me again" feature I afraid. Is there any good way to make Bluetooth device discoverable? I don't care about the duration. Also my device is not rooted.

我发现的来源$ C ​​$ C <一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.2_r1/android/bluetooth/BluetoothAdapter.java#BluetoothAdapter\"相对=nofollow> BluetoothAdapter.java 并已任命一个公共的方法 setDiscoverableDuration 。但是,为什么我不能访问呢?为什么有些公共方法隐藏在阿比单证?怎么他们甚至做到这一点?所有的方法都是公开的。

I found source code of BluetoothAdapter.java and it has a public method named setDiscoverableDuration. But why I can't access it? Why some public methods are hidden in Api documentations? How did they even do that? all methods are public.

推荐答案

最后,我已经找到一种方法来做到这一点使用反射。

Finally I have found a way to do this using reflection.

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中提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆