我正在尝试使用函数BluatoothLeScanner.startScan,而不是不推荐的BluetoothAdapter.startLeScan。昨天我把我的Nexus 5升级到了Android6.0,从那以后,我的应用程序就不再工作了。我首先添加了所需的首选项( ACCESS_COARSE_LOCATION ),如下所示,https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id。然后,我添加了如下所述的权限:https://developer.android.com/training/permissions/requesting.html。但到了最后,它似乎不起作用了,它没有把可再生的设备送回去。
这是我的密码:
清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stm.sensitronapp">
<uses-sdk android:maxSdkVersion="23"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>`DeviceScanActivity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED){
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_COARSE_LOCATION)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_COARSE);
}
}
// Device scan callback.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
mLeDeviceListAdapter.addDevice(result.getDevice());
mLeDeviceListAdapter.notifyDataSetChanged();
}
};
}
}
}
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
if (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON) {
mSwipeRefreshLayout.setRefreshing(true);
mLeDeviceListAdapter.clear();
mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
if(ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION ) == PackageManager.PERMISSION_GRANTED) {
mBluetoothLeScanner.startScan(mScanCallback);
}
}编辑:为了解决这个问题,我只打开了GPS。在这边请中以编程的方式实现它很容易。
发布于 2016-01-29 03:51:17
如果已授予权限,请尝试:打开GPS。
发布于 2015-10-10 16:42:49
您的应用程序是否提示在启动时获得位置许可?如果不是,则在其他地方处理代码,以便提示代码。
此外,您还可以检查这个以测试您的应用程序是否运行良好:
打开设置>应用程序> YourApplication >权限并启用位置,然后尝试扫描结果。
只有在清单上提供了ACCESS_COARSE_LOCATION时,才会在权限下列出位置。
发布于 2016-01-21 13:22:16
使用上面提供的解决方案是可行的,但是副作用是您必须为不需要它的东西打开位置服务。一个丑陋和令人不满意的工作是指定您的清单中的目标版本
android:targetSdkVersion="21“
它允许扫描我的Nexus 7,即使安装的版本是6.0.1。我不知道针对比安装版本更低版本的副作用是什么,但至少扫描工作。可能是无GPS设备的唯一解决方案(如果存在这种设备)。
谷歌应该为此受难。
https://stackoverflow.com/questions/33013818
复制相似问题