ACCESS_COARSE_LOCATION在Android 6上不起作用 [英] ACCESS_COARSE_LOCATION does not work on Android 6

查看:105
本文介绍了ACCESS_COARSE_LOCATION在Android 6上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照官方的例子来编写此程序

 公共类BluetoothActivity扩展了AppCompatActivity {私有静态最终long SCANNING_TIMEOUT = 5000;/* 5秒 */私有静态最终int ENABLE_BT_REQUEST_ID = 1;私人BleWrapper mBleWrapper = null;私有布尔值mScanning = false;私有处理程序mHandler = new Handler();专用BluetoothAdapter mBluetoothAdapter;@Override受保护的void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_bluetooth);如果(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)){Toast.makeText(this,"no ble",Toast.LENGTH_SHORT).show();结束();}最终的BluetoothManager bluetoothManager =(蓝牙管理器)getSystemService(Context.BLUETOOTH_SERVICE);mBluetoothAdapter = bluetoothManager.getAdapter();如果(mBluetoothAdapter == null ||!mBluetoothAdapter.isEnabled()){Intent enableBtIntent =新的Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);startActivityForResult(enableBtIntent,1);}scanLeDevice(true);}专用BluetoothAdapter.LeScanCallback mLeScanCallback =新的BluetoothAdapter.LeScanCallback(){@OverrideLeScan上的public void(最终的BluetoothDevice设备,int rssi,byte [] scanRecord){Log.v("ble",device.getName());}};//10秒钟后停止扫描.私有静态最终long SCAN_PERIOD = 3000;private void scanLeDevice(final boolean enable){如果(启用){//在预定义的扫描周期后停止扫描.mHandler.postDelayed(new Runnable(){@Override公共无效run(){mScanning = false;mBluetoothAdapter.stopLeScan(mLeScanCallback);}},SCAN_PERIOD);mScanning = true;mBluetoothAdapter.startLeScan(mLeScanCallback);} 别的 {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:从活页夹存根实现中捕获了RuntimeException.03-24 15:52:01.126 2223-2236/com.test W/Binder:java.lang.SecurityException:需要ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION权限才能获取扫描结果 

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

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

解决方案

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

只需遵循官方指南-> 在运行时请求权限

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?

解决方案

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

这篇关于ACCESS_COARSE_LOCATION在Android 6上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆