Android Enterprise - BluetoothAdapter.startDiscovery() 不开始扫描

Vla*_*nko 6 android bluetooth android-enterprise android-work-profile

我正在使用BluetoothAdapter.startDiscovery()查找特定的蓝牙设备(它是蓝牙 2.0 设备,所以我必须使用BluetoothAdapter.startDiscovery())。我拥有所有必需的权限(蓝牙、位置),并且该应用程序运行良好。

但现在我必须在 Android Enterprise (Work Profile) 下使用这个应用程序。在工作配置文件下,它不会开始扫描。配置文件中的所有限制都被禁用。
经过调查 logcat 我发现了这样几行:

非工作资料日志:

2019-07-29 10:23:13.109 10230-10446/? D/BluetoothAdapterService: startDiscovery() uid = 10126, pid = 6328
2019-07-29 10:23:13.110 10230-10446/? D/BluetoothAdapterService: startDiscovery
Run Code Online (Sandbox Code Playgroud)

工作档案日志:

2019-07-29 10:21:26.536 10230-13473/? D/BluetoothAdapterService: startDiscovery() uid = 1010126, pid = 13390
2019-07-29 10:21:26.536 10230-13473/? W/BluetoothAdapterService: startDiscovery() - Not allowed for non-active user
Run Code Online (Sandbox Code Playgroud)

这是OnePlus 6的日志,但我在三星S10上也观察到了类似的logcat。客户也表示他们在 S9、S8 和某些诺基亚设备(实际上是他们测试过的所有设备)上遇到了同样的问题。

通过 Android 源搜索,我发现以下代码,生成此日志:

    public boolean startDiscovery() {
        if (!Utils.checkCaller()) {
            Log.w(TAG, "startDiscovery() - Not allowed for non-active user");
            return false;
        }
        AdapterService service = getService();
        if (service == null) return false;
        return service.startDiscovery();
    }
Run Code Online (Sandbox Code Playgroud)

其中调用Utils.checkCaller()检查:

public static boolean checkCaller() {
    boolean ok;
    // Get the caller's user id then clear the calling identity
    // which will be restored in the finally clause.
    int callingUser = UserHandle.getCallingUserId();
    int callingUid = Binder.getCallingUid();
    long ident = Binder.clearCallingIdentity();
    try {
        // With calling identity cleared the current user is the foreground user.
        int foregroundUser = ActivityManager.getCurrentUser();
        ok = (foregroundUser == callingUser);
        if (!ok) {
            // Always allow SystemUI/System access.
            int systemUiUid = ActivityThread.getPackageManager().getPackageUid(
                    "com.android.systemui", UserHandle.USER_OWNER);
            ok = (systemUiUid == callingUid) || (Process.SYSTEM_UID == callingUid);
        }
    } catch (Exception ex) {
        Log.e(TAG, "checkIfCallerIsSelfOrForegroundUser: Exception ex=" + ex);
        ok = false;
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
    return ok;
}
Run Code Online (Sandbox Code Playgroud)

因此,startDiscovery() uid = 10126startDiscovery() uid = 1010126不同之处uid,我认为这是由工作的个人资料,这是我认为导致检查造成的(foregroundUser == callingUser)false

最后一个问题:我说这是一个错误是对的吗?如果是 - 有没有办法解决它?如果没有 - 如何在工作配置文件中启用蓝牙经典扫描?