Android lollipop java.lang.SecurityException:用户和当前进程都没有android.permission.BLUETOOTH_PRIVILEGED

我目前正在开发一个 Android应用程序,它使用Eclipse(Java)通过蓝牙连接到设备.目前我在Android 4.4(Kit-Kat)及以下版本上工作,但在Android 5(Lollipop)的新更新之后.发生安全异常.

表现:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
<uses-feature android:name="android.hardware.bluetooth_le"
        android:required="false" />

码:

mBleScanner.startScan(filters,scanSettings,mLeScanCallback);

private ScanCallback mLeScanCallback = new ScanCallback()
{
    @Override
    public void onScanResult(int callbackType,ScanResult result)
    {
        BluetoothDevice device = result.getDevice();
        if(device.getName() != null)
        {
            if(device.getName().toUpperCase().contains("MyDevice"))
            {

                mBleScanner.stopScan(mLeScanCallback);

                if (device.getBondState() == BluetoothDevice.BOND_BONDED)
                {
                    Connect(device.getAddress().toString()); 
                }
                else
                {
                    // pair device
                    device.setPairingConfirmation(true);
                    device.createBond();
                }
            }
        }
    }
};
...
..
.
private void BondDevice(BluetoothGattCharacteristic bgc,boolean pnEnable)
{
    boolean bool = gatt.setCharacteristicNotification(bgc,true); // this line throw security exception
    BluetoothGattDescriptor bgd=bgc.getDescriptor(UUID.fromString(BLE_DESCRIPTOR_NOTIFY));

    byte[] arrayOfByte = pnEnable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE:
                      BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
    gattDescriptor.setValue(arrayOfByte);
    mBluetoothGatt.writeDescriptor(bgd);
}

堆栈跟踪:

04-27 12:36:24.559: W/BluetoothGatt(17764): Unhandled exception in callback
04-27 12:36:24.559: W/BluetoothGatt(17764): java.lang.SecurityException: Need BLUETOOTH_PRIVILEGED permission: Neither user 10215 nor current process has android.permission.BLUETOOTH_PRIVILEGED.
04-27 12:36:24.559: W/BluetoothGatt(17764):     at android.os.Parcel.readException(Parcel.java:1540)
04-27 12:36:24.559: W/BluetoothGatt(17764):     at android.os.Parcel.readException(Parcel.java:1493)
04-27 12:36:24.559: W/BluetoothGatt(17764):     at android.bluetooth.IBluetoothGatt$Stub$Proxy.registerForNotification(IBluetoothGatt.java:1163)
04-27 12:36:24.559: W/BluetoothGatt(17764):     at android.bluetooth.BluetoothGatt.setCharacteristicNotification(BluetoothGatt.java:1239)
04-27 12:36:24.559: W/BluetoothGatt(17764):     at com.vibease.ap3.service.ServiceBLE.BondDevice(ServiceBLE.java:568)
04-27 12:36:24.559: W/BluetoothGatt(17764):     at com.vibease.ap3.service.ServiceBLE.CheckDevice(ServiceBLE.java:518)
04-27 12:36:24.559: W/BluetoothGatt(17764):     at com.vibease.ap3.service.ServiceBLE.access$7(ServiceBLE.java:493)
04-27 12:36:24.559: W/BluetoothGatt(17764):     at com.vibease.ap3.service.ServiceBLE$2.onServicesDiscovered(ServiceBLE.java:373)
04-27 12:36:24.559: W/BluetoothGatt(17764):     at android.bluetooth.BluetoothGatt$1.onSearchComplete(BluetoothGatt.java:309)
04-27 12:36:24.559: W/BluetoothGatt(17764):     at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:217)
04-27 12:36:24.559: W/BluetoothGatt(17764):     at android.os.Binder.execTransact(Binder.java:446)

解决方法

04-27 12:36:24.559: W/BluetoothGatt(17764): java.lang.SecurityException: Need BLUETOOTH_PRIVILEGED permission: Neither user 10215 nor current process has android.permission.BLUETOOTH_PRIVILEGED.

只需添加它要求的权限:

<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />

相关文章

以下为个人理解,如错请评 CE: 凭据加密 (CE) 存储空间, 实际路径/data/user_ce/ DE: 设备加密 (DE) 存...
转载来源:https://blog.csdn.net/yfbdxz/article/details/114702144 用EventLog.writeEvent打的日志(或...
事件分发机制详解 一、基础知识介绍 1、经常用的事件有:MotionEvent.ACTION_DOWN,MotionEvent.ACTION...
又是好久没有写博客了,一直都比较忙,最近终于有时间沉淀和整理一下最近学到和解决的一些问题。 最近进...
Android性能优化——之控件的优化 前面讲了图像的优化,接下来分享一下控件的性能优化,这里主要是面向...
android的开源库是用来在android上显示gif图片的。我在网上查了一下,大家说这个框架写的不错,加载大的...