[英]Bluetooth low energy security exception
I'm creating an application to scan for Bluetooth low energy devices and have implemented the scan functionality, however, am getting the error:我正在创建一个应用程序来扫描低功耗蓝牙设备并实现了扫描功能,但是,出现错误:
fail to start le scan - SecurityException thrown: java.lang.SecurityException: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results无法启动文件扫描 - 抛出 SecurityException:java.lang.SecurityException:需要 ACCESS_COARSE_LOCATION 或 ACCESS_FINE_LOCATION 权限才能获取扫描结果
I have the required permissions in my manifest, however, am still getting this error.我的清单中有所需的权限,但是,仍然收到此错误。 I have done some research and read that SDK versions > 23 require some kind of manual checking for permissions, is this the correct solution to this problem or is there a simpler alternative?我做了一些研究,读到 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);
}
}
}
No it's not enough to just have it in your manifest.不,仅在清单中包含它是不够的。 You must also explicitly ask the user at runtime.您还必须在运行时明确询问用户。 See https://developer.android.com/training/permissions/requesting for more info.有关更多信息,请参阅https://developer.android.com/training/permissions/requesting 。
It's 2020, but I respond to this question anyway so maybe it will be helpfull for someone.现在是 2020 年,但无论如何我都会回答这个问题,所以也许它会对某人有所帮助。 I encauntered this problem and I resolved it with adding this line of code in onCreate
call: requestPermissions(new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
我遇到了这个问题,并通过在onCreate
调用中添加这行代码来解决它: requestPermissions(new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
. . You're just asking to the user to allow access location.您只是要求用户允许访问位置。 Obvisully PERMISSION_REQUEST_COARSE_LOCATION
is defined like: private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;
显然PERMISSION_REQUEST_COARSE_LOCATION
定义如下: private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;
. .
I've a app to use BLE and this is my AndroidManifest.xml
file, all works fine.我有一个使用 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>
You should uninstall previous build:您应该卸载以前的构建:
Run > Edit Configurations > Application > Before launch section(on the right)
Create a gradle-aware Make
add :app:uninstallAll
Run > Edit Configurations > Application > Before launch section(on the right)
创建一个gradle-aware Make
add :app:uninstallAll
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.