问题
I've just updated my Nexus 5 and my app's feature has stopped working.
The reason is that the interface LeScanCallback
is not calling the function onLeScan
. On the below code, I'm using some deprecated code, but I need to do it since I'm testing some technology. This is how I start my bluetooth adapter:
@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);
}
}
};
and it works in each device with API < 23, but not in API = 23.
What could be the reason?
Thank you very much in advance.
Regards.
Rafael.
回答1:
There is A bug on some of the Nexus 5 phones with Marshmallow. in some phones the scan wont start if your gps is off.
来源:https://stackoverflow.com/questions/33257930/onlescan-from-bluetoothadapter-lescancallback-not-called-in-android-marshmallow