Getting Started (Rx module)

Recommended Step:

Download the SweetBlue Toolbox app here. Use the Toolbox to find the settings that work best for the device you're trying to connect to. Once you find settings that work well for you, you can export them to a json file, with which you can then import into SweetBlue (Using new RxBleManagerConfig(jsonData)).

Also recommended:

Do not use any non-Rx prefixed class, unless there is no other alternative. This mainly applies to BleManager, BleDevice, and BleServer. The Rx wrapper classes set listeners on the core classes, so using them directly can cause unintended things to happen (mainly, things will probably stop working). So don't do it.

  • Modify your app's build.gradle file to pull SweetBlue down from our maven server, and add the dependency to your project:


repositories {
    ...
    maven {
        url "https://archiva.sweetblue.io/repository/internal"
    }
}

dependencies { ... implementation "com.idevicesinc:sweetblue:3.0.2" implementation "com.idevicesinc:sweetbluerx:3.0.2" }

  • Create a file called sweetblue_api_key.txt in your app's assets directory. Paste your API key into this new file. Without this file, you will not be able to use SweetBlue in your app. If you try to instantiate RxBleManager without it, a RuntimeException will be thrown.
  • SweetBlue provides a helper class to handle all the permissions, and turning things on for you. If you decide to use this class, you must use it in an Activity, as it shows dialogs.
  • The following example shows how to use the BluetoothEnabler class, along with starting a scan, connecting to the first device seen, then reading it's battery level.


RxBleManagerConfig config = new RxBleManagerConfig();
RxBleManager manager = RxBleManager.get(this, config);
BleSetupHelper.runEnabler(manager.getBleManager(), this, result ->
{
    if (result.getSuccessful())
    {
        // Do whatever you want to do with Bluetooth now.
        scanThenConnectThenRead(manager);
    }
});


// This must be a class field (as opposed to a local variable) for the below to work (calling dispose()).
Disposable scanDisposable;

private void scanThenConnectThenRead(RxBleManager manager) { ScanOptions options = new ScanOptions().scanPeriodically(Interval.TEN_SECS, Interval.ONE_SEC); scanDisposable = manager.scan_onlyNew(options).subscribe(rxBleDevice -> { // Stop scanning now, so we don't end up connecting to every device we see. // You can also just call RxBleManager.stopScan() if you don't want to hold the Disposable // instance in your class. scanDisposable.dispose(); rxBleDevice.connect().subscribe(() -> { BleRead read = new BleRead(Uuids.BATTERY_SERVICE, Uuids.BATTERY_LEVEL); rxBleDevice.read(read).subscribe(readEvent -> { if (readEvent.wasSuccess()) Log.i("", "Battery level is " + readEvent.data_byte() + "%"); }); }, throwable -> { ConnectException cex = (ConnectException) throwable; Log.e("MyApp", "Device failed to connect. Fail Event: " + cex.getEvent()); } ); } }