Android SDK: Getting started
This guide explains how to install the Cartrack BLE Lock SDK for Android and run the basic connection flow against a compatible BLE terminal.
Installation
- Add the latest CartrackBleLock_Android_SDK
.aarfile to your Android project (for example, under alibs/directory). - In your project-level
build.gradle, addflatDir { dirs 'libs' }.
allprojects {
repositories {
...
flatDir {
dirs 'libs'
}
}
}
- In your app-level `build.gradle`, add the `.aar` file dependency.
```kotlin
dependencies {
implementation fileTree(include: ['*.aar'], dir: 'libs')
implementation(name: 'blesdk-release_v3.0.3.aar', ext: 'aar')
}
- For Android 12 (API 31) or higher, you must declare Bluetooth permissions.
- Update your app-level
AndroidManifest.xml.
<manifest>
<uses-permission android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
...
</manifest>
Core Classes
BleService
The main entry point for the Cartrack BLE SDK. Use this class to initialize the SDK and obtain terminal instances.
Key Methods:
- BleService.configure() - Configures the BleService. This function should be called first.
- BleService.getTerminal() - Get an instance of BleTerminal associated with the given terminal ID.
- BleService.isConfigured() - Checks if the given BleTerminal instance is configured.
- BleService.clear() - Clears the configured instance.
BleTerminal
Represents a terminal that communicates with CTG devices via Bluetooth Low Energy.
Key Methods:
- BleTerminal.scanAndConnectToPeripheral - Connects to a BleTerminal device.
- BleTerminal.sendAction - Executes a Bluetooth Low Energy (BLE) action on the connected peripheral.
- BleTerminal.saveAuthKey - Encrypts the authentication key and saves it to Shared Preferences.
- BleTerminal.disconnect - Disconnects the peripheral from the BleTerminal.
- BleTerminal.getAuthKey - Retrieves the encrypted authentication key from Shared Preferences, decrypts it, and returns it.
- BleTerminal.getBleConnectionState - Retrieves the Bluetooth Low Energy (BLE) connection state of the device.
- BleTerminal.getLockState - Gets the current lock state of the vehicle.
- BleTerminal.hasKey - Checks if a terminal authentication key is available for secure communication.
- BleTerminal.removeAuthKey - Removes the authentication key from Shared Preferences.
BleListener
Interface for handling BLE events and callbacks.
Key Callbacks:
- BleListener.onError() - Called when an error occurs during a BleTerminal operation.
- BleListener.onReconnect() - Called when the Bluetooth GATT layer returns
GATT_ERRORorGATT_FAILURE. After three reconnect attempts, the SDK returnsBleError.GATTError. - BleListener.onRemoveAuthKeySuccess() - Called when the authentication key is successfully removed.
- BleListener.onSaveAuthKeySuccess() - Called when the authentication key is successfully saved.
- BleListener.onSignalStrength() - Called when the signal strength changes.
- BleListener.onTerminalCommandResult() - Called when a terminal command returns a result.
- BleListener.onTerminalConnected() - Called when the BleTerminal successfully connects to a device.
- BleListener.onTerminalDidGetVehicleStats() - Called when fetching vehicle stat.
- BleListener.onTerminalDidGetVehicleStatus() - Called when fetching vehicle status.
- BleListener.onTerminalDisconnected() - Called when terminal is disconnected.
How to Use
All examples require
import CartrackBleLockSDKsomewhere in the source file.
Initialize CartrackBleLockSDK
Initialize CartrackBleLockSDK with Context:
V2.4
BleService.configure(Context, vendorId, apiKey, countryCode)
V3.0
BleService.configure(Context)
Before starting to get the authentication key, you can create a BleTerminal variable outside the function:
var bleTerminal: BleTerminal
Get an instance of BleTerminal:
bleTerminal = BleService.getTerminal(terminalID)
Save Authentication Key
Save the authentication key with BleTerminal.saveAuthKey():
bleTerminal.saveAuthKey()
Get Authentication Key
Get the authentication key with BleTerminal.getAuthKey():
bleTerminal.getAuthKey()
This action requires network connection thus performing it earlier is encouraged before connecting to terminal.