Previous Topic

Next Topic

Book Contents

Book Index

Creating a Device

After you retrieve the DeviceAdmin service you can create and register a Device with a set of functionalities – Device Items. The Device Items are registered after the Device itself. The creation process finishes when the Device and its Device Items are created and registered in the system. You can learn more about the Device and Device Items characteristics on the Device and Device Item Developer Guides respectively.

The following examples show how you can create devices. These examples use a Device Access Protocol Adapter Demo. For this reason you should install and start it.

By Invoking the DeviceAdmin.OPERATION_CREATE Operation

  1. Construct a Map that contains properties used for the functional item creation. The keys are the supported device creation properties while the values are correct types for each property. The keys of this Map can be following properties:
  2. Invoke the create operation of the DeviceAdmin with the following parameters:

The following simple example shows how to create a Device.

private Device create() {

  HashMap<String, Object> creationProperties = new HashMap<String, Object>(8, 1.0f);

  creationProperties.put(Device.UID, "test:device:UID");

  creationProperties.put(Device.NAME, "test:device:name");

  creationProperties.put(Device.TAGS, Collections.singleton("test:device:tag"));

  creationProperties.put(Device.ADAPTER_NAME, "DA Demo");

  Set<DeviceItemDescriptor> deviceItems = Collections.singleton(

    new DeviceItemDescriptor(StepperMotor.class.getName(), "test:device:item:UID", "test:device:item:name", Collections.singleton("test:device:item:tag"), null));

  creationProperties.put(DeviceAdmin.ITEMS, deviceItems);

  creationProperties.put(DeviceAdmin.ATTRIBUTES, Collections.singletonMap("test:device:attribute:key", "test:device:attribute:value"));

  return deviceAdmin.create(Device.class.getName(), creationProperties);

}

By Invoking the DeviceAdmin.OPERATION_CREATE_DEVICE Operation

  1. Construct a DeviceDescriptor instance. You can view the details on "Creating a DeviceDescriptor instance" section below. The DeviceDescriptor is a FunctionalItem bean. This bean holds the device properties which are used for creation of Device. These properties are:
  2. Invoke the createDevice operation with parameter the constructed DeviceDescriptor.