Aller au contenu principal

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 context
  • terminalId - 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:

  • timeoutSeconds The duration to wait for a connection to be established.

Callbacks:

  • On success: BleListener.onTerminalConnected()
  • On error: BleListener.onError() with various BleError types

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 vehicle
  • BleAction.UNLOCK - Unlock the vehicle
  • BleAction.UNLOCK_NOKEYFOB - Unlock without key fob
  • BleAction.HEADLIGHT - Flash headlights
  • BleAction.HORN - Sound horn
  • BleAction.GET_LOCK_STATE - Get current lock state
  • BleAction.VEHICLE_GET_CONFIG - Get vehicle configuration
  • BleAction.VEHICLE_GET_STATUS - Get vehicle ignition status

Callbacks:

  • Result: BleListener.onTerminalCommandResult()
  • Error: BleListener.onError()

See Also