From d206a02a1703bb35e47899d3a5ce472c57d0ea1e Mon Sep 17 00:00:00 2001 From: yangyakun Date: Mon, 25 Nov 2024 11:40:30 +0800 Subject: [PATCH] =?UTF-8?q?[6.8.0]=20[fea]=20[=E8=BF=9E=E6=8E=A5=E4=BF=9D?= =?UTF-8?q?=E6=8C=8110s=E3=80=81=E5=90=8C=E6=97=B6=E8=BF=9E=E6=8E=A56?= =?UTF-8?q?=E4=B8=AA=E5=90=8E=E4=BC=9A=E8=87=AA=E5=8A=A8=E5=B0=9D=E8=AF=95?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E6=9C=80=E6=97=A9=E7=9A=84=E4=B8=80=E4=B8=AA?= =?UTF-8?q?]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/manager/bluetooth/BleManager.kt | 11 +++-- .../bluetooth/OchBluetoothGattCallback.kt | 40 ++++++++++++------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/OCH/common/common/src/main/java/com/mogo/och/common/module/manager/bluetooth/BleManager.kt b/OCH/common/common/src/main/java/com/mogo/och/common/module/manager/bluetooth/BleManager.kt index 01cf3cb78e..7e30569e4b 100644 --- a/OCH/common/common/src/main/java/com/mogo/och/common/module/manager/bluetooth/BleManager.kt +++ b/OCH/common/common/src/main/java/com/mogo/och/common/module/manager/bluetooth/BleManager.kt @@ -30,7 +30,8 @@ object BleManager : BaseBluetoothManager() { val sendDataIntDev = mutableListOf() - var listener: BleDevListener? = null + // 最多同时保持6个链接 + var maxConnectCount = 6 private val leScanCallback = object : ScanCallback(){ @SuppressLint("MissingPermission") @@ -42,7 +43,6 @@ object BleManager : BaseBluetoothManager() { scanList.add(bleDevItem) } Log.d(TAG, "callbackType:${callbackType}_____result:${result}") - listener?.dataChange(scanList) } } } @@ -92,6 +92,9 @@ object BleManager : BaseBluetoothManager() { it.connectGattAndSend(data) scanList.remove(it) sendDataIntDev.add(it) + if (sendDataIntDev.size > maxConnectCount) { + sendDataIntDev.first().tryCloseConn() + } return true } return false @@ -102,8 +105,4 @@ object BleManager : BaseBluetoothManager() { scanList.add(ochBluetoothGattCallback) } - interface BleDevListener{ - fun dataChange(scanList:MutableList) - } - } \ No newline at end of file diff --git a/OCH/common/common/src/main/java/com/mogo/och/common/module/manager/bluetooth/OchBluetoothGattCallback.kt b/OCH/common/common/src/main/java/com/mogo/och/common/module/manager/bluetooth/OchBluetoothGattCallback.kt index 37a8161bdd..1fe3fe86b9 100644 --- a/OCH/common/common/src/main/java/com/mogo/och/common/module/manager/bluetooth/OchBluetoothGattCallback.kt +++ b/OCH/common/common/src/main/java/com/mogo/och/common/module/manager/bluetooth/OchBluetoothGattCallback.kt @@ -43,27 +43,26 @@ class OchBluetoothGattCallback(device: BluetoothDevice) : BluetoothGattCallback( } private val TAG = "GattCallback" + + /** + * @param + */ override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) { val dev = gatt.device Log.i(TAG, "${device.name}:onConnectionStateChange:${dev.name},${dev.address},${status},${newState},${dev.type}") - if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED) { - isConnected = true - gatt.discoverServices() //启动服务发现 - } else { - isConnected = false - closeConn() - } - if(status==0){ - if(newState==2){ + if(status==BluetoothGatt.GATT_SUCCESS){ + if(newState==BluetoothProfile.STATE_CONNECTED){ Log.i(TAG,"与[${dev.name}]连接成功") + isConnected = true + gatt.discoverServices() //启动服务发现 }else{ + isConnected = false + connect() Log.i(TAG,"与[${dev.name}]连接断开") } }else{ Log.i(TAG,"与[${dev.name}]连接出错,错误码:$status") - if(status==133){ - connect() - } + connect() } } @@ -104,7 +103,7 @@ class OchBluetoothGattCallback(device: BluetoothDevice) : BluetoothGattCallback( waitSendData.remove(valueStr) if(waitSendData.isEmpty()){ RxUtils.disposeSubscribe(timeoutCLose) - timeoutCLose = RxUtils.createSubscribeOnOwnThread(30_000) { + timeoutCLose = RxUtils.createSubscribeOnOwnThread(10_000) { closeConn() } } @@ -121,12 +120,23 @@ class OchBluetoothGattCallback(device: BluetoothDevice) : BluetoothGattCallback( Log.i(TAG,"onDescriptorWrite:${gatt.device.name},${gatt.device.address},$uuid,$valueStr,$status") } + fun tryCloseConn(){ + if(isConnected){ + if (waitSendData.isNotEmpty()) { + return + }else{ + closeConn() + } + } + } + // BLE中心设备连接外围设备的数量有限(大概2~7个),在建立新连接之前必须释放旧连接资源,否则容易出现连接错误133 fun closeConn() { if (mBluetoothGatt != null) { - mBluetoothGatt!!.disconnect() - mBluetoothGatt!!.close() + mBluetoothGatt?.disconnect() + mBluetoothGatt?.close() mBluetoothGatt = null + currentTryCount = 0 isConnected = false BleManager.copy2ScanList(this) }