仅在Android Marshmallow中未调用BluetoothAdapter.LeScanCallback中的扫描
我刚刚更新了Nexus 5,我的应用程序的功能已经停止工作 原因是接口仅在Android Marshmallow中未调用BluetoothAdapter.LeScanCallback中的扫描,android,bluetooth,android-6.0-marshmallow,Android,Bluetooth,Android 6.0 Marshmallow,我刚刚更新了Nexus 5,我的应用程序的功能已经停止工作 原因是接口lescallback没有调用函数onLeScan。在下面的代码中,我使用了一些不推荐使用的代码,但我需要这样做,因为我正在测试一些技术。以下是启动蓝牙适配器的方式: @Override protected void onStart() { super.onStart(); // Check if the device has BLE if (!this.getPackageManager().has
lescallback
没有调用函数onLeScan
。在下面的代码中,我使用了一些不推荐使用的代码,但我需要这样做,因为我正在测试一些技术。以下是启动蓝牙适配器的方式:
@Override
protected void onStart()
{
super.onStart();
// Check if the device has BLE
if (!this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))
{
bleNotSupported();
return;
}
// Initializes a Bluetooth adapter.
final BluetoothManager bluetoothManager = (BluetoothManager)this.getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled())
{
bleNotEnabled();
/*
* Bluetooth can be enabled in app:
*
* Intent btIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
* btIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
* mAppContext.startActivity(btIntent);
*/
return;
}
boolean bluetoothScanning = mBluetoothAdapter.startLeScan(mScanCallback); // this is true as well
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onStop() {
super.onStop();
if (mBluetoothAdapter != null)
{
mBluetoothAdapter.stopLeScan(mScanCallback);
}
progressBar.setVisibility(View.GONE);
mDeviceAdapter.clearList();
}
BluetoothAdapter.LeScanCallback mScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord)
{
// if (previousBluetoothSelected == null)
mDeviceAdapter.refreshList(device);
if (device.getAddress().equalsIgnoreCase(previousBluetoothSelected)) {
selectDeviceAndGo(device);
mBluetoothAdapter.stopLeScan(this);
}
}
};
并且它在API<23的每个设备中工作,但在API=23的情况下不工作
原因可能是什么
事先非常感谢
问候
Rafael。Nexus 5的一些带有棉花糖的手机上有一个bug。在某些手机中,如果gps关闭,扫描将无法启动 这不是Nexus5的bug,而是棉花糖的工作原理。蓝牙扫描或WiFi扫描需要打开位置服务,并且您的应用程序必须声明访问精细位置或粗略位置。。github altbeacon正在对此进行讨论。@MaorHadad我看不出重点,因为我说过这不是nexus 5的问题,即使在nexus 9上使用mra58k也存在同样的“问题”。在MM的最终版本中,你需要位置服务。这太疯狂了!定位服务应该开启以执行蓝牙扫描(这会浪费电池)是毫无意义的,因此无论是否有意,我都认为这是一个bug。你救了我的命,我花了2个小时来找出为什么我没有得到任何结果。