如何在没有用户确认的情况下以编程方式设置可发现时间? [英] How to programmatically set Discoverable time without user confirmation?

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

问题描述

我经常用这个

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 访问(如上一个答案中已经建议的那样).但是对于需要的人来说,这里是必要的解决方案:

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里面放两个文件,放到代码里面,如图此处.

And then create a new package android.bluetooth, place two files inside IBluetooth.aidl and IBluetoothCallback.aidl and put inside the code as shown here.

这将允许访问标准 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天全站免登陆