|
@@ -1,7 +1,6 @@
|
|
|
package com.iscs.bozzys.utils.ble
|
|
package com.iscs.bozzys.utils.ble
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint
|
|
import android.annotation.SuppressLint
|
|
|
-import android.app.Application
|
|
|
|
|
import android.bluetooth.BluetoothAdapter
|
|
import android.bluetooth.BluetoothAdapter
|
|
|
import android.bluetooth.BluetoothDevice
|
|
import android.bluetooth.BluetoothDevice
|
|
|
import android.bluetooth.BluetoothManager
|
|
import android.bluetooth.BluetoothManager
|
|
@@ -9,30 +8,16 @@ import android.bluetooth.le.ScanCallback
|
|
|
import android.bluetooth.le.ScanFilter
|
|
import android.bluetooth.le.ScanFilter
|
|
|
import android.bluetooth.le.ScanResult
|
|
import android.bluetooth.le.ScanResult
|
|
|
import android.bluetooth.le.ScanSettings
|
|
import android.bluetooth.le.ScanSettings
|
|
|
|
|
+import android.content.Context
|
|
|
import com.iscs.bozzys.Entry
|
|
import com.iscs.bozzys.Entry
|
|
|
import com.iscs.bozzys.api.ApiRequest
|
|
import com.iscs.bozzys.api.ApiRequest
|
|
|
import com.iscs.bozzys.api.ApiRequest.getResponse
|
|
import com.iscs.bozzys.api.ApiRequest.getResponse
|
|
|
import com.iscs.bozzys.api.KeyTicket
|
|
import com.iscs.bozzys.api.KeyTicket
|
|
|
import com.iscs.bozzys.utils.LogUtil
|
|
import com.iscs.bozzys.utils.LogUtil
|
|
|
-import com.iscs.bozzys.utils.ble.BleFrameExt.buildBLEDisconnectCMD
|
|
|
|
|
-import com.iscs.bozzys.utils.ble.BleFrameExt.buildBLEGetPowerCMD
|
|
|
|
|
-import com.iscs.bozzys.utils.ble.BleFrameExt.buildBLEGetStatusCMD
|
|
|
|
|
-import com.iscs.bozzys.utils.ble.BleFrameExt.buildBLEGetTicketInfoCMD
|
|
|
|
|
-import com.iscs.bozzys.utils.ble.BleFrameExt.buildBLESwitchRunModeCMD
|
|
|
|
|
-import com.iscs.bozzys.utils.ble.BleFrameExt.buildBLETicketDataCMDList
|
|
|
|
|
-import com.iscs.bozzys.utils.ble.BleFrameExt.getDisconnectResult
|
|
|
|
|
-import com.iscs.bozzys.utils.ble.BleFrameExt.getPower
|
|
|
|
|
-import com.iscs.bozzys.utils.ble.BleFrameExt.getRunMode
|
|
|
|
|
-import com.iscs.bozzys.utils.ble.BleFrameExt.getSendTicketResult
|
|
|
|
|
-import com.iscs.bozzys.utils.ble.BleFrameExt.getSwitchRunModeResult
|
|
|
|
|
-import com.iscs.bozzys.utils.ble.BleFrameExt.getTicketPackageInfo
|
|
|
|
|
-import com.iscs.bozzys.utils.ble.BleFrameExt.getToken
|
|
|
|
|
-import com.iscs.bozzys.utils.byteArrayToHexString
|
|
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
import kotlinx.coroutines.SupervisorJob
|
|
import kotlinx.coroutines.SupervisorJob
|
|
|
import kotlinx.coroutines.channels.Channel
|
|
import kotlinx.coroutines.channels.Channel
|
|
|
-import kotlinx.coroutines.delay
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
import kotlinx.coroutines.launch
|
|
|
import kotlinx.serialization.json.Json
|
|
import kotlinx.serialization.json.Json
|
|
|
|
|
|
|
@@ -43,7 +28,9 @@ import kotlinx.serialization.json.Json
|
|
|
* 2. 循环连接钥匙设备,读取设备任务完成情况
|
|
* 2. 循环连接钥匙设备,读取设备任务完成情况
|
|
|
* 3. 使用中的设备享有优先权,被标记为使用中的设备不进行作业任务的读取操作,等待进行作业任务的下发操作
|
|
* 3. 使用中的设备享有优先权,被标记为使用中的设备不进行作业任务的读取操作,等待进行作业任务的下发操作
|
|
|
*/
|
|
*/
|
|
|
-object BleTask {
|
|
|
|
|
|
|
+object KeyManager {
|
|
|
|
|
+
|
|
|
|
|
+ private const val TAG = "KeyManager"
|
|
|
|
|
|
|
|
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
|
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
|
|
|
|
|
|
@@ -88,10 +75,10 @@ object BleTask {
|
|
|
/**
|
|
/**
|
|
|
* 蓝牙任务管理初始化
|
|
* 蓝牙任务管理初始化
|
|
|
*
|
|
*
|
|
|
- * @param app
|
|
|
|
|
|
|
+ * @param ctx
|
|
|
*/
|
|
*/
|
|
|
- fun init(app: Application) {
|
|
|
|
|
- bm = app.getSystemService(BluetoothManager::class.java)
|
|
|
|
|
|
|
+ fun init(ctx: Context) {
|
|
|
|
|
+ bm = ctx.getSystemService(BluetoothManager::class.java)
|
|
|
ba = bm.adapter
|
|
ba = bm.adapter
|
|
|
scan()
|
|
scan()
|
|
|
loop()
|
|
loop()
|
|
@@ -151,6 +138,7 @@ object BleTask {
|
|
|
* 循环发现设备
|
|
* 循环发现设备
|
|
|
*/
|
|
*/
|
|
|
private fun loop() {
|
|
private fun loop() {
|
|
|
|
|
+ LogUtil.d(TAG, "功能模块 - 发现钥匙 已开启")
|
|
|
scope.launch {
|
|
scope.launch {
|
|
|
for (device in deviceQueue) {
|
|
for (device in deviceQueue) {
|
|
|
connectDeviceAndDealData(device)
|
|
connectDeviceAndDealData(device)
|
|
@@ -170,69 +158,51 @@ object BleTask {
|
|
|
private suspend fun connectDeviceAndDealData(device: BluetoothDevice) {
|
|
private suspend fun connectDeviceAndDealData(device: BluetoothDevice) {
|
|
|
if (usedDevices.contains(device.address)) {
|
|
if (usedDevices.contains(device.address)) {
|
|
|
if (usedDevices[device.address].isNullOrEmpty()) {
|
|
if (usedDevices[device.address].isNullOrEmpty()) {
|
|
|
- LogUtil.w("BleTask", "[${device.address}] 设备即将被使用,不做状态获取处理")
|
|
|
|
|
|
|
+ LogUtil.w(TAG, "[${device.address}] 设备即将被使用,不做状态获取处理")
|
|
|
} else {
|
|
} else {
|
|
|
// 执行作业任务的下发操作
|
|
// 执行作业任务的下发操作
|
|
|
sendTicket2Device(device)
|
|
sendTicket2Device(device)
|
|
|
}
|
|
}
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
- LogUtil.d("BleTask", "[${device.address}] 连接开始")
|
|
|
|
|
- val bm = BleManager(Entry.app, mac = device.address)
|
|
|
|
|
- val result = bm.connect(device)
|
|
|
|
|
- if (result.connected) {
|
|
|
|
|
- // 获取设备token
|
|
|
|
|
- val token = bm.writeByResponse(BleFrameExt.buildBLEGetTokenCMD()).getToken()
|
|
|
|
|
- LogUtil.i("BleTask", "[${device.address}] 设备密钥 -> ${token.byteArrayToHexString()}")
|
|
|
|
|
|
|
+ LogUtil.d(TAG, "[${device.address}] 连接开始")
|
|
|
|
|
+ val gc = GattClient(Entry.app, device)
|
|
|
|
|
+ if (gc.connect()) {
|
|
|
|
|
+ val kh = KeyHelper(gc)
|
|
|
// 获取设备电量
|
|
// 获取设备电量
|
|
|
- val power = bm.writeByResponse(token.buildBLEGetPowerCMD()).getPower()
|
|
|
|
|
- LogUtil.i("BleTask", "[${device.address}] 设备电量 -> $power")
|
|
|
|
|
|
|
+ val power = kh.getPower()
|
|
|
|
|
+ LogUtil.i(TAG, "[${device.address}] 设备电量 -> $power")
|
|
|
// 获取当前设备运行模式
|
|
// 获取当前设备运行模式
|
|
|
- val runMode = bm.writeByResponse(token.buildBLEGetStatusCMD()).getRunMode()
|
|
|
|
|
- LogUtil.i("BleTask", "[${device.address}] 设备模式 -> $runMode")
|
|
|
|
|
- if (runMode == BleRunMode.WORK) {
|
|
|
|
|
- // 执行作业票的获取
|
|
|
|
|
- val pkgList = ArrayList<BleTicketDataPackage>()
|
|
|
|
|
- val mainPkg = bm.writeByResponse(token.buildBLEGetTicketInfoCMD()).getTicketPackageInfo()
|
|
|
|
|
- pkgList.add(mainPkg)
|
|
|
|
|
- // 校验是否有子包,如果有,继续读取子包数据
|
|
|
|
|
- for (idx in 1 until mainPkg.pkgTotal) {
|
|
|
|
|
- val subPkg =
|
|
|
|
|
- bm.writeByResponse(token.buildBLEGetTicketInfoCMD(idx, mainPkg.pkgTotal)).getTicketPackageInfo()
|
|
|
|
|
- pkgList.add(subPkg)
|
|
|
|
|
- }
|
|
|
|
|
- var datas = byteArrayOf()
|
|
|
|
|
- pkgList.forEach { datas += it.pkgData }
|
|
|
|
|
- val ticketJson = String(datas)
|
|
|
|
|
- LogUtil.i("BleTask", "[${device.address}] 设备数据 -> $ticketJson")
|
|
|
|
|
|
|
+ val runMode = kh.getWorkMode()
|
|
|
|
|
+ LogUtil.i(TAG, "[${device.address}] 设备模式 -> $runMode")
|
|
|
|
|
+ if (runMode == "WORK") {
|
|
|
|
|
+ val ticketJson = kh.getTicketData()
|
|
|
|
|
+ LogUtil.i(TAG, "[${device.address}] 设备数据 -> $ticketJson")
|
|
|
// 解析作业票
|
|
// 解析作业票
|
|
|
val json = Json { ignoreUnknownKeys = true }
|
|
val json = Json { ignoreUnknownKeys = true }
|
|
|
try {
|
|
try {
|
|
|
// 处理作业票完成情况
|
|
// 处理作业票完成情况
|
|
|
val ticket = json.decodeFromString<KeyTicket>(ticketJson)
|
|
val ticket = json.decodeFromString<KeyTicket>(ticketJson)
|
|
|
- // LogUtil.i("BleTask", "[${device.address}] 解析作业")
|
|
|
|
|
|
|
+ // LogUtil.i(TAG, "[${device.address}] 解析作业")
|
|
|
val params = ticket.getReturnParams(device.address)
|
|
val params = ticket.getReturnParams(device.address)
|
|
|
// 执行归还数据核对
|
|
// 执行归还数据核对
|
|
|
- LogUtil.i("BleTask", "[${device.address}] 提交数据 -> $params")
|
|
|
|
|
|
|
+ LogUtil.i(TAG, "[${device.address}] 提交数据 -> $params")
|
|
|
val keyRsp = ApiRequest.returnKey(params).getOrElse { it.getResponse() }
|
|
val keyRsp = ApiRequest.returnKey(params).getOrElse { it.getResponse() }
|
|
|
- LogUtil.i("BleTask", "[${device.address}] 提交返回 -> ${keyRsp.data}")
|
|
|
|
|
|
|
+ LogUtil.i(TAG, "[${device.address}] 提交返回 -> ${keyRsp.data}")
|
|
|
if (keyRsp.data?.keyBack == "1") {
|
|
if (keyRsp.data?.keyBack == "1") {
|
|
|
// 切换钥匙为待机模式
|
|
// 切换钥匙为待机模式
|
|
|
- val switch = bm.writeByResponse(token.buildBLESwitchRunModeCMD(BleRunMode.STBY))
|
|
|
|
|
- .getSwitchRunModeResult()
|
|
|
|
|
- LogUtil.i("BleTask", "[${device.address}] 切换模式 -> ${if (switch) "成功" else "失败"}")
|
|
|
|
|
|
|
+ val switch = kh.setWorkMode("STBY")
|
|
|
|
|
+ LogUtil.i(TAG, "[${device.address}] 切换模式 -> ${if (switch) "成功" else "失败"}")
|
|
|
}
|
|
}
|
|
|
// 归还钥匙接口调用结果验证
|
|
// 归还钥匙接口调用结果验证
|
|
|
listener.forEach { it.onReadTicketSuccess(device.address, ticket) }
|
|
listener.forEach { it.onReadTicketSuccess(device.address, ticket) }
|
|
|
} catch (e: Exception) {
|
|
} catch (e: Exception) {
|
|
|
- LogUtil.i("BleTask", "[${device.address}] 解析异常 -> $e")
|
|
|
|
|
|
|
+ LogUtil.i(TAG, "[${device.address}] 解析异常 -> $e")
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- val disRet = bm.writeByResponse(token.buildBLEDisconnectCMD()).getDisconnectResult()
|
|
|
|
|
- LogUtil.i("BleTask", "[${device.address}] 设备断开 -> ${if (disRet == 1) "成功" else "失败"}")
|
|
|
|
|
}
|
|
}
|
|
|
- bm.disconnect()
|
|
|
|
|
- LogUtil.d("BleTask", "[${device.address}] 连接断开")
|
|
|
|
|
|
|
+ gc.disconnect()
|
|
|
|
|
+ LogUtil.d(TAG, "[${device.address}] 连接断开")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -242,43 +212,32 @@ object BleTask {
|
|
|
*/
|
|
*/
|
|
|
private suspend fun sendTicket2Device(device: BluetoothDevice) {
|
|
private suspend fun sendTicket2Device(device: BluetoothDevice) {
|
|
|
val json = usedDevices[device.address] ?: "{}"
|
|
val json = usedDevices[device.address] ?: "{}"
|
|
|
- LogUtil.d("BleTask", "[${device.address}] 下发作业")
|
|
|
|
|
- LogUtil.d("BleTask", "[${device.address}] 连接开始")
|
|
|
|
|
- val bm = BleManager(Entry.app, mac = device.address)
|
|
|
|
|
- val result = bm.connect(device)
|
|
|
|
|
- if (result.connected) {
|
|
|
|
|
- // 获取设备token
|
|
|
|
|
- val token = bm.writeByResponse(BleFrameExt.buildBLEGetTokenCMD()).getToken()
|
|
|
|
|
- LogUtil.i("BleTask", "[${device.address}] 设备密钥 -> ${token.byteArrayToHexString()}")
|
|
|
|
|
|
|
+ LogUtil.d(TAG, "[${device.address}] 下发作业")
|
|
|
|
|
+ LogUtil.d(TAG, "[${device.address}] 连接开始")
|
|
|
|
|
+ val gc = GattClient(Entry.app, device)
|
|
|
|
|
+ if (gc.connect()) {
|
|
|
|
|
+ val kh = KeyHelper(gc)
|
|
|
// 获取设备电量
|
|
// 获取设备电量
|
|
|
- val power = bm.writeByResponse(token.buildBLEGetPowerCMD()).getPower()
|
|
|
|
|
- LogUtil.i("BleTask", "[${device.address}] 设备电量 -> $power")
|
|
|
|
|
|
|
+ val power = kh.getPower()
|
|
|
|
|
+ LogUtil.i(TAG, "[${device.address}] 设备电量 -> $power")
|
|
|
// 获取当前设备运行模式
|
|
// 获取当前设备运行模式
|
|
|
- val runMode = bm.writeByResponse(token.buildBLEGetStatusCMD()).getRunMode()
|
|
|
|
|
- LogUtil.i("BleTask", "[${device.address}] 设备模式 -> $runMode")
|
|
|
|
|
|
|
+ val runMode = kh.getWorkMode()
|
|
|
|
|
+ LogUtil.i(TAG, "[${device.address}] 设备模式 -> $runMode")
|
|
|
// 下发作业票
|
|
// 下发作业票
|
|
|
- val tickets = token.buildBLETicketDataCMDList(json)
|
|
|
|
|
- var ticketSendOk = true
|
|
|
|
|
- tickets.forEach {
|
|
|
|
|
- delay(200)
|
|
|
|
|
- val ticket = bm.writeByResponse(it).getSendTicketResult()
|
|
|
|
|
- if (ticket != 0) {
|
|
|
|
|
- ticketSendOk = false
|
|
|
|
|
- return@forEach
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ val tickets = kh.sendTicketData(json)
|
|
|
|
|
+ if (tickets) {
|
|
|
|
|
+ LogUtil.i(TAG, "[${device.address}] 下发作业 -> 成功")
|
|
|
|
|
+ // 作业票下发成功,修改设备运行模式为工作模式
|
|
|
|
|
+ val switch = kh.setWorkMode("WORK")
|
|
|
|
|
+ LogUtil.i(TAG, "[${device.address}] 切换模式 -> ${if (switch) "成功" else "失败"}")
|
|
|
|
|
+ removeDeviceUsed(device.address)
|
|
|
|
|
+ listener.forEach { it.onSendTicketSuccess(device.address) }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ LogUtil.i(TAG, "[${device.address}] 下发作业 -> 失败")
|
|
|
}
|
|
}
|
|
|
- LogUtil.i("BleTask", "[${device.address}] 下发作业 -> ${if (ticketSendOk) "成功" else "失败"}")
|
|
|
|
|
- // 作业票下发成功,修改设备运行模式为工作模式
|
|
|
|
|
- val switch = bm.writeByResponse(token.buildBLESwitchRunModeCMD(BleRunMode.WORK))
|
|
|
|
|
- .getSwitchRunModeResult()
|
|
|
|
|
- LogUtil.i("BleTask", "[${device.address}] 切换模式 -> ${if (switch) "成功" else "失败"}")
|
|
|
|
|
- val disRet = bm.writeByResponse(token.buildBLEDisconnectCMD()).getDisconnectResult()
|
|
|
|
|
- LogUtil.i("BleTask", "[${device.address}] 设备断开 -> ${if (disRet == 1) "成功" else "失败"}")
|
|
|
|
|
- removeDeviceUsed(device.address)
|
|
|
|
|
- listener.forEach { it.onSendTicketSuccess(device.address) }
|
|
|
|
|
}
|
|
}
|
|
|
- bm.disconnect()
|
|
|
|
|
- LogUtil.d("BleTask", "[${device.address}] 断开连接")
|
|
|
|
|
|
|
+ gc.disconnect()
|
|
|
|
|
+ LogUtil.d(TAG, "[${device.address}] 断开连接")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|