Java 蓝牙低能量安全异常
我正在创建一个应用程序来扫描蓝牙低能设备,并且已经实现了扫描功能,但是,我收到了错误:Java 蓝牙低能量安全异常,java,android,bluetooth,sdk,bluetooth-lowenergy,Java,Android,Bluetooth,Sdk,Bluetooth Lowenergy,我正在创建一个应用程序来扫描蓝牙低能设备,并且已经实现了扫描功能,但是,我收到了错误: 无法启动le扫描-引发SecurityException:java.lang.SecurityException:需要访问\u粗略\u位置或访问\u精细\u位置权限才能获取扫描结果 我在清单中拥有所需的权限,但仍收到此错误。我做了一些研究,了解到SDK版本>23需要某种手动检查权限,这是解决此问题的正确方法还是有更简单的替代方法 package com.example.jake.bluetooth; i
无法启动le扫描-引发SecurityException:java.lang.SecurityException:需要访问\u粗略\u位置或访问\u精细\u位置权限才能获取扫描结果 我在清单中拥有所需的权限,但仍收到此错误。我做了一些研究,了解到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);
}
}
}
我有一个应用程序要使用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>
我有一个应用程序要使用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>
您应该卸载以前的版本:
运行>编辑配置>应用程序>启动前部分(右侧)
创建一个支持渐变的Make添加
:app:uninstallAll
您应该卸载以前的版本:
运行>编辑配置>应用程序>启动前部分(右侧)
创建一个支持渐变的Makeadd
:app:uninstallAll
不,仅将其包含在清单中是不够的。您还必须在运行时明确询问用户。有关更多信息,请参阅。不,仅将其包含在您的清单中是不够的。您还必须在运行时明确询问用户。有关更多信息,请参阅。现在是2020年,但我还是回答了这个问题,因此可能对某些人有帮助。我解决了这个问题,并通过在onCreate
call:requestPermissions(新字符串[]{android.Manifest.permission.ACCESS\u rough\u LOCATION},permission\u REQUEST\u rough\u LOCATION})中添加这行代码来解决它代码>。您只是要求用户允许访问位置。从视觉上看,PERMISSION\u REQUEST\u rough\u LOCATION
的定义如下:private static final int PERMISSION\u REQUEST\u rough\u LOCATION=1代码>现在是2020年,但我还是回答了这个问题,所以也许这对某些人会有帮助。我解决了这个问题,并通过在onCreate
call:requestPermissions(新字符串[]{android.Manifest.permission.ACCESS\u rough\u LOCATION},permission\u REQUEST\u rough\u LOCATION})中添加这行代码来解决它代码>。您只是要求用户允许访问位置。从视觉上看,PERMISSION\u REQUEST\u rough\u LOCATION
的定义如下:private static final int PERMISSION\u REQUEST\u rough\u LOCATION=1代码>发布您的清单文件。发布您的清单文件。嘿,谢谢您的回复,我的清单是相同的,但仍然收到此错误。2018-10-31 15:57:56.630 16185-16198/com.example.jake.bluetooth E/BluetoothLeScanner:无法启动le扫描-引发安全异常:java.lang.SecurityException:需要访问粗略位置或访问精细位置权限才能获得扫描结果尝试添加两个权限:android.permission.ACCESS\u rough\u LOCATION和android.permission.ACCESS\u FINE\u LOCATION之前尝试过,但不起作用,返回相同的错误。你的目标SDK是什么?嘿,谢谢你的回复,我的清单是相同的,但仍然收到此错误。2018-10-31 15:57:56.630 16185-16198/com.example.jake.bluetooth E/BluetoothLeScanner:无法启动le扫描-引发安全异常:java.lang.SecurityException:需要访问粗略位置或访问精细位置权限才能获得扫描结果尝试添加两个权限:android.permission.ACCESS\u rough\u LOCATION和android.permission.ACCESS\u FINE\u LOCATION之前尝试过,但它不起作用,返回相同的错误。您的目标SDK是什么?感谢您的回复,您将如何为Bluetooth LE实现此功能?例如,我要验证什么权限?BLUETOOTH、BLUETOOTH\u ADMIN等访问\u粗略\u位置感谢您的回复,您将如何为BLUETOOTH LE实现此功能?例如,我要验证什么权限?蓝牙、蓝牙管理等访问位置