我刚刚更新了Nexus 5,但我的应用功能已停止运行。
原因是接口LeScanCallback
没有调用函数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
可能是什么原因?
提前非常感谢您。
问候。
拉斐尔
最佳答案
某些配备棉花糖的Nexus 5手机上存在错误。在某些手机中,如果您的gps已关闭,则扫描不会开始。
关于android - Android棉花糖中未调用BluetoothAdapter.LeScanCallback上的onLeScan,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33257930/