Search code examples
androidandroid-bluetoothrx-androidrxandroidble

RxAndroidBle - RxBleDevice getName() always returns null


Having the following code

rxBleClient = RxBleClient.create(this);
        scanSubscription = rxBleClient.scanBleDevices(
                new ScanSettings.Builder()
                        .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
                        .build())
                .subscribe(new Observer<ScanResult>() {
                               @Override
                               public void onCompleted() {
                                   Log.d(TAG_BT, "Scan completed");
                               }

                               @Override
                               public void onError(Throwable e) {
                                   Log.d(TAG_BT, "Scan onError", e);
                               }

                               @Override
                               public void onNext(ScanResult scanResult) {
                                   RxBleDevice device = scanResult.getBleDevice();
                                   Log.d(TAG_BT, "Scan - " + device.getName());
                                   if (device.getName() != null &&
                                           device.getName().contains(
                                                   SMParameters.SM_BLUETOOTH_SSID_PREFIX)) {
                                       connectToDevice(device);
                                   }
                               }
                           }
                );

The results I get on every onNext() event are the following:

07-05 13:06:24.065 24390-24390/com.example.app D/TAG_BT: Scan - null
07-05 13:06:24.416 24390-24390/com.example.app D/TAG_BT: Scan - null
07-05 13:06:25.670 24390-24390/com.example.app D/TAG_BT: Scan - null
07-05 13:06:25.706 24390-24390/com.example.app D/TAG_BT: Scan - null
07-05 13:06:26.930 24390-24390/com.example.app D/TAG_BT: Scan - null
07-05 13:08:18.339 26550-26550/com.example.app D/TAG_BT: Scan - null
07-05 13:08:19.567 26550-26550/com.example.app D/TAG_BT: Scan - null
07-05 13:08:24.810 26550-26550/com.example.app D/TAG_BT: Scan - null
07-05 13:08:25.981 26550-26550/com.example.app D/TAG_BT: Scan - null
07-05 13:08:26.024 26550-26550/com.example.app D/TAG_BT: Scan - null
07-05 13:08:26.027 26550-26550/com.example.app D/TAG_BT: Scan - null
07-05 13:08:26.029 26550-26550/com.example.app D/TAG_BT: Scan - null
07-05 13:08:26.042 26550-26550/com.example.app D/TAG_BT: Scan - null
07-05 13:08:26.098 26550-26550/com.example.app D/TAG_BT: Scan - null
07-05 13:08:26.101 26550-26550/com.example.app D/TAG_BT: Scan - null
07-05 13:08:26.123 26550-26550/com.example.app D/TAG_BT: Scan - null
07-05 13:08:26.130 26550-26550/com.example.app D/TAG_BT: Scan - null
07-05 13:08:27.246 26550-26550/com.example.app D/TAG_BT: Scan - null
07-05 13:08:28.508 26550-26550/com.example.app D/TAG_BT: Scan - null

Do I miss any configuration parameter? No error is thrown so if anyone has any clue...

Thanks!


Solution

  • BluetoothDevice may have a null name if it is not broadcasted. You may alternatively check for a name from scanResult.getScanRecord().getDeviceName() though it also can be null.

    This question was answered already here or here.