如何编程设置,无需用户确认可发现时间? [英] How to programmatically set Discoverable time without user confirmation?

查看:270
本文介绍了如何编程设置,无需用户确认可发现时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常使用这种

private void ensureDiscoverable() {
    if(D) Log.d(TAG, "ensure discoverable");
    if (mBluetoothAdapter.getScanMode() !=
            BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        startActivity(discoverableIntent);
    }
}

不过,系统将提示用户确认。有没有一种方法可以绕过这个编程?

But that prompts a user confirmation. Is there a way to bypass this programmatically?

另外,我想有没有新闻上的永远发现模式

Also, I suppose there are no "news" on the "always discoverable mode" ?

推荐答案

经过一番研究,我得出的结论是设定发现超时而无需用户交互,这只是可能的root访问权限(如previous答案已经建议)。然而,对于人谁需要,这里是必要的解决方案:

After some research I concluded that setting discoverable timeout without user interaction it's only possible with root access (as already suggested in the previous answer). However for someone who need that here is the necessary solution:

private void ensureBluetoothDiscoverability() {
    try {
        IBluetooth mBtService = getIBluetooth();
        Log.d("TESTE", "Ensuring bluetoot is discoverable");
        if(mBtService.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
            Log.e("TESTE", "Device was not in discoverable mode");
            try {
                mBtService.setDiscoverableTimeout(100);
                // mBtService.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, 1000);
            } catch(Exception e) {
                Log.e("TESTE", "Error setting bt discoverable",e);
            }
            Log.i("TESTE", "Device must be discoverable");
        } else {
            Log.e("TESTE", "Device already discoverable");
        }
    } catch(Exception e) {
        Log.e("TESTE", "Error ensuring BT discoverability", e);
    }    
}



<uses-permission android:name="android.permission.WRITE_SETTINGS" />  
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

然后创建一个新的包android.bluetooth,将两个文件里面IBluetooth.aidl和IBluetoothCallback.aidl,把里面的code,如图的这里

这将允许访问的功能不可用的标准API,但他们中的一些你需要的权限编写安全设置(上面的注释行,你会得到例外缺乏权限过程/用户)。

This will allow to access functions that are not available on the standard API, but for some of them you will need permission to "write secure settings" (the comment line above is where you will get that exception for lack of permissions of the process/user).

这篇关于如何编程设置,无需用户确认可发现时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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