Before the BACtrack API can be used to communicate with a BACtrack device, it must first be initialized, like so:
BACtrackAPI bacTrackAPI = new BACtrackAPI(this, callbacks, apiKey)
This constructor creates a BACtrackAPI object with the passed-in BACtrackAPICallbacks object. Additionally, it checks with our server whether the included API Key is valid. In case your app has not been approved to use our SDK, the following required callback will be called:
void BACtrackAPIKeyDeclined(String errorMessage)
Once you’ve created your BacTrackAPI object, you can establish a Bluetooth LE connection between your Android device and your BACtrack by executing the following command call on the BACtrackAPI object: public void connectToNearestBreathalyzer()
Tapping on the “Take Test” button calls public boolean startCountdown()
on the BacTrackAPI object and initiates a series of callbacks on the BacTrackAPIDelegate shown below.
In order to connect to a selected device, you must first scan for it. Call startScan(), and wait for the BACtrackFoundBreathalyzer() callback to come in. The scan will continue until stopScan() is called.
Once the BACtrackFoundBreathalyzer() callback comes in with the BACtrackDevice you would like to connect to, call connectToDevice(), passing in the BACtrackDevice object.
public BACtrackAPI(Context context, final BACtrackAPICallbacks bacTrackAPICallbacks, String apiKey)
Constructs an instance of the BACtrack API, with a given set of callbacks and an API key.
Expect one of the following callbacks, depending on whether or not the API key was valid:
void BACtrackAPIKeyAuthorized()
void BACtrackAPIKeyDeclined()
public void connectToNearestBreathalyzer()
Connect to the nearest BACtrack device with no scanning timeout. Expect either BACtrackConnected() or BACtrackConnectionTimeout().
public void connectToNearestBreathalyzerWithTimeout()
Connect to the nearest BACtrack device found after a one-second scan. See connectToNearestBreathalyzer() for more information.
public void startScan()
Scans for BACtracks within range. Continues until stopScan()
is called.
public ArrayList<BACtrackDevice> stopScan()
Stop scanning for devices. Returns an array of breathalyzers found by the scan, which can then be connected to using connectToDevice(BACtrackDevice)
,
public void connectToDevice(BACtrackDevice breathalyzer)
Alternate use case for connecting to a BACtrack device. You can retrieve a breathalyzer instance by using startScan
and stopScan()
public void disconnect()
Disconnect from the currently-connected BACtrack device.
void BACtrackAPIKeyAuthorized()
Called during construction if the API key is valid.
void BACtrackAPIKeyDeclined(String errorMessage)
Called during construction if the API key is invalid or otherwise declined.
void BACtrackDidConnect(String breathalyzerAdvertisedName)
Called after the BACtrack is connected but before services are discovered.
void BACtrackConnected(BACTrackDeviceType bacTrackDeviceType)
Called after the BACtrack is connected and services are discovered.
void BACtrackConnectionTimeout()
Called if a connection attempt times out.
void BACtrackDisconnected()
Called when the BACtrack disconnects from the Android device.
void BACtrackFoundBreathalyzer(BACtrackDevice breathalyzer)
Called whenever a BACtrack device is found while scanning for a device. You can use this callback to register a list of BACtrack devices in your app, or you can use the result of the stopScan()
API call, which returns a list of discovered devices.