-
Notifications
You must be signed in to change notification settings - Fork 2
Communicating Without Services
Communicating via either the SimpleTappyManager or a custom Tappy manager interfacing to a TappyBleCommunicationsService is not always the best choice for a given application. Developers who would prefer not to use either of these built-in services can instead work with the TappyBleCommunicator class directly.
compile "com.taptrack.tcmptappy:commlink-ble:${latestVersion}"
TappyBleCommunicator communicator =
new TappyBleCommunicator(context,deviceDefinition);
//prepare to connect
communicator.initialize();
//connect to the tappy
communicator.connect();
// work with tappy
// disconnect from the tappy
communicator.disconnect();
// close the connection
// this cancels the connection and removes all listeners
communicator.close();
In order to listen to the status changes of the Tappy connection and any messages the Tappy sends, register a CommunicatorStatusChangedListener
(see note on status values) and a TcmpMessageListener
respectively. If you wish to also be notified of malformed unparsable packets being received, register an UnparsablePacketListener
. One the communicator notified that it has entered a state of TappyBleDeviceStatus.READY
, it is ready to communicate with the Tappy. Any TCMP messages you attempt to send while the Tappy is not ready will be queued up for sending once the connection is fully established. Additionally, the communicator will not attempt to reconnect if a connection is lost, if you wish to autoreconnect, you will have to listen for a change to TappyBleDeviceStatus.DISCONNECTED
and reconnect (by calling connect()) yourself.
The TappyBleCommunicator is thread-safe so feel free to interact with it from whatever threads are convenient.