ACCESS_COARSE_LOCATION does not work on Android 6 [duplicate]

天大地大妈咪最大 提交于 2021-02-05 05:27:51

问题


I follow the official example to write this program

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

And this is permission in 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" />

But I still got this exception

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

How to make BLE work on Android 6, the Android version of my device is 6.0.1

Why I added that permission, still got that exception?


回答1:


Andorid M features requesting permissions at runtime. It means that when your app is installed it does not have permission to use Bluetooth yet. User have to manually grant that permission (once per install).

Just follow official guide -> Requesting Permissions at Run Time



来源:https://stackoverflow.com/questions/36195576/access-coarse-location-does-not-work-on-android-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!