Monitoring Transport Conditions

If syncing over Bluetooth LE is a critical part of your application you may want to warn the user if they are missing the permission or if the hardware is disabled. DittoKit will help you by reporting conditions via a delegate or callback object.

First, while configuring DittoKit, assign a delegate or a callback to receive notifications.

// Setting up inside a ViewController
ditto = DittoKit()
ditto.delegate = self
ditto.setAccessLicense("...")
ditto.start()
// Setting up inside a ViewController
DITDittoKit *ditto = [[DITDittoKit alloc] init];
ditto.delegate = self;
[ditto setAccessLicense: @"..."];
[ditto start];
// Setting up inside an Activity
val androidDependencies = DefaultAndroidDittoKitDependencies(applicationContext)
val ditto = DittoKit(androidDependencies)
ditto.callback = this
ditto.setAccessLicense("...")
ditto.start()
// Setting up inside an Activity
DefaultAndroidDittoSyncKitDependencies androidDependencies = new DefaultAndroidDittoSyncKitDependencies(getApplicationContext());
DittoSyncKit ditto = new DittoSyncKit(androidDependencies);
ditto.callback = this;
ditto.setAccessLicense("...");
ditto.start();
// Setting up inside Main
DittoSyncKit ditto = new DittoSyncKit();
ditto.SetAccessLicense("...");
ditto.start();

Here we are using either the ViewController or Activity to receive the condition callbacks. Extend the appropriate interface and implement the callback method. Condition events may occur multiple times.

DittoKit callbacks come on the background thread. Controlling UI elements, such as making a Toast, requires posting that work onto the main thread on both iOS and Android.

A wide variety of potential problems is described through TransportCondition. Some of them are very unusual, or not applicable on some platforms. Handle only the cases that you are interested in.

extension ViewController: DittoKitDelegate {
    func transportConditionDidChange(transportID: Int64, condition: TransportCondition) {
        if condition == .BleDisabled {
            print("BLE disabled")
        } else if condition == .NoBleCentralPermission {
            print("Permission missing for BLE")
        } else if condition == .NoBlePeripheralPermission {
            print("Permission missing for BLE")
        }
    }
}
@interface ViewController () <DITDittoKitDelegate>

@end

@implementation ViewController

- (void)transportConditionChanged:(enum DITTransportCondition)condition forSubsystem:(enum DITConditionSource)source {
    if (condition == DITTransportConditionBleDisabled) {
        NSLog(@"BLE disabled");
    } else if (condition == DITTransportConditionNoBleCentralPermission) {
        NSLog(@"Permission missing for BLE");
    } else if (condition == DITTransportConditionNoBlePeripheralPermission) {
        NSLog(@"Permission missing for BLE");
    }
}

@end
class MainActivity : AppCompatActivity(), DittoKitCallback {
    // ...
    override fun transportConditionDidChange(transportId: Long, condition: TransportCondition) {
        var toastText: String? = null
        if (condition == TransportCondition.TRANSPORT_CONDITION_BLE_DISABLED) {
            toastText = "BLE disabled"
        } else if (condition == TransportCondition.TRANSPORT_CONDITION_NO_BLE_CENTRAL_PERMISSION) {
            toastText = "Permission missing for BLE"
        } else if (condition == TransportCondition.TRANSPORT_CONDITION_NO_BLE_PERIPHERAL_PERMISSION) {
            toastText = "Permission missing for BLE"
        }
        toastText?.let {
            Handler(mainLooper).post {
                Toast.makeText(this, it, Toast.LENGTH_LONG).show()
            }
        }
    } 
}
public class MainActivity extends AppCompatActivity implements DittoSyncKitCallback {
    @Override
    public void transportConditionDidChange(@NotNull DittoTransportCondition condition, @NotNull DittoConditionSource transportId) {
        String toastText = null;
        if (condition == DittoTransportCondition.BleDisabled) {
            toastText = "BLE disabled";
        } else if (condition == DittoTransportCondition.NoBleCentralPermission) {
            toastText = "Permission missing for BLE";
        } else if (condition == DittoTransportCondition.NoBlePeripheralPermission) {
            toastText = "Permission missing for BLE";
        }

        if (toastText != null) {
            String finalToastText = toastText;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(MainActivity.this, finalToastText, Toast.LENGTH_LONG).show();
                }
            });
        }
    }
}

Top