1

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.
Juan CA
  • 156
  • 4
  • 15

2 Answers2

1

The way I did it was to implement scanCallback() object and overriding the onScanResult() or onBatchScanResults() as needed.

    private ScanCallback callback= new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            super.onScanResult(callbackType, result);

            // handles scan result
        }

        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            super.onBatchScanResults(results);

            // handles batch scan results
            for (ScanResult result : results) {

                // you can iterate through each result like so
            }
        }

        @Override
        public void onScanFailed(int errorCode) {
            super.onScanFailed(errorCode);
            // handles error
        }
    };

You then pass callback inside of stopScan() and startScan()

bluetoothLeScanner.startScan(callback);
bluetoothLeScanner.stopScan(callback);

Also consider using result.getDevice() so you can retrieve the specific data of the device you need instead of a large chunk of information. For example, result.getDevice().getAddress() if you want the address only.

andy
  • 26
  • 4
  • When I run the app printing the information stored in `result`, the app closes suddenly. If I try to print `results` instead of `result`, the app closes equally. Both times I see errors related to null object reference (even when the BLE device is active) – Juan CA Oct 24 '19 at 14:43
  • It looks like you are not getting anything from the scans and your app breaks due to null object references. Try debugging there, do something like ```if(result == null){ }``` or ternary operators like ```String text = result != null ? result.toString() : "no device"``` – andy Oct 24 '19 at 14:56
  • That's a bit strange because of the BLE device is active. I used an app to show BLE devices and it finds mine. Any idea? – Juan CA Oct 25 '19 at 14:42
  • Try looking at @dglozano link. In my case, I added devices to an ArrayList of devices every time my scan found a device. Can you update your post with your new code? It is a bit hard to help you without more context. – andy Oct 25 '19 at 15:35
  • The app needs to connect to some low-energy devices. That's the reason why I need to search for devices and list them in order to select the ones I need. The question by @dglozano wasn't very helpful because the app closes suddenly even if I don't try to print `results`. Anyways, with the solution of the if-else statement, the app runs without closing when I try to print `result`. Another problem is that I don't know the difference between `onBatchResult` and `onScanResult`. – Juan CA Oct 25 '19 at 15:57
  • @dglozano explains ```onBatchResult``` in his answer. Anyway, just override the method ```onScanResult``` and have it log out the devices found. Something like ```Log.i("SCAN", result.toString());```. ```System.out.print()``` would also work. You should see in the ```Run``` terminal at the bottom of Android Studio the results printed out. – andy Oct 25 '19 at 16:06
  • 1
    After some testing, the result is the same. In Diego's (@dglozano) question he proposes the use of RxAndroidBle library, but I would prefer not to use external libraries. The solution, override the method `onScanResult`, doesn't work, because the problem continues beeing the return of `result` as null when at least there're three BLE devices working fine. – Juan CA Oct 27 '19 at 16:21
  • It could be your phone then. Have you tried with a different phone? What are you using right now, what android version? Also show your permissions in Manifest and code. [Check this out](https://stackoverflow.com/questions/35447252/android-ble-how-is-onscanresult-method-being-called-in-scancallback). – andy Oct 28 '19 at 12:44
  • I have downloaded a BLE scan app that had detected the devices I'm interested in. The link isn't useful (i have the correct permissions). I don't know what to do in order to solve the problem. – Juan CA Oct 29 '19 at 23:29
  • ```Unfortunately, if you don't request those permissions, the scan just silently fails``` Have you checked your console? At this point there is not much I can do if you don't provide more context. – andy Oct 30 '19 at 12:22
  • The permissions are this ones ` `, they're the ones needed. There aren't error messages in the Logcat. – Juan CA Nov 06 '19 at 21:45
0

Have you added location permissions in manifest and also check if location permission is granted at runtime? If you havent done this, your results will always be null.

Robby Lebotha
  • 1,211
  • 11
  • 9