其他分享
首页 > 其他分享> > ACCESS_COARSE_LOCATION在Android 6上不起作用

ACCESS_COARSE_LOCATION在Android 6上不起作用

作者:互联网

这个问题已经在这里有了答案:            >            Android permission doesn’t work even if I have declared it                                    12个
我按照官方的例子来写这个程序

public class BluetoothActivity extends AppCompatActivity {
    private static final long SCANNING_TIMEOUT = 5000; /* 5 seconds */
    private static final int ENABLE_BT_REQUEST_ID = 1;
    private BleWrapper mBleWrapper = null;
    private boolean mScanning = false;
    private Handler mHandler = new Handler();
    private BluetoothAdapter mBluetoothAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bluetooth);
        if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            Toast.makeText(this, "no ble", Toast.LENGTH_SHORT).show();
            finish();
        }
        final BluetoothManager bluetoothManager =
                (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();
        if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, 1);
        }
        scanLeDevice(true);


    }
    private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {
                @Override
                public void onLeScan(final BluetoothDevice device, int rssi,
                                     byte[] scanRecord) {
                    Log.v("ble",device.getName());
                }
            };
    // Stops scanning after 10 seconds.
    private static final long SCAN_PERIOD = 3000;
    private void scanLeDevice(final boolean enable) {
        if (enable) {
            // Stops scanning after a pre-defined scan period.
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mScanning = false;
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                }
            }, SCAN_PERIOD);

            mScanning = true;
            mBluetoothAdapter.startLeScan(mLeScanCallback);
        } else {
            mScanning = false;
            mBluetoothAdapter.stopLeScan(mLeScanCallback);
        }
    }
}

这是manifests.xml中的权限

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

但是我仍然有这个例外

03-24 15:52:01.126 2223-2236/com.test W/Binder: Caught a RuntimeException from the binder stub implementation.
03-24 15:52:01.126 2223-2236/com.test W/Binder: java.lang.SecurityException: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results

如何使BLE在Android 6上工作,我的设备的Android版本为6.0.1

为什么我添加了该权限,但仍然有该异常?

解决方法:

Andorid M功能在运行时请求权限.这意味着在安装您的应用程序后,它尚无使用蓝牙的权限.用户必须手动授予该权限(每次安装一次).

只需遵循官方指南-> Requesting Permissions at Run Time

标签:android-bluetooth,bluetooth-lowenergy,android
来源: https://codeday.me/bug/20191027/1942631.html