带有Android 6.0的BluetoothLeScanner.startScan无法发现设备 [英] BluetoothLeScanner.startScan with Android 6.0 does not discover devices
问题描述
我正在尝试使用功能 BluatoothLeScanner.startScan 代替已弃用的功能 BluetoothAdapter.startLeScan . 昨天我将Nexus 5更新为Android 6.0,从那时起,我的应用程序不再起作用. 我首先添加所需的首选项 ACCESS_COARSE_LOCATION ,如在此处找到的 https://developer.android.com/training/权限/requesting.html . 但最后似乎无法正常工作,它不会发送回ble设备.
I'm trying to use the function BluatoothLeScanner.startScan instead of the deprecated one BluetoothAdapter.startLeScan. Yesterday I updated my Nexus 5 to Android 6.0 and since that moment my app does not work anymore. I firstly add the preferences required ACCESS_COARSE_LOCATION as found here, https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id. Then I added the permission as described here: https://developer.android.com/training/permissions/requesting.html. But at the end it seems not working, it does not send back the ble devices.
这是我的代码:
清单
<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.通过这种方式,可以很容易地以编程方式完成.
to solve this problem I only turned on the GPS. It is easy to do it programmatically in this way.
推荐答案
如果授予了权限,请尝试:打开GPS.
if permissions granted, have a try: turn ON the GPS.
这篇关于带有Android 6.0的BluetoothLeScanner.startScan无法发现设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!