|
|
@@ -1,10 +1,9 @@
|
|
|
package com.grkj.iscs_mars.ble
|
|
|
|
|
|
+import android.annotation.SuppressLint
|
|
|
import android.bluetooth.BluetoothGatt
|
|
|
+import android.bluetooth.BluetoothGattCharacteristic
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
-import com.clj.fastble.BleManager
|
|
|
-import com.clj.fastble.data.BleDevice
|
|
|
-import com.clj.fastble.exception.BleException
|
|
|
import com.grkj.iscs_mars.BusinessManager
|
|
|
import com.grkj.iscs_mars.BusinessManager.deviceList
|
|
|
import com.grkj.iscs_mars.BusinessManager.getBleDeviceByMac
|
|
|
@@ -16,6 +15,7 @@ import com.grkj.iscs_mars.R
|
|
|
import com.grkj.iscs_mars.extentions.toHexStrings
|
|
|
import com.grkj.iscs_mars.modbus.ModBusController
|
|
|
import com.grkj.iscs_mars.modbus.ModBusController.controlKeyCharge
|
|
|
+import com.grkj.iscs_mars.model.Constants
|
|
|
import com.grkj.iscs_mars.model.Constants.PERMISSION_REQUEST_CODE
|
|
|
import com.grkj.iscs_mars.model.eventmsg.LoadingMsg
|
|
|
import com.grkj.iscs_mars.model.eventmsg.MsgEvent
|
|
|
@@ -26,6 +26,9 @@ import com.grkj.iscs_mars.util.CommonUtils
|
|
|
import com.grkj.iscs_mars.util.Executor
|
|
|
import com.grkj.iscs_mars.util.log.LogUtil
|
|
|
import com.grkj.iscs_mars.view.base.BaseActivity
|
|
|
+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.thread.ThreadUtils
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
@@ -78,12 +81,23 @@ object BleConnectionManager {
|
|
|
fun registerConnectListener(
|
|
|
mac: String,
|
|
|
connectNow: Boolean = false,
|
|
|
+ isSend: Boolean = true,
|
|
|
callBack: ((Boolean, BleBean?) -> Unit)? = null
|
|
|
) {
|
|
|
LogUtil.i("蓝牙连接-开始连接 : $mac")
|
|
|
// 已连接且已获取 token
|
|
|
+ LogUtil.i("蓝牙连接-记录的设备:${deviceList}")
|
|
|
+ LogUtil.i(
|
|
|
+ "蓝牙连接-是否连接:${
|
|
|
+ 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 ->
|
|
|
LogUtil.i("蓝牙连接-设备已连接")
|
|
|
callBack?.invoke(true, bean)
|
|
|
@@ -91,7 +105,9 @@ object BleConnectionManager {
|
|
|
}
|
|
|
if (connectNow) {
|
|
|
LogUtil.w("蓝牙连接-立即连接 mac: $mac")
|
|
|
+ if (isSend) BleSendDispatcher.disconnectAll() else BleReturnDispatcher.disconnectAll()
|
|
|
unregisterConnectListener(mac)
|
|
|
+ currentConnectingMac.removeIf { it == mac }
|
|
|
}
|
|
|
// 重复注册检查
|
|
|
if (currentConnectingMac.contains(mac)) {
|
|
|
@@ -102,14 +118,22 @@ object BleConnectionManager {
|
|
|
// 加入队列并启动连接
|
|
|
fun checkAndConnect(isDisconnectAll: Boolean = false) {
|
|
|
LogUtil.w("蓝牙连接-开始检查连接 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) {
|
|
|
LogUtil.i("蓝牙连接-超过最大连接数,但是需要立即连接,断开所有连接进行连接")
|
|
|
- BleManager.getInstance().disconnectAllDevice()
|
|
|
- deviceList.clear()
|
|
|
+ if (isSend) {
|
|
|
+ BleSendDispatcher.disconnectAll()
|
|
|
+ } else {
|
|
|
+ BleReturnDispatcher.disconnectAll()
|
|
|
+ }
|
|
|
checkAndConnect(true)
|
|
|
} else {
|
|
|
ThreadUtils.runOnIODelayed(500) {
|
|
|
@@ -149,11 +173,16 @@ object BleConnectionManager {
|
|
|
/**
|
|
|
* 连接钥匙,单个mac走完prepare再进行下一个
|
|
|
*/
|
|
|
- private fun connectKey() {
|
|
|
+ private fun connectKey(isSend: Boolean) {
|
|
|
if (connectListeners.isEmpty()) return
|
|
|
- if (isPreparing || BleManager.getInstance().allConnectedDevice.size > maxStandbyCount) {
|
|
|
- LogUtil.i("暂时不能连接:${isPreparing},${BleManager.getInstance().allConnectedDevice.size > maxStandbyCount}")
|
|
|
- Executor.delayOnMain(1000) { connectKey() }
|
|
|
+ val canConnect = if (isSend) {
|
|
|
+ BleSendDispatcher.canConnect()
|
|
|
+ } else {
|
|
|
+ BleReturnDispatcher.canConnect()
|
|
|
+ }
|
|
|
+ if (isPreparing || !canConnect) {
|
|
|
+ LogUtil.i("暂时不能连接:${isPreparing},${canConnect}")
|
|
|
+ Executor.delayOnMain(1000) { connectKey(isSend) }
|
|
|
return
|
|
|
}
|
|
|
val listener = connectListeners.first()
|
|
|
@@ -189,7 +218,7 @@ object BleConnectionManager {
|
|
|
listener.mac
|
|
|
)
|
|
|
}
|
|
|
- if (connectListeners.isNotEmpty()) connectKey()
|
|
|
+ if (connectListeners.isNotEmpty()) connectKey(isSend)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -212,6 +241,7 @@ object BleConnectionManager {
|
|
|
}
|
|
|
|
|
|
@AfterPermissionGranted(PERMISSION_REQUEST_CODE)
|
|
|
+ @SuppressLint("MissingPermission")
|
|
|
private fun doScanBle(
|
|
|
mac: String,
|
|
|
isNeedLoading: Boolean = false,
|
|
|
@@ -228,7 +258,6 @@ object BleConnectionManager {
|
|
|
override fun onPrompt(promptStr: String?) {
|
|
|
// 蓝牙未启动重试
|
|
|
LogUtil.i("蓝牙连接-参数:${promptStr}")
|
|
|
- BleManager.getInstance().enableBluetooth()
|
|
|
doScanBle(mac, isNeedLoading, retryTimes, prepareDoneCallBack)
|
|
|
}
|
|
|
|
|
|
@@ -242,26 +271,30 @@ object BleConnectionManager {
|
|
|
)
|
|
|
prepareDoneCallBack?.invoke(false, null)
|
|
|
} else if (!success && retryTimes > 0) {
|
|
|
- BleManager.getInstance().cancelScan()
|
|
|
+ BleManager.cancelScan()
|
|
|
ThreadUtils.runOnMainDelayed(3000) {
|
|
|
doScanBle(mac, isNeedLoading, retryTimes - 1, prepareDoneCallBack)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- override fun onScanning(bleDevice: BleDevice?) {
|
|
|
- val bleMac = bleDevice?.mac ?: return
|
|
|
+ override fun onLeScan(
|
|
|
+ oldDevice: BleDevice,
|
|
|
+ newDevice: BleDevice,
|
|
|
+ scannedBefore: Boolean
|
|
|
+ ) {
|
|
|
+ val bleMac = newDevice.mac ?: return
|
|
|
LogUtil.i("蓝牙连接-onScanning:$bleMac")
|
|
|
if (mac.equals(bleMac, ignoreCase = true)) {
|
|
|
// 找到目标设备,马上停止扫描
|
|
|
LogUtil.i("蓝牙连接-找到目标设备 $bleMac,停止扫描并尝试连接")
|
|
|
- BleManager.getInstance().cancelScan()
|
|
|
+ BleManager.cancelScan()
|
|
|
// 立刻调用 doConnect,下一步进入连接流程
|
|
|
- doConnect(bleDevice, isNeedLoading, prepareDoneCallBack)
|
|
|
+ doConnect(newDevice, isNeedLoading, prepareDoneCallBack)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- override fun onScanFinished(scanResultList: MutableList<BleDevice>?) {
|
|
|
+ override fun onScanFinished(scanResultList: List<BleDevice>) {
|
|
|
LogUtil.i("蓝牙连接-onScanFinished: $mac - ${scanResultList?.none { it.mac == mac }}")
|
|
|
if (isNeedLoading) sendEventMsg(
|
|
|
MsgEvent(
|
|
|
@@ -274,12 +307,17 @@ object BleConnectionManager {
|
|
|
prepareDoneCallBack?.invoke(false, null)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ override fun onFilter(bleDevice: BleDevice): Boolean {
|
|
|
+ return bleDevice.name == Constants.BLE_LOCAL_NAME
|
|
|
+ }
|
|
|
})
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 连接蓝牙设备
|
|
|
*/
|
|
|
+ @SuppressLint("MissingPermission")
|
|
|
private fun doConnect(
|
|
|
bleDevice: BleDevice,
|
|
|
isNeedLoading: Boolean = false,
|
|
|
@@ -293,8 +331,6 @@ object BleConnectionManager {
|
|
|
)
|
|
|
)
|
|
|
ThreadUtils.runOnIO {
|
|
|
- BleManager.getInstance().disconnect(bleDevice)
|
|
|
- deviceList.removeIf { device -> device.bleDevice.mac == bleDevice.mac }
|
|
|
delay(300)
|
|
|
BleUtil.instance?.connectBySelect(
|
|
|
bleDevice, object : CustomBleGattCallback() {
|
|
|
@@ -307,11 +343,11 @@ object BleConnectionManager {
|
|
|
)
|
|
|
}
|
|
|
|
|
|
- override fun onStartConnect() {
|
|
|
+ override fun onStartConnect(bleDevice: BleDevice) {
|
|
|
LogUtil.i("蓝牙连接-开始连接")
|
|
|
}
|
|
|
|
|
|
- override fun onConnectFail(bleDevice: BleDevice?, exception: BleException?) {
|
|
|
+ override fun onConnectFail(bleDevice: BleDevice?, exception: BleException) {
|
|
|
if (isNeedLoading) sendEventMsg(
|
|
|
MsgEvent(
|
|
|
MSG_EVENT_LOADING,
|
|
|
@@ -327,7 +363,9 @@ object BleConnectionManager {
|
|
|
}
|
|
|
|
|
|
override fun onConnectSuccess(
|
|
|
- bleDevice: BleDevice?, gatt: BluetoothGatt?, status: Int
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ gatt: BluetoothGatt?,
|
|
|
+ status: Int
|
|
|
) {
|
|
|
if (isNeedLoading) sendEventMsg(
|
|
|
MsgEvent(
|
|
|
@@ -353,7 +391,7 @@ object BleConnectionManager {
|
|
|
|
|
|
override fun onDisConnected(
|
|
|
isActiveDisConnected: Boolean,
|
|
|
- device: BleDevice?,
|
|
|
+ device: BleDevice,
|
|
|
gatt: BluetoothGatt?,
|
|
|
status: Int
|
|
|
) {
|
|
|
@@ -363,12 +401,10 @@ object BleConnectionManager {
|
|
|
)
|
|
|
)
|
|
|
LogUtil.i("蓝牙连接-onDisConnected : ${device?.mac} - $isActiveDisConnected")
|
|
|
- getBleDeviceByMac(device?.mac)?.let {
|
|
|
- deviceList.remove(it)
|
|
|
- it.token = null
|
|
|
- }
|
|
|
- bleDevice.mac?.let { itMac ->
|
|
|
- unregisterConnectListener(itMac)
|
|
|
+ bleDevice.mac.let { itMac ->
|
|
|
+ unregisterConnectListener(
|
|
|
+ itMac
|
|
|
+ )
|
|
|
}
|
|
|
if (!isActiveDisConnected) {
|
|
|
// 测试模式下不重连
|
|
|
@@ -390,6 +426,15 @@ object BleConnectionManager {
|
|
|
ModBusController.updateKeyReadyStatus(bleDevice.mac, false, 3)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ override fun onConnectCancel(bleDevice: BleDevice, skip: Boolean) {
|
|
|
+ if (isNeedLoading) sendEventMsg(
|
|
|
+ MsgEvent(
|
|
|
+ MSG_EVENT_LOADING, LoadingMsg(false, null, false)
|
|
|
+ )
|
|
|
+ )
|
|
|
+ LogUtil.i("蓝牙连接-onConnectCancel : ${bleDevice?.mac}")
|
|
|
+ }
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
@@ -423,13 +468,20 @@ object BleConnectionManager {
|
|
|
LogUtil.i("蓝牙连接-indicate onDisConnectPrompt : $promptStr")
|
|
|
}
|
|
|
|
|
|
- override fun onIndicateSuccess() {
|
|
|
+ override fun onIndicateSuccess(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ characteristic: BluetoothGattCharacteristic
|
|
|
+ ) {
|
|
|
LogUtil.i("蓝牙连接-onIndicateSuccess")
|
|
|
isIndicateSuccess = true
|
|
|
getToken(bleBean, isNeedLoading, prepareDoneCallBack)
|
|
|
}
|
|
|
|
|
|
- override fun onIndicateFailure(exception: BleException?) {
|
|
|
+ override fun onIndicateFailure(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ characteristic: BluetoothGattCharacteristic?,
|
|
|
+ exception: BleException
|
|
|
+ ) {
|
|
|
if (isNeedLoading) sendEventMsg(
|
|
|
MsgEvent(
|
|
|
MSG_EVENT_LOADING, LoadingMsg(false, null, false)
|
|
|
@@ -444,7 +496,11 @@ object BleConnectionManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- override fun onCharacteristicChanged(data: ByteArray?) {
|
|
|
+ override fun onCharacteristicChanged(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ characteristic: BluetoothGattCharacteristic,
|
|
|
+ data: ByteArray?
|
|
|
+ ) {
|
|
|
LogUtil.i("蓝牙连接-onCharacteristicChanged : ${data?.toHexStrings()}")
|
|
|
data?.let { itData ->
|
|
|
BusinessManager.handleRsp(
|
|
|
@@ -455,6 +511,13 @@ object BleConnectionManager {
|
|
|
)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ override fun onIndicateCancel(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ characteristic: BluetoothGattCharacteristic
|
|
|
+ ) {
|
|
|
+ LogUtil.i("蓝牙连接-onIndicateCancel")
|
|
|
+ }
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
@@ -478,7 +541,14 @@ object BleConnectionManager {
|
|
|
)
|
|
|
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) sendEventMsg(
|
|
|
MsgEvent(
|
|
|
MSG_EVENT_LOADING, LoadingMsg(false, "token获取成功", null)
|
|
|
@@ -487,7 +557,16 @@ object BleConnectionManager {
|
|
|
LogUtil.i("蓝牙连接-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) sendEventMsg(
|
|
|
MsgEvent(
|
|
|
MSG_EVENT_LOADING, LoadingMsg(false, "token获取失败", false)
|
|
|
@@ -505,34 +584,12 @@ 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 = true
|
|
|
+ ): Boolean =
|
|
|
withContext(Dispatchers.IO) {
|
|
|
- // -------- 第一次尝试 --------
|
|
|
-// LogUtil.i("蓝牙连接-第一次尝试")
|
|
|
-// val firstTry = suspendCancellableCoroutine<Boolean> { cont ->
|
|
|
-// // 1. 定义一个 flag,确保只 resume 一次
|
|
|
-// var isCalled = false
|
|
|
-// BusinessManager.registerConnectListener(mac, true) { isDone, _ ->
|
|
|
-// if (isCalled) {
|
|
|
-// return@registerConnectListener
|
|
|
-// }
|
|
|
-// isCalled = true
|
|
|
-// if (isDone && cont.isActive) {
|
|
|
-// // 连接成功后,把电关掉(以防万一)
|
|
|
-// controlKeyCharge(false, mac) { }
|
|
|
-// cont.resume(true)
|
|
|
-// } else {
|
|
|
-// cont.resume(false)
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }
|
|
|
-// LogUtil.i("蓝牙连接-第一次连接:${firstTry},是否继续尝试上电连接${withOpenCharge}")
|
|
|
-// if (firstTry) {
|
|
|
-// return@withContext true
|
|
|
-// }
|
|
|
-// if (!withOpenCharge) {
|
|
|
-// return@withContext false
|
|
|
-// }
|
|
|
// -------- 第二次尝试:先开电,再连 --------
|
|
|
// 开电,并等待回调
|
|
|
suspendCoroutine<Unit> { unitCont ->
|
|
|
@@ -552,13 +609,13 @@ object BleConnectionManager {
|
|
|
// 再次注册连接监听
|
|
|
val secondTry = suspendCancellableCoroutine<Boolean> { cont ->
|
|
|
var isCalled = false
|
|
|
- BusinessManager.registerConnectListener(mac, true) { isDone, _ ->
|
|
|
+ BusinessManager.registerConnectListener(mac, true, isSend) { isDone, _ ->
|
|
|
if (isCalled) {
|
|
|
return@registerConnectListener
|
|
|
}
|
|
|
isCalled = true
|
|
|
// 无论成功或失败,都先把电关掉
|
|
|
-// controlKeyCharge(false, mac) { }
|
|
|
+ controlKeyCharge(false, mac) { }
|
|
|
if (cont.isActive) {
|
|
|
cont.resume(isDone)
|
|
|
}
|
|
|
@@ -576,7 +633,6 @@ object BleConnectionManager {
|
|
|
override fun onPrompt(promptStr: String?) {
|
|
|
// 蓝牙未启动重试
|
|
|
LogUtil.i("设备录入-参数:${promptStr}")
|
|
|
- BleManager.getInstance().enableBluetooth()
|
|
|
ThreadUtils.runOnMainDelayed(300) {
|
|
|
scanOnlineKeyLockMacAndSwitchModeToClose()
|
|
|
}
|
|
|
@@ -591,12 +647,16 @@ object BleConnectionManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- override fun onScanning(bleDevice: BleDevice?) {
|
|
|
- val mac = bleDevice?.mac ?: return
|
|
|
+ override fun onLeScan(
|
|
|
+ oldDevice: BleDevice,
|
|
|
+ newDevice: BleDevice,
|
|
|
+ scannedBefore: Boolean
|
|
|
+ ) {
|
|
|
+ val mac = newDevice?.mac ?: return
|
|
|
LogUtil.i("设备录入-onScanning:$mac")
|
|
|
}
|
|
|
|
|
|
- override fun onScanFinished(scanResultList: MutableList<BleDevice>?) {
|
|
|
+ override fun onScanFinished(scanResultList: List<BleDevice>) {
|
|
|
val devicesSnapshot = scanResultList?.toList().orEmpty()
|
|
|
ThreadUtils.runOnIO {
|
|
|
val handlerDeviceSize = AtomicInteger(0)
|
|
|
@@ -615,6 +675,10 @@ object BleConnectionManager {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ override fun onFilter(bleDevice: BleDevice): Boolean {
|
|
|
+ return bleDevice.name == Constants.BLE_LOCAL_NAME
|
|
|
+ }
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
@@ -629,9 +693,12 @@ object BleConnectionManager {
|
|
|
bleDevice = bleDevice,
|
|
|
callback = object : CustomBleWriteCallback() {
|
|
|
override fun onWriteSuccess(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ characteristic: BluetoothGattCharacteristic,
|
|
|
current: Int,
|
|
|
total: Int,
|
|
|
- justWrite: ByteArray?
|
|
|
+ justWrite: ByteArray,
|
|
|
+ data: ByteArray
|
|
|
) {
|
|
|
ThreadUtils.runOnIO {
|
|
|
delay(3000)
|
|
|
@@ -639,7 +706,16 @@ object BleConnectionManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- override fun onWriteFailure(exception: BleException?) {
|
|
|
+ override fun onWriteFailure(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ characteristic: BluetoothGattCharacteristic?,
|
|
|
+ exception: BleException,
|
|
|
+ current: Int,
|
|
|
+ total: Int,
|
|
|
+ justWrite: ByteArray?,
|
|
|
+ data: ByteArray?,
|
|
|
+ isTotalFail: Boolean
|
|
|
+ ) {
|
|
|
ThreadUtils.runOnMainDelayed(300) {
|
|
|
cont.resume(sendEmptyTicketJson(bleDevice))
|
|
|
}
|
|
|
@@ -658,12 +734,14 @@ object BleConnectionManager {
|
|
|
bleDevice,
|
|
|
object : CustomBleWriteCallback() {
|
|
|
override fun onWriteSuccess(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ characteristic: BluetoothGattCharacteristic,
|
|
|
current: Int,
|
|
|
total: Int,
|
|
|
- justWrite: ByteArray?
|
|
|
+ justWrite: ByteArray,
|
|
|
+ data: ByteArray
|
|
|
) {
|
|
|
- BleManager.getInstance().disconnect(bleDevice)
|
|
|
- deviceList.removeIf { device -> device.bleDevice.mac == bleDevice.mac }
|
|
|
+ BleSendDispatcher.scheduleDisconnect(bleDevice.mac)
|
|
|
ThreadUtils.runOnIO {
|
|
|
delay(800)
|
|
|
cont.resume(true)
|
|
|
@@ -671,7 +749,16 @@ object BleConnectionManager {
|
|
|
LogUtil.i("设备录入-切换模式发送成功 : ${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
|
|
|
+ ) {
|
|
|
LogUtil.e("设备录入-切换模式发送失败 : ${bleDevice.mac}")
|
|
|
ThreadUtils.runOnMainDelayed(300) {
|
|
|
cont.resume(sendEmptyTicketJson(bleDevice))
|
|
|
@@ -681,39 +768,15 @@ object BleConnectionManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 切换待机模式
|
|
|
- */
|
|
|
- fun switchReadyMode(bleDevice: BleDevice) {
|
|
|
- BleCmdManager.switchMode(
|
|
|
- BleConst.STATUS_READY,
|
|
|
- bleDevice,
|
|
|
- object : CustomBleWriteCallback() {
|
|
|
- override fun onWriteSuccess(
|
|
|
- current: Int,
|
|
|
- total: Int,
|
|
|
- justWrite: ByteArray?
|
|
|
- ) {
|
|
|
- BleManager.getInstance().disconnect(bleDevice)
|
|
|
- deviceList.removeIf { device -> device.bleDevice.mac == bleDevice.mac }
|
|
|
- LogUtil.i("设备录入-切换模式发送成功 : ${bleDevice.mac}")
|
|
|
- }
|
|
|
-
|
|
|
- override fun onWriteFailure(exception: BleException?) {
|
|
|
- LogUtil.e("设备录入-切换模式发送失败 : ${bleDevice.mac}")
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 扫描在线的蓝牙
|
|
|
*/
|
|
|
+ @SuppressLint("MissingPermission")
|
|
|
fun scanOnlineKeyLockMac(existsMac: List<String>, callback: (BleDevice?) -> Unit) {
|
|
|
BleUtil.instance?.scan(object : CustomBleScanCallback() {
|
|
|
override fun onPrompt(promptStr: String?) {
|
|
|
// 蓝牙未启动重试
|
|
|
LogUtil.d("设备录入-参数:${promptStr}")
|
|
|
- BleManager.getInstance().enableBluetooth()
|
|
|
ThreadUtils.runOnMainDelayed(300) {
|
|
|
scanOnlineKeyLockMac(existsMac, callback)
|
|
|
}
|
|
|
@@ -728,18 +791,26 @@ object BleConnectionManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- override fun onScanning(bleDevice: BleDevice?) {
|
|
|
- val mac = bleDevice?.mac ?: return
|
|
|
+ override fun onLeScan(
|
|
|
+ oldDevice: BleDevice,
|
|
|
+ newDevice: BleDevice,
|
|
|
+ scannedBefore: Boolean
|
|
|
+ ) {
|
|
|
+ val mac = newDevice?.mac ?: return
|
|
|
LogUtil.d("设备录入-onScanning:$mac")
|
|
|
if (mac !in existsMac) {
|
|
|
- callback(bleDevice)
|
|
|
- BleManager.getInstance().cancelScan()
|
|
|
+ callback(newDevice)
|
|
|
+ BleManager.cancelScan()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- override fun onScanFinished(scanResultList: MutableList<BleDevice>?) {
|
|
|
+ override fun onScanFinished(scanResultList: List<BleDevice>) {
|
|
|
LogUtil.d("设备录入-扫描完成:$scanResultList")
|
|
|
}
|
|
|
+
|
|
|
+ override fun onFilter(bleDevice: BleDevice): Boolean {
|
|
|
+ return bleDevice.name == Constants.BLE_LOCAL_NAME
|
|
|
+ }
|
|
|
})
|
|
|
}
|
|
|
|