Sorry, you need to enable JavaScript to visit this website.

Commands

Commands

ConnectivityApi allows to send data between devices. The data is represented by Command. By
default, ConnectivityApi does not support any specific Command, therefore support for default commands
needs to be configured manually.

Step 1: To obtain access to default commands module you will need to add following dependency to
your build.gradle file:

implementation("com.tomtom.online:sdk-connectivity-commands:2.4307")

Step 2: Add common commands modules to the builder, as shown below:

val connectivityApi = PriorityConnectivityApiBuilder(applicationContext)
    .addCommunicator(communicatorFactory1)
    .addCommunicator(communicatorFactory2)
    .addModule(commandModule)
    .withStatusHandler(statusHandler)
    .build()

 

CommandCommonModule will provide proper CommandSerializer, CommandDeserializer and
CommandTypeMappingFactory which are necessary for correct serialization/deserialization of Command

Custom Commands

If you need to send command that common commands module does not support or you wish to use a
different serialization/deserialization/mapping for an existing Command,
you can easily create your own implementation.

Serialization

To provide a serializer for a command create a class that implements the CommandSerializer and
class that implements CommandSerializersFactory which will return map of custom serializers.
Finally pass it to the ConnectivityApiBuilder:

val connectivityApi = PriorityConnectivityApiBuilder(applicationContext)
    .addCommunicator(communicatorFactory1)
    .addSerializer(MyCommandSerializersFactory())
    .withStatusHandler(statusHandler)
    .build()

Deserialization

To provide a deserializer for a command create a class that implements the CommandDeserializer
and class that implements CommandDeserializersFactory which will return map of custom deserializers.
Finally pass it to the ConnectivityApiBuilder:

val connectivityApi = PriorityConnectivityApiBuilder(applicationContext)
    .addCommunicator(communicatorFactory1)
    .addDeserializer(MyCommandDeserializersFactory())
    .withStatusHandler(statusHandler)
    .build()

Mapping

Each of command should have defined header that will be represented by single Short value wrapped
with CommandType. This value is used to recognize specific command during deserialization.

When adding new command or when you wish to provide different CommandType for existing command
you can provide your own mapping by implementing CommandTypeMappingFactory:

class MyCommandTypeMappingFactory : CommandTypeMappingFactory {
    override fun create(): Map<CommandType, Class<out Command>> {
        val mapping = mutableMapOf<CommandType, Class<out Command>>()
        mapping[MY_COMMAND] = MyCommand::class.java
        return mapping.toMap()
    }

    companion object {
        val MY_COMMAND = CommandType(0)
    }
}

Then pass it to the ConnectivityApiBuilder:

val connectivityApi = PriorityConnectivityApiBuilder(applicationContext)
    .addCommunicator(communicatorFactory1)
    .addMapping(MyCommandTypeMappingFactory())
    .withStatusHandler(statusHandler)
    .build()

Module

Alternatively you can implement CommandModule interface which will provide complete set of extensions.

class MyCommandModule : CommandModule {

    override fun createDeserializerFactory(): CommandDeserializersFactory {
        return MyCommandDeserializersFactory()
    }

    override fun createMappingFactory(): CommandTypeMappingFactory {
        return MyCommandTypeMappingFactory()
    }

    override fun createSerializerFactory(): CommandSerializersFactory {
        return MyCommandSerializersFactory()
    }
}

You are here