|
|
@@ -1,19 +1,21 @@
|
|
|
package com.grkj.ui_base.utils.ble
|
|
|
|
|
|
+import android.Manifest
|
|
|
+import android.annotation.SuppressLint
|
|
|
import android.bluetooth.BluetoothDevice
|
|
|
import android.bluetooth.BluetoothGatt
|
|
|
-import com.clj.fastble.BleManager
|
|
|
-import com.clj.fastble.data.BleDevice
|
|
|
-import com.clj.fastble.exception.BleException
|
|
|
-import com.grkj.data.data.CommonConstants
|
|
|
+import android.bluetooth.BluetoothGattCharacteristic
|
|
|
+import androidx.annotation.RequiresPermission
|
|
|
import com.grkj.shared.utils.extension.startsWith
|
|
|
import com.grkj.shared.utils.extension.toHexStrings
|
|
|
import com.grkj.ui_base.R
|
|
|
import com.grkj.ui_base.config.ISCSConfig
|
|
|
-import com.grkj.ui_base.service.CheckKeyInfoTask
|
|
|
import com.grkj.ui_base.utils.CommonUtils
|
|
|
import com.grkj.ui_base.utils.event.LoadingEvent
|
|
|
import com.grkj.ui_base.utils.modbus.ModBusController
|
|
|
+import com.huyuhui.fastble.BleManager
|
|
|
+import com.huyuhui.fastble.data.BleDevice
|
|
|
+import com.huyuhui.fastble.exception.BleException
|
|
|
import com.sik.sikandroid.activity.ActivityTracker
|
|
|
import com.sik.sikcore.bluetooth.BLEScanner
|
|
|
import com.sik.sikcore.bluetooth.IBluetoothScanCallback
|
|
|
@@ -64,11 +66,6 @@ object BleConnectionManager {
|
|
|
*/
|
|
|
var mExceptionKeyList = mutableListOf<String>()
|
|
|
|
|
|
- /**
|
|
|
- * 断开连接任务
|
|
|
- */
|
|
|
- private val disconnectJob: HashMap<String, String> = hashMapOf()
|
|
|
-
|
|
|
/**
|
|
|
* 蓝牙的通信返回监听
|
|
|
*/
|
|
|
@@ -95,7 +92,6 @@ object BleConnectionManager {
|
|
|
bleBean.bleDevice, byteArray
|
|
|
) { isSuccess ->
|
|
|
if (isSuccess) {
|
|
|
- prepareDoneCallBack?.invoke(true, bleBean)
|
|
|
//尝试使用命令作为心跳 ,获取token完成之后就要建立心跳了
|
|
|
getBatteryPower(bleBean.bleDevice, true)
|
|
|
}
|
|
|
@@ -106,67 +102,12 @@ object BleConnectionManager {
|
|
|
val power = byteArray[4].toInt()
|
|
|
ModBusController.updateKeyPower(power, bleBean.bleDevice.mac)
|
|
|
logger.info("电量(${bleBean.bleDevice.mac}):${power}")
|
|
|
+ prepareDoneCallBack?.invoke(true, bleBean)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 启动断连任务
|
|
|
- */
|
|
|
- fun launchDisconnectJob(mac: String) {
|
|
|
- val jobId = ThreadUtils.runOnIODelayed(CommonConstants.BLE_DISCONNECT_DELAY_TIME) {
|
|
|
- val bleBean = getBleDeviceByMac(mac)
|
|
|
- bleBean?.bleDevice?.let {
|
|
|
- BleManager.getInstance().disconnect(it)
|
|
|
- deviceList.removeIf { it.bleDevice.mac == bleBean.bleDevice.mac }
|
|
|
- disconnectJob.remove(it.mac)
|
|
|
- logger.info("断开连接计划单把断开:${it.mac}")
|
|
|
- }
|
|
|
- }
|
|
|
- disconnectJob[mac] = jobId
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 启动断连所有设备任务
|
|
|
- */
|
|
|
- fun launchDisconnectAllJob() {
|
|
|
- deviceList.forEach { bleBean ->
|
|
|
- val jobId = ThreadUtils.runOnIODelayed(CommonConstants.BLE_DISCONNECT_DELAY_TIME) {
|
|
|
- val bleBean =
|
|
|
- getBleDeviceByMac(bleBean.bleDevice.mac)
|
|
|
- bleBean?.bleDevice?.let {
|
|
|
- BleManager.getInstance().disconnect(it)
|
|
|
- deviceList.removeIf { it.bleDevice.mac == bleBean.bleDevice.mac }
|
|
|
- disconnectJob.remove(it.mac)
|
|
|
- logger.info("断开连接计划全部断开:${it.mac}")
|
|
|
- }
|
|
|
- }
|
|
|
- disconnectJob[bleBean.bleDevice.mac] = jobId
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 取消断连所有设备任务
|
|
|
- */
|
|
|
- fun cancelDisconnectAllJob() {
|
|
|
- disconnectJob.forEach {
|
|
|
- ThreadUtils.cancel(it.value)
|
|
|
- }
|
|
|
- disconnectJob.clear()
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 取消断连任务
|
|
|
- */
|
|
|
- fun cancelDisconnectJob(mac: String) {
|
|
|
- val jobId = disconnectJob[mac]
|
|
|
- jobId?.let {
|
|
|
- ThreadUtils.cancel(it)
|
|
|
- disconnectJob.remove(jobId)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 注册连接监听:
|
|
|
* - 如果设备已在 deviceList 且拥有 token,立即回调并返回
|
|
|
@@ -177,6 +118,7 @@ object BleConnectionManager {
|
|
|
fun registerConnectListener(
|
|
|
mac: String,
|
|
|
connectNow: Boolean = false,
|
|
|
+ isSend: Boolean = true,
|
|
|
callBack: ((Boolean, BleBean?) -> Unit)? = null
|
|
|
) {
|
|
|
logger.info("蓝牙连接-开始连接 : $mac")
|
|
|
@@ -184,11 +126,15 @@ object BleConnectionManager {
|
|
|
logger.info("蓝牙连接-记录的设备:${deviceList}")
|
|
|
logger.info(
|
|
|
"蓝牙连接-是否连接:${
|
|
|
- BleManager.getInstance().isConnected(mac)
|
|
|
+ if (isSend) BleSendDispatcher.isConnected(mac) else BleReturnDispatcher.isConnected(
|
|
|
+ mac
|
|
|
+ )
|
|
|
},记录的设备是否存在:${deviceList.any { it.bleDevice.mac == mac }}"
|
|
|
)
|
|
|
deviceList.find {
|
|
|
- it.bleDevice.mac == mac && BleManager.getInstance().isConnected(mac) && it.token != null
|
|
|
+ it.bleDevice.mac == mac && (if (isSend) BleSendDispatcher.isConnected(mac) else BleReturnDispatcher.isConnected(
|
|
|
+ mac
|
|
|
+ )) && it.token != null
|
|
|
}?.let { bean ->
|
|
|
logger.info("蓝牙连接-设备已连接")
|
|
|
callBack?.invoke(true, bean)
|
|
|
@@ -196,8 +142,7 @@ object BleConnectionManager {
|
|
|
}
|
|
|
if (connectNow) {
|
|
|
logger.warn("蓝牙连接-立即连接 mac: $mac")
|
|
|
- BleManager.getInstance().disconnectAllDevice()
|
|
|
- deviceList.clear()
|
|
|
+ if (isSend) BleSendDispatcher.disconnectAll() else BleReturnDispatcher.disconnectAll()
|
|
|
unregisterConnectListener(mac)
|
|
|
currentConnectingMac.removeIf { it == mac }
|
|
|
}
|
|
|
@@ -210,14 +155,19 @@ object BleConnectionManager {
|
|
|
// 加入队列并启动连接
|
|
|
fun checkAndConnect(isDisconnectAll: Boolean = false) {
|
|
|
logger.warn("蓝牙连接-开始检查连接 mac: $mac")
|
|
|
- if (BleManager.getInstance().allConnectedDevice.size < maxConnectCount) {
|
|
|
+ val canConnect =
|
|
|
+ if (isSend) BleSendDispatcher.canConnect() else BleReturnDispatcher.canConnect()
|
|
|
+ if (canConnect) {
|
|
|
connectListeners.add(ConnectListener(mac, callBack))
|
|
|
- connectKey()
|
|
|
+ connectKey(isSend)
|
|
|
} else {
|
|
|
if (connectNow && !isDisconnectAll) {
|
|
|
logger.info("蓝牙连接-超过最大连接数,但是需要立即连接,断开所有连接进行连接")
|
|
|
- BleManager.getInstance().disconnectAllDevice()
|
|
|
- deviceList.clear()
|
|
|
+ if (isSend) {
|
|
|
+ BleSendDispatcher.disconnectAll()
|
|
|
+ } else {
|
|
|
+ BleReturnDispatcher.disconnectAll()
|
|
|
+ }
|
|
|
checkAndConnect(true)
|
|
|
} else {
|
|
|
ThreadUtils.runOnIODelayed(500) {
|
|
|
@@ -271,10 +221,12 @@ object BleConnectionManager {
|
|
|
/**
|
|
|
* 连接钥匙,单个mac走完prepare再进行下一个
|
|
|
*/
|
|
|
- private fun connectKey() {
|
|
|
+ private fun connectKey(isSend: Boolean = true) {
|
|
|
if (connectListeners.isEmpty()) return
|
|
|
- if (isPreparing || BleManager.getInstance().allConnectedDevice.size >= maxStandbyCount) {
|
|
|
- logger.debug("暂时不能连接:${isPreparing},${BleManager.getInstance().allConnectedDevice.size >= maxStandbyCount}")
|
|
|
+ val canConnect =
|
|
|
+ if (isSend) BleSendDispatcher.canConnect() else BleReturnDispatcher.canConnect()
|
|
|
+ if (isPreparing || !canConnect) {
|
|
|
+ logger.debug("暂时不能连接:${isPreparing},${canConnect}")
|
|
|
ThreadUtils.runOnMainDelayed(1000) { connectKey() }
|
|
|
return
|
|
|
}
|
|
|
@@ -292,7 +244,7 @@ object BleConnectionManager {
|
|
|
return
|
|
|
}
|
|
|
prepareBle(
|
|
|
- listener.mac, false
|
|
|
+ listener.mac, false, isSend
|
|
|
) { isDone, bleBean ->
|
|
|
ThreadUtils.runOnMain {
|
|
|
isPreparing = false
|
|
|
@@ -320,17 +272,20 @@ object BleConnectionManager {
|
|
|
private fun prepareBle(
|
|
|
mac: String,
|
|
|
isNeedLoading: Boolean = false,
|
|
|
+ isSend: Boolean,
|
|
|
prepareDoneCallBack: ((Boolean, BleBean?) -> Unit)?
|
|
|
) {
|
|
|
ThreadUtils.runOnMain {
|
|
|
- doScanBle(mac, isNeedLoading, 3, prepareDoneCallBack)
|
|
|
+ doScanBle(mac, isNeedLoading, 3, isSend, prepareDoneCallBack)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @SuppressLint("MissingPermission")
|
|
|
private fun doScanBle(
|
|
|
mac: String,
|
|
|
isNeedLoading: Boolean = false,
|
|
|
retryTimes: Int = 3,
|
|
|
+ isSend: Boolean,
|
|
|
prepareDoneCallBack: ((Boolean, BleBean?) -> Unit)?
|
|
|
) {
|
|
|
logger.info("蓝牙连接-doScanBle:$mac")
|
|
|
@@ -339,42 +294,47 @@ object BleConnectionManager {
|
|
|
override fun onPrompt(promptStr: String?) {
|
|
|
// 蓝牙未启动重试
|
|
|
logger.info("蓝牙连接-参数:${promptStr}")
|
|
|
- BleManager.getInstance().enableBluetooth()
|
|
|
ThreadUtils.runOnIODelayed(5000) {
|
|
|
- doScanBle(mac, isNeedLoading, 3, prepareDoneCallBack)
|
|
|
+ doScanBle(mac, isNeedLoading, 3, isSend, prepareDoneCallBack)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @RequiresPermission(Manifest.permission.BLUETOOTH_SCAN)
|
|
|
override fun onScanStarted(success: Boolean) {
|
|
|
logger.info("蓝牙连接-onScanStarted:${success}")
|
|
|
if (!success && retryTimes == 0) {
|
|
|
if (isNeedLoading) LoadingEvent.sendLoadingEvent(null, false)
|
|
|
prepareDoneCallBack?.invoke(false, null)
|
|
|
} else if (!success && retryTimes > 0) {
|
|
|
- BleManager.getInstance().cancelScan()
|
|
|
+ BleManager.cancelScan()
|
|
|
ThreadUtils.runOnMainDelayed(3000) {
|
|
|
- doScanBle(mac, isNeedLoading, retryTimes - 1, prepareDoneCallBack)
|
|
|
+ doScanBle(mac, isNeedLoading, retryTimes - 1, isSend, prepareDoneCallBack)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- override fun onScanning(bleDevice: BleDevice?) {
|
|
|
- val scanMac = bleDevice?.mac ?: return
|
|
|
+ @RequiresPermission(Manifest.permission.BLUETOOTH_SCAN)
|
|
|
+ override fun onLeScan(
|
|
|
+ oldDevice: BleDevice,
|
|
|
+ newDevice: BleDevice,
|
|
|
+ scannedBefore: Boolean
|
|
|
+ ) {
|
|
|
+ val scanMac = newDevice.mac
|
|
|
logger.info("蓝牙连接-onScanning:${mac}")
|
|
|
if (scanMac.equals(mac, ignoreCase = true)) {
|
|
|
// 找到目标设备,马上停止扫描
|
|
|
logger.info("找到目标设备 $mac,停止扫描并尝试连接")
|
|
|
- BleManager.getInstance().cancelScan()
|
|
|
+ BleManager.cancelScan()
|
|
|
// 立刻调用 doConnect,下一步进入连接流程
|
|
|
doConnect(
|
|
|
- bleDevice,
|
|
|
+ newDevice,
|
|
|
isNeedLoading,
|
|
|
prepareDoneCallBack
|
|
|
)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- override fun onScanFinished(scanResultList: MutableList<BleDevice>?) {
|
|
|
+ override fun onScanFinished(scanResultList: List<BleDevice>) {
|
|
|
logger.info("蓝牙连接-onScanFinished: $mac - ${scanResultList?.none { it.mac == mac }}")
|
|
|
if (isNeedLoading) LoadingEvent.sendLoadingEvent(null, false)
|
|
|
// 没有扫描到
|
|
|
@@ -384,12 +344,17 @@ object BleConnectionManager {
|
|
|
prepareDoneCallBack?.invoke(false, null)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ override fun onFilter(bleDevice: BleDevice): Boolean {
|
|
|
+ return bleDevice.name == BleConst.BLE_LOCAL_NAME
|
|
|
+ }
|
|
|
})
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 连接蓝牙设备
|
|
|
*/
|
|
|
+ @SuppressLint("MissingPermission")
|
|
|
private fun doConnect(
|
|
|
bleDevice: BleDevice,
|
|
|
isNeedLoading: Boolean = false,
|
|
|
@@ -400,10 +365,6 @@ object BleConnectionManager {
|
|
|
CommonUtils.getStr(R.string.ble_connecting), true
|
|
|
)
|
|
|
ThreadUtils.runOnIO {
|
|
|
- BleManager.getInstance().disconnect(bleDevice)
|
|
|
- deviceList.removeIf { it.bleDevice.mac == bleDevice.mac }
|
|
|
- logger.info("断开连接连接中断开:${bleDevice.mac}")
|
|
|
- delay(300)
|
|
|
BleUtil.Companion.instance?.connectBySelect(
|
|
|
bleDevice, object : CustomBleGattCallback() {
|
|
|
override fun onPrompt(promptStr: String?) {
|
|
|
@@ -413,9 +374,11 @@ object BleConnectionManager {
|
|
|
)
|
|
|
}
|
|
|
|
|
|
- override fun onStartConnect() {}
|
|
|
+ override fun onStartConnect(bleDevice: BleDevice) {
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- override fun onConnectFail(bleDevice: BleDevice?, exception: BleException?) {
|
|
|
+ override fun onConnectFail(bleDevice: BleDevice?, exception: BleException) {
|
|
|
if (isNeedLoading) LoadingEvent.sendLoadingEvent(
|
|
|
CommonUtils.getStr(R.string.ble_connect_fail), false
|
|
|
)
|
|
|
@@ -424,7 +387,9 @@ object BleConnectionManager {
|
|
|
}
|
|
|
|
|
|
override fun onConnectSuccess(
|
|
|
- bleDevice: BleDevice?, gatt: BluetoothGatt?, status: Int
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ gatt: BluetoothGatt?,
|
|
|
+ status: Int
|
|
|
) {
|
|
|
if (isNeedLoading) LoadingEvent.sendLoadingEvent(
|
|
|
null, false
|
|
|
@@ -450,7 +415,7 @@ object BleConnectionManager {
|
|
|
|
|
|
override fun onDisConnected(
|
|
|
isActiveDisConnected: Boolean,
|
|
|
- device: BleDevice?,
|
|
|
+ device: BleDevice,
|
|
|
gatt: BluetoothGatt?,
|
|
|
status: Int
|
|
|
) {
|
|
|
@@ -458,10 +423,6 @@ object BleConnectionManager {
|
|
|
null, false
|
|
|
)
|
|
|
logger.info("蓝牙连接-onDisConnected : ${device?.mac} - $isActiveDisConnected")
|
|
|
- getBleDeviceByMac(device?.mac)?.let {
|
|
|
- deviceList.remove(it)
|
|
|
- it.token = null
|
|
|
- }
|
|
|
bleDevice.mac?.let { itMac ->
|
|
|
unregisterConnectListener(itMac)
|
|
|
}
|
|
|
@@ -485,6 +446,10 @@ object BleConnectionManager {
|
|
|
ModBusController.updateKeyReadyStatus(bleDevice.mac, false, 3)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ override fun onConnectCancel(bleDevice: BleDevice, skip: Boolean) {
|
|
|
+ logger.info("蓝牙连接-onConnectCancel : ${bleDevice?.mac}")
|
|
|
+ }
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
@@ -517,13 +482,29 @@ object BleConnectionManager {
|
|
|
}
|
|
|
}
|
|
|
BleCmdManager.getCurrentStatus(bleDevice, object : CustomBleWriteCallback() {
|
|
|
- override fun onWriteSuccess(current: Int, total: Int, justWrite: ByteArray?) {
|
|
|
+ override fun onWriteSuccess(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ characteristic: BluetoothGattCharacteristic,
|
|
|
+ current: Int,
|
|
|
+ total: Int,
|
|
|
+ justWrite: ByteArray,
|
|
|
+ data: ByteArray
|
|
|
+ ) {
|
|
|
logger.info("getCurrentStatus success : ${bleDevice.mac}")
|
|
|
isTimeout = false
|
|
|
timeoutCallBack?.invoke(false)
|
|
|
}
|
|
|
|
|
|
- override fun onWriteFailure(exception: BleException?) {
|
|
|
+ override fun onWriteFailure(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ characteristic: BluetoothGattCharacteristic?,
|
|
|
+ exception: BleException,
|
|
|
+ current: Int,
|
|
|
+ total: Int,
|
|
|
+ justWrite: ByteArray?,
|
|
|
+ data: ByteArray?,
|
|
|
+ isTotalFail: Boolean
|
|
|
+ ) {
|
|
|
logger.info("getCurrentStatus fail : ${bleDevice.mac}")
|
|
|
isTimeout = false
|
|
|
ThreadUtils.runOnMainDelayed(1000) {
|
|
|
@@ -539,10 +520,27 @@ object BleConnectionManager {
|
|
|
fun getBatteryPower(bleDevice: BleDevice, isHeartBeat: Boolean = false) {
|
|
|
logger.debug("获取电池电量:${bleDevice.mac},是否为心跳:${isHeartBeat}")
|
|
|
BleCmdManager.getPower(bleDevice.mac, object : CustomBleWriteCallback() {
|
|
|
- override fun onWriteSuccess(p0: Int, p1: Int, p2: ByteArray?) {
|
|
|
+ override fun onWriteSuccess(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ characteristic: BluetoothGattCharacteristic,
|
|
|
+ current: Int,
|
|
|
+ total: Int,
|
|
|
+ justWrite: ByteArray,
|
|
|
+ data: ByteArray
|
|
|
+ ) {
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- override fun onWriteFailure(p0: BleException?) {
|
|
|
+ override fun onWriteFailure(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ characteristic: BluetoothGattCharacteristic?,
|
|
|
+ exception: BleException,
|
|
|
+ current: Int,
|
|
|
+ total: Int,
|
|
|
+ justWrite: ByteArray?,
|
|
|
+ data: ByteArray?,
|
|
|
+ isTotalFail: Boolean
|
|
|
+ ) {
|
|
|
ThreadUtils.runOnIODelayed(500) {
|
|
|
getBatteryPower(bleDevice)
|
|
|
}
|
|
|
@@ -576,6 +574,7 @@ object BleConnectionManager {
|
|
|
/**
|
|
|
* 监听蓝牙设备
|
|
|
*/
|
|
|
+ @SuppressLint("MissingPermission")
|
|
|
private fun indicate(
|
|
|
bleBean: BleBean?,
|
|
|
isNeedLoading: Boolean = false,
|
|
|
@@ -598,13 +597,20 @@ object BleConnectionManager {
|
|
|
logger.info("蓝牙连接-indicate onDisConnectPrompt : $promptStr")
|
|
|
}
|
|
|
|
|
|
- override fun onIndicateSuccess() {
|
|
|
+ override fun onIndicateSuccess(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ characteristic: BluetoothGattCharacteristic
|
|
|
+ ) {
|
|
|
logger.info("蓝牙连接-onIndicateSuccess")
|
|
|
isIndicateSuccess = true
|
|
|
getToken(bleBean, isNeedLoading, prepareDoneCallBack)
|
|
|
}
|
|
|
|
|
|
- override fun onIndicateFailure(exception: BleException?) {
|
|
|
+ override fun onIndicateFailure(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ characteristic: BluetoothGattCharacteristic?,
|
|
|
+ exception: BleException
|
|
|
+ ) {
|
|
|
if (isNeedLoading) LoadingEvent.sendLoadingEvent(null, false)
|
|
|
logger.error("蓝牙连接-onIndicateFailure : ${bleBean.bleDevice.mac} - ${exception?.description}")
|
|
|
ThreadUtils.runOnIODelayed(500) {
|
|
|
@@ -615,7 +621,11 @@ object BleConnectionManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- override fun onCharacteristicChanged(data: ByteArray?) {
|
|
|
+ override fun onCharacteristicChanged(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ characteristic: BluetoothGattCharacteristic,
|
|
|
+ data: ByteArray?
|
|
|
+ ) {
|
|
|
logger.info("蓝牙连接-onCharacteristicChanged : ${data?.toHexStrings()}")
|
|
|
if (bleIndicateListeners.isEmpty()) {
|
|
|
bleIndicateListeners.put(this, baseIndicateListeners)
|
|
|
@@ -628,6 +638,13 @@ object BleConnectionManager {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ override fun onIndicateCancel(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ characteristic: BluetoothGattCharacteristic
|
|
|
+ ) {
|
|
|
+ logger.info("蓝牙连接-onIndicateCancel : ${bleDevice.mac}")
|
|
|
+ }
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
@@ -643,12 +660,28 @@ object BleConnectionManager {
|
|
|
if (isNeedLoading) LoadingEvent.sendLoadingEvent("开始获取token...", true)
|
|
|
bleBean?.let {
|
|
|
BleCmdManager.getToken(it.bleDevice.mac, object : CustomBleWriteCallback() {
|
|
|
- override fun onWriteSuccess(current: Int, total: Int, justWrite: ByteArray?) {
|
|
|
+ override fun onWriteSuccess(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ characteristic: BluetoothGattCharacteristic,
|
|
|
+ current: Int,
|
|
|
+ total: Int,
|
|
|
+ justWrite: ByteArray,
|
|
|
+ data: ByteArray
|
|
|
+ ) {
|
|
|
if (isNeedLoading) LoadingEvent.sendLoadingEvent("token获取成功", true)
|
|
|
logger.info("getToken success : ${bleBean.bleDevice.mac}")
|
|
|
}
|
|
|
|
|
|
- override fun onWriteFailure(exception: BleException?) {
|
|
|
+ override fun onWriteFailure(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ characteristic: BluetoothGattCharacteristic?,
|
|
|
+ exception: BleException,
|
|
|
+ current: Int,
|
|
|
+ total: Int,
|
|
|
+ justWrite: ByteArray?,
|
|
|
+ data: ByteArray?,
|
|
|
+ isTotalFail: Boolean
|
|
|
+ ) {
|
|
|
if (isNeedLoading) LoadingEvent.sendLoadingEvent("token获取失败", true)
|
|
|
logger.error("getToken fail : ${bleBean.bleDevice.mac}")
|
|
|
prepareDoneCallBack?.invoke(false, null)
|
|
|
@@ -662,7 +695,11 @@ object BleConnectionManager {
|
|
|
* 1. 先尝试不充电连接,若成功就返回 true;
|
|
|
* 2. 否则开启“充电”,等 500ms,再尝试一次连接,连接成功后断电并返回 true;否则返回 false。
|
|
|
*/
|
|
|
- suspend fun tryConnectWithOptionalCharge(mac: String, withOpenCharge: Boolean = true): Boolean =
|
|
|
+ suspend fun tryConnectWithOptionalCharge(
|
|
|
+ mac: String,
|
|
|
+ withOpenCharge: Boolean = true,
|
|
|
+ isSend: Boolean
|
|
|
+ ): Boolean =
|
|
|
withContext(Dispatchers.IO) {
|
|
|
// -------- 第二次尝试:先开电,再连 --------
|
|
|
// 开电,并等待回调
|
|
|
@@ -683,13 +720,13 @@ object BleConnectionManager {
|
|
|
// 再次注册连接监听
|
|
|
val secondTry = suspendCancellableCoroutine<Boolean> { cont ->
|
|
|
var isCalled = false
|
|
|
- registerConnectListener(mac, true) { isDone, _ ->
|
|
|
+ registerConnectListener(mac, true, isSend) { isDone, _ ->
|
|
|
if (isCalled) {
|
|
|
return@registerConnectListener
|
|
|
}
|
|
|
isCalled = true
|
|
|
// 无论成功或失败,都先把电关掉
|
|
|
-// ModBusController.controlKeyCharge(false, mac) { }
|
|
|
+ ModBusController.controlKeyCharge(false, mac) { }
|
|
|
if (cont.isActive) {
|
|
|
cont.resume(isDone)
|
|
|
}
|