I am now using the new BLE api in android developing.
Basic idea is using bluetooth scanning result to inflate the recyclerview(list);
I followed the BLE guide on google developer
Now I have two problem:
1. onBatchScanResults
listener is never triggered, butonScanResult
works well, is that because the scanner only sense 1 sensor nearby?
- my BLE scanner is much slower compared with other applications.
The following is the two core functions' code snippet.
private void scanBLE(boolean enable) {
final BluetoothLeScanner mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
if (enable) {
mScanning = true;
mBluetoothLeScanner.startScan(mScanCallback);
} else {
if (mScanning) {
mScanning = false;
mBluetoothLeScanner.stopScan(mScanCallback);
}
}
Log.i(TAG, "now the scanning state is" + mScanning);
}
// Device scan callback.
private ScanCallback mScanCallback =
new ScanCallback() {
public void onScanResult(int callbackType, android.bluetooth.le.ScanResult result) {
addBeaconTolist(result, beaconsList);
mAdapter.notifyDataSetChanged();
};
public void onScanFailed(int errorCode) {
Log.i(TAG, "error code is:" + errorCode);
};
public void onBatchScanResults(java.util.List<android.bluetooth.le.ScanResult> results) {
Log.i(TAG, "event linstener is called!!!!");
Log.i(TAG, "batch result are:" + results);
beaconsList.clear();
for (int i = 0; i < results.size(); i++) {
ScanResult result = results.get(i);
addBeaconTolist(result, beaconsList);
}
mAdapter.notifyDataSetChanged();
};
};
in MainFragment is like following:
beaconsList = new ArrayList<BeaconsInfo>();
mAdapter = new BeaconsAdapter(beaconsList);
mRecyclerView.setAdapter(mAdapter);
scannBLE(true);