Android SDK Guide
Install ATH SDK
Install the SDK Using Gradle
implementation "com.aisenz:ath:1.0.4"
You need to add the maven repository to your build.gradle or settings.gradle file
repositories {
google()
mavenCentral()
jcenter()
maven {url "https://mvn.senzflow.io/repository/ath/" }
}
Update Android Manifest
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
Classes and Interfaces
BleManager
fun init(context: Context)
fun getDeviceList(): List<BleDevice>
fun getDevice(mac: String): BleDevice?
BleDevice
fun connect(callback: ConnectCallback)
fun disconnect()
fun setListener(listener: BleDeviceListener)
fun removeListener()
fun sendCommand(cmd: Command, callback: CommandCallback)
fun getConnectionState(): BleConnectionState
BleDeviceListener
fun onDeviceStateChange(state: BleDeviceState)
fun onDataComing(data:Data)
BleDeviceState
enum class BleDeviceState(private val mState: Int) {
CONNECTED(1),
CONNECTING(2),
DISCONNECTED(0),
DISCONNECTING(3)
}
Command
class Command(val type: String, val parameters: Map<String, Any>)
Data
class Data(val type: String, val values: Map<String, Any>)
ConnectCallback
interface ConnectCallback {
fun onConenctFailed(errorCode: Int, detail: String)
fun onConnectSuccess()
companion object {
//error code
val CONNECT_TIMEOUT = -1
val CONNECTION_LIMIT_EXCEEDED = -2
}
}
CommandCallback
interface CommandCallback {
fun onCommandFailed(errorCode: Int, detail: String)
fun onCommandSuccess()
companion object {
//error code
val UNKNOWN_COMMAND = -1
val PARAMETER_INVALID = -2
val DEVICE_DISCONNECT = -3
}
}
Quick Start
Initialize The BleManager
To manage Bluetooth devices, you need to initialize BleManager.
import android.app.Application
class DemoApplication : Application() {
override fun onCreate() {
super.onCreate()
BleManager.instance.init(this);
}
}
Get Device List
val bleDeviceList = BleManager.instance.getDeviceList()
Get Device and State
val bleDevice = BleManager.instance.getDevice("00:00:00:00:00:00")
val deviceState = bleDevice.getState()
Set Listener For Device
When you get the BleDevice, You can set up a listener to handle the device connection status and receive notification data from the device.
bleDevice.setListener(object: BleDeviceListener {
override fun onDeviceStateChange(state: BleDeviceState){
}
override fun onDataComing(it: Data){
if(it.type=="RealTime")
{
//Get data and convert it to the specified data type
val bloodOxygen= (it.values.get("blood_oxygen") as Int
val heartRate= (it.values.get("heart_rate") as Int
}
}
})
Connect Device
After obtaining the BleDevice, you can start the device. The device will establish a connection and set the monitoring for notification.
bleDevice.connect(object:ConnectCallback{
override fun onConenctFailed(errorCode: Int, detail: String) {
// handle error here
if(errorCode==ConnectCallback.CONNECT_TIMEOUT){
}
}
override fun onConnectSuccess() {
}
})
Send Command To Device
If you want to get data from a Bluetooth device, you can send the specified command.
//start get realtime data,
//you can handle realtime data from BleDeviceListener.onDataComing
bleDevice.sendCommand(Command("startMeasure", mapOf()),
object:CommandCallback{
override fun onCommandFailed(errorCode: Int, detail: String) {
// handle error here
if(errorCode==CommandCallback.UNKNOWN_COMMAND){
}
}
override fun onCommandSuccess() {
}
})
//stop get realtime data
bleDevice.sendCommand(Command("stopMeasure", mapOf()),
object:CommandCallback{
override fun onCommandFailed(errorCode: Int, detail: String) {
// handle error here
if(errorCode==CommandCallback.PARAMETER_INVALID){
}
}
override fun onCommandSuccess() {
}
})