BleTerminal
Class: BleTerminal(context: Context, terminalId: String)
Represents a terminal that interact with the bluetooth BleCentral and peripheral BlePeripheral to send commands to the CTG device.
Constructor
BleTerminal(context: Context, terminalId: String)
Creates a new BleTerminal instance.
Parameters:
context- Android application contextterminalId- Unique identifier for the terminal
Properties
bleListener: BleListener? = null
Represents as a callback to show the result from the BleTerminal process.
terminalId: String
Represents current connected terminalId.
Methods
Authentication Methods
saveAuthKey
fun saveAuthKey(key: String)
Encrypts the authentication key and saves it to Shared Preferences.
getAuthKey
fun getAuthKey(): String?
Retrieves the encrypted authentication key from Shared Preferences, decrypts it, and returns it.
Returns: The decrypted authentication key, or null if not found
removeAuthKey
fun removeAuthKey()
Removes the authentication key from Shared Preferences.
hasKey
fun hasKey(): Boolean
Checks if a terminal authentication key is available for secure communication.
Returns: true if key exists, false otherwise
Connection Methods
scanAndConnectToPeripheral
fun scanAndConnectToPeripheral(timeout: Long = 10000)
Connects to a BleTerminal device.
This function initiates the connection process by first validating the provided authKey and then scanning for available BLE devices. Upon finding a matching device, it establishes a connection and performs authentication.
Error Handling: The function returns a BleError in the following scenarios:
-
BleError.KeyNotFound: The authKey is not found.
-
BleError.KeyInvalid: The authKey has an invalid length (expected 64 characters).
-
BleError.BluetoothUnsupported: The device does not support Bluetooth.
-
BleError.BluetoothUnauthorized: Bluetooth permissions are not granted.
-
BleError.LocationUnauthorized: Location permissions are not granted (required for BLE scanning).
-
BleError.TerminalNotFound: Unable to find the target BleTerminal device.
Successful Connection: Upon successful connection and authentication, the BleListener.onTerminalConnected callback is invoked.
Parameters:
timeoutSecondsThe duration to wait for a connection to be established.
Callbacks:
- On success:
BleListener.onTerminalConnected() - On error:
BleListener.onError()with variousBleErrortypes
disconnect()
fun disconnect()
Disconnects the peripheral from the BleTerminal.
This function initiates the disconnection process and triggers a callback to BleListener.onTerminalDisconnected upon successful disconnection.
Status Methods
getBleConnectionState()
fun getBleConnectionState(): BleConnectionState
Retrieves the Bluetooth Low Energy (BLE) connection state of the device.
This function indicates whether the Android device is currently connected, connecting, or disconnected from the specified BLE device.
Possible values are:
- BleConnectionState.CONNECTED: Indicates an active connection to the device.
- BleConnectionState.CONNECTING: Indicates an ongoing connection attempt.
- BleConnectionState.DISCONNECTED: Indicates no active connection to the device.
This function should be called before initiating a BLE connection or disconnection to ensure the desired operation is performed in the correct state.
Returns: The current BLE connection state as a BleConnectionState enum.
getLockState()
fun getLockState(): LockState
Gets the current lock state of the vehicle.
Returns: Current LockState (locked/unlocked)
The lock state of the vehicle. Possible values are:
- LOCKED: The vehicle is locked.
- UNLOCKED: The vehicle is unlocked.
- UNKNOWN: The lock state of the vehicle is unknown.
Action Methods
sendAction(action: BleAction)
fun sendAction(action: BleAction)
Executes a Bluetooth Low Energy (BLE) action on the connected peripheral.
Parameters:
action- The BLE action to execute
Available Actions:
BleAction.LOCK- Lock the vehicleBleAction.UNLOCK- Unlock the vehicleBleAction.UNLOCK_NOKEYFOB- Unlock without key fobBleAction.HEADLIGHT- Flash headlightsBleAction.HORN- Sound hornBleAction.GET_LOCK_STATE- Get current lock stateBleAction.VEHICLE_GET_CONFIG- Get vehicle configurationBleAction.VEHICLE_GET_STATUS- Get vehicle ignition status
Callbacks:
- Result:
BleListener.onTerminalCommandResult() - Error:
BleListener.onError()
See Also
- BleService - For SDK initialization
- BleListener - For handling callbacks
- BLE Actions and States - For available actions and states