2

I am trying to use the RxAndroidBle lib (https://github.com/Polidea/RxAndroidBle). I want the app the start and scan for BLE devices. I want to print the found devices in the LogCat. How can I do this?

RxBleClient rxBleClient;
RxBleScanResult rxBleScanResult;
private Subscription scanSubscription;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   rxBleClient = RxBleClient.create(this);
   Subscription scanSubscription = rxBleClient.scanBleDevices().subscribe(
      rxBleScanResult.getBleDevice().getMacAddress());
}
Dariusz Seweryn
  • 3,212
  • 2
  • 14
  • 21
amanda45
  • 535
  • 10
  • 29

2 Answers2

1

from http://polidea.github.io/RxAndroidBle/ .

Subscription scanSubscription = rxBleClient.scanBleDevices().subscribe(
        rxBleScanResult -> {
            // Process scan result here.
            Log.e("MainActivity","FOUND :"+ rxBleScanResult.getBleDevice().getName());
        },
        throwable -> {
            // Handle an error here.
        }
    );

// When done, just unsubscribe.
scanSubscription.unsubscribe();

Edit: What I have noticed, that this breaks the Scan. Even something like comparing if BleScanResult.getBleDevice().getName().equals("BleName") breaks the scan. It just returns like 3 or 5 devices and then nothing else comes.

Edit 2: I'll leave the previous edit. Someone probably will have the same issue. Some phones (LG G4 Android 6) return null for some bluetooth devices. But some others (Samsung J5 Android 6) do not return a null value. That's what make me look for bug in different place. But its simple , just add

if(BleScanResult.getBleDevice().getName()!=null) 

and now it doesn't break the scan anymore.

paakjis
  • 369
  • 4
  • 16
0

In kotlin you can do:

Disposable scanSubscription = rxBleClient.scanBleDevices(
        new ScanSettings.Builder()
            // .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) // change if needed
            // .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES) // change if needed
            .build()
        // add filters if needed
)
    .subscribe(
        scanResult -> {
            // Process scan result here.
            Log.v(TAG,"Ble device address: " it.bleDevice.macAddress
        },
        throwable -> {
            // Handle an error here.
        }
    );

// When done, just dispose.
scanSubscription.dispose();

companion object {
   const val TAG = "your_tag_here"
}
Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64