蓝牙低能耗安全例外

时间:2018-10-31 16:18:18

标签: java android bluetooth sdk bluetooth-lowenergy

我正在创建一个应用程序以扫描低功耗蓝牙设备,并实现了扫描功能,但是却出现错误:

无法开始扫描-抛出SecurityException:java.lang.SecurityException:需要ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION权限才能获取扫描结果

我的清单中具有所需的权限,但是仍然出现此错误。我已经进行了一些研究,并阅读到SDK版本> 23需要某种形式的手动检查权限,这是此问题的正确解决方案,还是有更简单的替代方法?

  package com.example.jake.bluetooth;

import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_ENABLE_BT = 1;
    private String ble_not_supported = "BLE not supported";
    private BluetoothManager bluetoothManager;
    private BluetoothAdapter mBluetoothAdapter;
    private Button startScanBtn;
    private boolean mScanning;
    private Handler mHandler;
    private BluetoothAdapter.LeScanCallback mLeScanCallBack;
    private static final long SCAN_PERIOD = 10000;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();
        startScanBtn = findViewById(R.id.start_scan);
        mHandler = new Handler();
        mScanning = true;

        mLeScanCallBack = new BluetoothAdapter.LeScanCallback() {
            @Override
            public void onLeScan(final BluetoothDevice bluetoothDevice, int rssi, byte[] bytes) {

            }
        };

        if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }

        if(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)){
            Toast.makeText(this, ble_not_supported, Toast.LENGTH_SHORT).show();
            finish();
        }

        startScanBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                scanLeDevice(mScanning);
            }
        });

    }

    private void scanLeDevice(final boolean enable){
        if(enable){
            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);
        }
    }

}

3 个答案:

答案 0 :(得分:2)

不,仅将其包含在清单中是不够的。您还必须在运行时明确询问用户。有关更多信息,请参见https://developer.android.com/training/permissions/requesting

答案 1 :(得分:0)

我有一个使用BLE的应用程序,这是我的AndroidManifest.xml文件,一切正常。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bluetoothlowenergy_gatt_sendreceive">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ServiceActivity"></activity>
    </application>

</manifest>

答案 2 :(得分:0)

您应该卸载以前的版本:

Run > Edit Configurations > Application > Before launch section(on the right) 创建一个gradle-aware Make添加:app:uninstallAll