简体   繁体   中英

Android Bluetooth pairing Security

Since I upgraded to android 4.2 I'm having trouble when I try to pair a device The device should be paired but now it says that across_user_permission is required.

Here is the error log :

error:code 3:
java.lang.SecurityException::
Permission Denial: broadcast from android asks to run as user -1 but is calling from user0; this requires
android.permission.INTERACT_ACROSS_USERS_FULL or
android.permission.INTERACT_ACROSS_USERS.

and here my method :

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;
}

I would change the code to:

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 is an asynchronous call, it will return immediately. Register for ACTION_BOND_STATE_CHANGED intents to be notified when the bonding process completes, and its result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM