Skip to content

Manage your devices

To manage your devices and related communication, you have multiple implemententation choices.

If you have a single process application, the easiest solution is to use DeviceManager class. See below for usage.

What to use ? Inter Process Communication Multithreading in service
DeviceManager NO NO
IBinder NO YES
Messenger YES NO
AIDL YES YES

Single process application

You can use the DeviceManager class.

This class can store IoTizeDevice instances across your application and pass it through activities.

Initialize

To make it accessible across your application, you can create a global instance in your Android Application class.

public class MyApplication extends Application {

    /**
     * Global instance of the DeviceManager
     */
    private static DeviceManager deviceManager;

    @Override
    public void onCreate() {
        super.onCreate();
        // Instantiate in onCreate
        deviceManager = DeviceManager.getDefaultInstance();
    }

    @Override
    public void onTerminate() {
        // Do not forget to clear to terminate pending device connections.
        deviceManager.clear();
        super.onTerminate();
    }

    /**
     * Getter for the global instance of DeviceManager
     */
    public static DeviceManager getDeviceManager() {
        return deviceManager;
    }

}

If you need multiple device managers, use method DeviceManager manager = DeviceManager.newInstance().

Otherwise use DeviceManager.getDefaultInstance().

Usage

Each device is stored with a unique identifier that can be used to retrieve it. By default the identifier (or tag) is the serial number of the device, which is unique.

Add a device

IoTizeDevice device = ...;
// With default identifier
MyApplication.getDeviceManager().create(device);

// With custom identifier
MyApplication.getDeviceManager().create(device, "<device-identifier>");

Retrieve device

IoTizeDevice device = MyApplication.getDeviceManager().get("<device-identifier");

Pass it through activity or a fragment

You can use Bundle class to pass the device identifier and get it back in the target activity.

Here an example:

// First class
class Activity1 extends Activiy  {

    public openOtherActivity (){
        IoTizeDevice device = ...;
        // Make sure it is registered in DeviceManager
        if (!MyApplication.getDeviceManager().has(device.getTag())){
            throw new IllegalStateException("You register the device before");
        }
        Intent intent = new Intent(this, Activity2.class);
        intent.putExtra("DEVICE_IDENTIFIER", device.getTag());
        startActivity(intent);
    }
}

// In your activity
class Activity2 extends Activiy {

    @Override
    public void onCreate(){
        String deviceIdentifier = null;
        Intent intent = getIntent();
        if (intent != null){
            deviceIdentifier = integer.getStringExtra("DEVICE_IDENTIFIER", null);
        }
        if (deviceIdentifier == null) {
            throw new IllegalStateException("Device identifier should be passed to this activity");
        }
        IoTizeDevice device = MyApplication.getDeviceManager().get(deviceIdentifier);
        // OK
    }
}

Inter-process communication or multi-threading

This is not available in the API yet.

Refer to Android guide and more particulary on these subjects: