My app is scanning a Bluetooth 4.0 advertising device (TI SensorTag) via the new Lollipop API BluetoothLeScanner.
I've got a strange behavior: On certain devices (Samsung Tab4, Sony Xperia M2), the callback void onScanResult(int callbackType, ScanResult result) is only called 1 time during the advertising phase. On other devices (Samsung Galaxy Tab A, Acer Iconia One 10), the callback is always called: each time I got an advertising packet.
My straightforward code:
public class BluetoothMgr {
private static BluetoothMgr mBluetoothMgr = null;
private BluetoothAdapter mBluetoothAdapter;
private BluetoothLeScanner mBluetoothLeScanner;
private BluetoothMgr(){
BluetoothManager manager = (BluetoothManager) DeviceLiftApplication.getContext().getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = manager.getAdapter();
mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
}
public static synchronized BluetoothMgr getInstance(){
if(mBluetoothMgr == null){
mBluetoothMgr = new BluetoothMgr();
}
return mBluetoothMgr;
}
public void startScan() {
ScanFilter filter = new ScanFilter.Builder()
//.setServiceUuid(Device.THERM_SERVICE)
.build();
ArrayList<ScanFilter> filters = new ArrayList<ScanFilter>();
filters.add(filter);
ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.build();
mBluetoothLeScanner.startScan(filters, settings, mScanCallback);
}
private ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
// This is called 1 or several times depending on devices!!
processResult(result);
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
for (ScanResult result : results) {
processResult(result);
}
}
@Override
public void onScanFailed(int errorCode) {
}
private void processResult(ScanResult result) {
}
};
}