Skip to content
Ivan Baranov edited this page Dec 5, 2017 · 5 revisions

To start discovering devices, call rxBluetooth.startDiscovery(). The process is asynchronous and the method will immediately return with a boolean indicating whether discovery has successfully started.

Getting devices found while discovering:

rxBluetooth.observeDevices()
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeOn(Schedulers.computation())
    .subscribe(new Consumer<BluetoothDevice>() {
      @Override public void accept(@NonNull BluetoothDevice bluetoothDevice) throws Exception {
        //
      }
    }));

To create connection with device:

// Use 00001101-0000-1000-8000-00805F9B34FB for SPP service 
// (ex. Arduino) or use your own generated UUID.
UUID uuid = UUID.fromString("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");

rxBluetooth.observeConnectDevice(bluetoothDevice, uuid)
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeOn(Schedulers.io())
    .subscribe(new Consumer<BluetoothSocket>() {
      @Override public void accept(BluetoothSocket socket) throws Exception {
        // Connected to the device, do anything with the socket
      }
    }, new Consumer<Throwable>() {
      @Override public void accept(Throwable throwable) throws Exception {
        // Error occured
      }
    });