【问题标题】:Android Bluetooth pairing SecurityAndroid 蓝牙配对安全
【发布时间】:2014-02-18 14:45:06
【问题描述】:

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

这是错误日志:

错误:代码 3:
java.lang.SecurityException::
权限拒绝:来自 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;
}

【问题讨论】:

    标签: android android-bluetooth pairing


    【解决方案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();
      }
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-21
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多