gpt4 book ai didi

android - 使用 Android 6.0 的 BluetoothLeScanner.startScan 无法发现设备

转载 作者:IT老高 更新时间:2023-10-28 23:05:27 28 4
gpt4 key购买 nike

我正在尝试使用函数 BluatoothLeScanner.startScan 而不是已弃用的 BluetoothAdapter.startLeScan。昨天我将我的 Nexus 5 更新到了 Android 6.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 .但最后它似乎不起作用,它没有发回 ble 设备。

这是我的代码:

list

<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。在 this way 中以编程方式很容易做到这一点.

最佳答案

如果授予权限,请尝试:打开 GPS。

关于android - 使用 Android 6.0 的 BluetoothLeScanner.startScan 无法发现设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33013818/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com