我很难从BLE扫描仪得到扫描结果。我的AndroidManifest.xml中有适当的权限(AndroidManifest.xml),详见下文,但得到一个异常,表明我需要所拥有的权限。不足为奇的是,扫描程序的回调从未被调用。
W/Binder: Caught a RuntimeException from the binder stub implementation.
java.lang.SecurityException: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results. I'm not sure why the exception is thrown but I certianly DON'T get scan call backs.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
ScanCallback btScanCallback = new ScanCallback()
{
@Override
public void onScanResult(int callbackType, final ScanResult result)
{
BluetoothDevice btDevice = result.getDevice();
Log.d(LOGTAG, "Found BLE device: " + btDevice.getName());
// Remove the device from the scanner select view if its there already
for( int i=0; i<btDeviceNameList.size(); i++)
{
String aDevice = btDeviceNameList.get(i);
if(aDevice.equalsIgnoreCase(btDevice.getName()))
{
btDeviceNameList.remove(i);
btDeviceList.remove(i);
break;
}
}
// Add the device to the scanner select view
btDeviceList.add(btDevice);
btDeviceNameList.add(btDevice.getName());
btListAdapter.notifyDataSetChanged();
}
@Override
public void onScanFailed(int errorCode)
{
Log.e(LOGTAG, "BT scan error: " + errorCode);
}
};发布于 2018-06-14 09:02:35
我遇到过你所面临的同样的情况。如果您使用的是Android6.0或更高的。必须在运行时请求位置权限。在获得蓝牙适配器之后,让我们按照下面的代码行请求位置权限。当您的应用程序运行时,将显示一个对话框来询问您是否同意共享您的位置。
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001);https://stackoverflow.com/questions/50823501
复制相似问题