My Project
|
The tutorial will show you how to integrate the remote interface in your application, how to establish a connection to the PTV Navigator and how to communicate with the PTV Navigator.
We will show how messages will be created, how to send them to the PTV Navigator and how to receive messages sent by the PTV Navigator.
Remark: We often use constant values for the message IDs, the type of messages and the error codes. All these values are stored in the "Constants" class of the RITest demo program. You could also use the integer values given by this documentation, but for better readability, we prefer to use the values of the Constants class.
There are 3 steps to connect to the navigation service:
There are two main types of messages:
All messages have in common that the PTV Navigator sends back an acknowledgement message with an error code in the field "arg1" of the message. The user has to check every time this acknowledgement message and it'S returning error code to know that the command sent to the PTV Navigator had succeeded or not.
For more details, see All Commands.
To communicate with the PTV Navigator, it is necessary that it's started and running. If the PTV Navigator is not running, it will as default try to start up when an RI command was sent to it (on some devices, this auto startup is not possible because of the device configuration). During start up, the PTV Navigator will return the error message RI_ERROR_IS_INITIALIZING until the application is fully running. This auto startup can be configured by the preference key "system_allow_start_over_ri" of the PTV Navigator. The default is set to auto start up, to disable it, set the key to false. When auto startup is disabled, the PTV Navigator will return RI_ERROR_NOT_INITIALIZED. In this case you have to start the PTV Navigator manually.
First, be sure that the PTV Navigator software is running and the binding had suceeded (the onServiceConnected() method from the ServiceConnection had been called).
Let's do a little practice: we want to query all vehicle profiles that are available in the PTV Navigator.
To see what we have to do and what message has to be sent, take a look at the documentation for it: Get all available vehicle profiles.
Therefore the first thing we have to do to send a message is creating a Message object by obtaining it from the Android message pool and set the correct message id for what you want to do:
Next, set the replyTo field of the Message to "clientMessenger", so the PTV Navigator can send messages back to us:
Finally, send the message with the serviceMessenger object to the PTV Navigator:
Sending a message with data is similar to the above described, you only have to attach the bundle to the message, so the code looks like this:
When the PTV Navigator receives a message from a client, it will immediately send back a message with an error code, and optionally a bundle of data.
To receive messages from the PTV Navigator, you have to implement a Handler (see above). When the PTV Navigator sends back a message, the handleMessage() method of the handler will be called, and the client can react to the message.
The message arrived has a field "arg1" which holds the actual errorcode, set by the PTV Navigator, to let the client determine if the command succeeded or not. See Error Values for more details.
The message can also hold data as a Bundle.
Here is how to handle the incoming message from the PTV Navigator in the Handler and how to get the information out of the message in our example of getting the vehicle profiles:
As you can see, the message arrives in the incoming handler and we only have to test if the message is the one we want. We do this by checking the "what"-field of the message which holds the message id. After that, we can read the data out of the Bundle.
We also checked the "arg1"-field for an error code, to see if the command succeeded.
(The checkInitialized() function in the above example does only check, if the PTV Navigator is running by testing the error code for RI_ERROR_NOT_INITIALIZED.)
Reading out a Bundle from a message is generally done by this code sequence:
The Bundle stores key/value pairs, so to read a key with name "Profiles" of type StringArray, you can call:
In the documentation of the various messages, you will find every key you can set and get when sending or receiving messages with bundles.
Take a look at the Android Bundle documentation for more details about Bundles and Messages.
Clients can register themselves to certain events. After this registration, the PTV Navigator will frequently send information about these events. If the client wants to stop listening, it can send an unregister message.
It's possible to listen to multiple events, just send a register message for all events you want to listen to.
Let's show how this registering works by the following example:
We want to listen to navigation information (which only works then a navigation is running and the GPS settings in the PTV Navigator are set to "GPS active in the background").
So the first thing we have to do is sending a message with the appropriate fields set to let the service know what type of information we want to receive. This is done by creating a bundle with the key "EventType" and the value of the wanted event, in our case "EVENT_NAVIGATION_INFO".
From know on, the PTV Navigator will send back frequently navigation information. The sent messages have the "what" field of the message set to "MSG_RI_EVENT_DATA" to inform the client that this message is a push message with event information.
The message will be received in the IncomingHandler like in the examples above:
If the client doesn't want to listen to the information anymore, he can unregister the service:
We create a message with the id "MSG_RI_UNREGISTER_EVENT_LISTENER" and set the EventType of the bundle to "EVENT_NAVIGATION_INFO" to unregister the listening to navigation info.
Some of the commands cannot be sent in particular situations, like deleting the target station list while navigating, please check the API reference for more details.
To find out more, see the RITest page where you can find the RITest documentation.
© PTV Group 2021 | Generated on Sun Oct 2 2022 19:28:23 for My Project by 1.8.17 |