[6.8.0]
[fea] [连接保持10s、同时连接6个后会自动尝试关闭最早的一个]
This commit is contained in:
@@ -30,7 +30,8 @@ object BleManager : BaseBluetoothManager() {
|
||||
|
||||
val sendDataIntDev = mutableListOf<OchBluetoothGattCallback>()
|
||||
|
||||
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<OchBluetoothGattCallback>)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user