Android蓝牙配对安全

use*_*231 6 android android-bluetooth pairing

自从我升级到android 4.2后,当我尝试配对设备时我遇到了问题设备应该配对但现在它说需要across_user_permission.

这是错误日志:

错误:代码3:
java.lang.SecurityException ::
Permission Denial:来自android的广播请求以用户-1运行但是从user0调用; 这需要
android.permission.INTERACT_ACROSS_USERS_FULL或
android.permission.INTERACT_ACROSS_USERS.

在这里我的方法:

public boolean ensurePaired(BluetoothDevice bd) {
    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(bd.getAddress());
    boolean paired = false;

    Log.d(TAG,"Pairing with Bluetooth device with name " + device.getName()+" and address "+device.getAddress());

    try {
        Method m = device.getClass().getMethod("createBond");
        paired = (Boolean) m.invoke(device);                    
    } catch (Exception e) 
    {
        return paired;
    }  
    Log.d("BluetoothPlugin -", "Returning "+ "Result: "+paired);
    return paired;
}
Run Code Online (Sandbox Code Playgroud)

Gil*_*son 1

我会将代码更改为:

public boolean ensurePaired(BluetoothDevice bd) {
  BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(bd.getAddress());

  Log.d(TAG,"Pairing with Bluetooth device with name " + device.getName()+" and address "+device.getAddress());

  if(device.getBondState() != BluetoothDevice.BOND_BONDED){
    device.createBond();
  }
}
Run Code Online (Sandbox Code Playgroud)

createBond 是一个异步调用,它会立即返回。注册 ACTION_BOND_STATE_CHANGED 意图,以便在绑定过程完成时收到通知及其结果。