ACCESS_COARSE_LOCATION 在 Android 6 上不起作用 [重复]

Posted

技术标签:

【中文标题】ACCESS_COARSE_LOCATION 在 Android 6 上不起作用 [重复]【英文标题】:ACCESS_COARSE_LOCATION does not work on Android 6 [duplicate] 【发布时间】:2016-07-11 18:30:45 【问题描述】:

我按照官方的例子来写这个程序

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

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

【问题讨论】:

您是否在运行时授予了权限?如果你有然后使用 ACCESS_FINE_LOCATION。不知道为什么,但 ACCESS_COARSE_LOCATION 不起作用 【参考方案1】:

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

按照官方指南 -> Requesting Permissions at Run Time

【讨论】:

你为什么链接到西班牙语翻译? 因为 Google 有时会将我的语言切换为西班牙语,不知道为什么;)已修复。

以上是关于ACCESS_COARSE_LOCATION 在 Android 6 上不起作用 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

Android 应用可以在没有 ACCESS_COARSE_LOCATION 权限的情况下使用蓝牙

缺少 FusedLocationProviderClient.getLastLocation 所需的权限:android.permission.ACCESS_COARSE_LOCATION

ACCESS_COARSE_LOCATION android 权限问题

错误:客户端必须具有 ACCESS_COARSE_LOCATION 或 ACCESS_FINE_LOCATION [重复]

sdk 22 和 25 之间的 ACCESS_FINE_LOCATION 和 ACCESS_COARSE_LOCATION 权限有啥区别吗?

java.lang.SecurityException:需要 ACCESS_FINE_LOCATION 或 ACCESS_COARSE_LOCATION 权限