I'm developing an android app that needs to connect to a Bluetooth-low-energy device. In order of achieving that goal, and following the Android Dev page, I have included the correct permissions in the manifest file. In the mainActivity I'm trying to scan for BLE devices and printing the result on the screen. The code looks like this:
final BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
bluetoothLeScanner.startScan(callback);
// Before 5 seconds, stop the scan and show results.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
bluetoothLeScanner.stopScan(callback);
callback.onBatchScanResults(results);
callback.onScanFailed(2);
callback.onScanResult(3,result);
listOfResults.setText(results.toString());
}
},5000);
- Where:
bluetoothApater
is the BlueoothAdapter needed to perform the operation as it's told in the android page,bluetoothLeScanner
is the object needed to perform LE scan operations,callback
is a Scan call back object,results
is a List < ScanResult >result
is a ScanResult,- and
listOfResults
is text view.
- The problem maybe is in the method used, because according to the Android Official Page, we find three voids to perform with a callback (onBatchScanResult, onScanResult, and onScanFailed), but I only get working onBatchScanResult.
- Why no device is shown? The only thing printed is the name of the activity, the name of the package and the app name.