[6.8.0]
[fix] [蓝牙连接失败重试机制修改]
This commit is contained in:
@@ -127,13 +127,20 @@ object BleManager : BaseBluetoothManager() {
|
||||
if(!scanList.contains(ochBluetoothGattCallback)){
|
||||
scanList.add(ochBluetoothGattCallback)
|
||||
}
|
||||
Log.d(TAG1, "超时返回 $ochBluetoothGattCallback scanList个数:${scanList.size}___sendDataIntDev个数:${sendDataIntDev.size}")
|
||||
Log.d(TAG1, "超时返回 $ochBluetoothGattCallback scanList个数:${scanList}___sendDataIntDev个数:${sendDataIntDev}")
|
||||
}
|
||||
|
||||
fun removeFromScandAndWrite(ochBluetoothGattCallback: OchBluetoothGattCallback) {
|
||||
@SuppressLint("MissingPermission")
|
||||
fun removeFromScandAndWrite(ochBluetoothGattCallback: OchBluetoothGattCallback,trySend:Boolean) {
|
||||
sendDataIntDev.remove(ochBluetoothGattCallback)
|
||||
scanList.remove(ochBluetoothGattCallback)
|
||||
Log.d(TAG1, "链接超次 $ochBluetoothGattCallback scanList个数:${scanList.size}___sendDataIntDev个数:${sendDataIntDev.size}")
|
||||
if(scanList.contains(ochBluetoothGattCallback)){
|
||||
val temp = ochBluetoothGattCallback.getSendData()
|
||||
// 只发送第一条
|
||||
temp.firstNotNullOf {data->
|
||||
sendData2Wx(ochBluetoothGattCallback.device.name,data.key)
|
||||
}
|
||||
}
|
||||
Log.d(TAG1, "链接超次 $ochBluetoothGattCallback scanList个数:${scanList}___sendDataIntDev个数:${sendDataIntDev}")
|
||||
}
|
||||
|
||||
}
|
||||
@@ -45,23 +45,26 @@ class OchBluetoothGattCallback(device: BluetoothDevice) : BluetoothGattCallback(
|
||||
|
||||
private var isDestory = false
|
||||
|
||||
private var connectStartTime = 0L
|
||||
|
||||
private val connectTimeOUt = 25_000
|
||||
|
||||
|
||||
var isConnected:Boolean by Delegates.observable(false) { _, oldValue, newValue ->
|
||||
if (oldValue != newValue) {
|
||||
Log.i(TAG,"${device.name}:链接状态发生变化${newValue}")
|
||||
if(!newValue){
|
||||
OchChainLogManager.writeChainLogBluetooth("连接","连接断开")
|
||||
closeAndDestory()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val TAG = "GattCallback"
|
||||
|
||||
fun closeAndDestory(){
|
||||
fun closeAndDestory(trySend:Boolean){
|
||||
isDestory = true
|
||||
closeConn(false)
|
||||
BleManager.removeFromScandAndWrite(this)
|
||||
BleManager.removeFromScandAndWrite(this,trySend)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,16 +85,21 @@ class OchBluetoothGattCallback(device: BluetoothDevice) : BluetoothGattCallback(
|
||||
gatt.discoverServices() //启动服务发现
|
||||
}else{
|
||||
isConnected = false
|
||||
closeAndDestory(true)
|
||||
Log.i(TAG,"与[${dev.name}]连接断开")
|
||||
}
|
||||
}else{
|
||||
Log.i(TAG,"与[${dev.name}]连接出错,错误码:$status")
|
||||
when (status) {
|
||||
8 -> {
|
||||
closeAndDestory()
|
||||
closeAndDestory(false)
|
||||
}
|
||||
else -> {
|
||||
connect()
|
||||
if(System.currentTimeMillis()-connectStartTime>=connectTimeOUt){
|
||||
closeAndDestory(true)
|
||||
}else {
|
||||
connect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,6 +176,7 @@ class OchBluetoothGattCallback(device: BluetoothDevice) : BluetoothGattCallback(
|
||||
mBluetoothGatt?.disconnect()
|
||||
mBluetoothGatt?.close()
|
||||
mBluetoothGatt = null
|
||||
isConnected = false
|
||||
currentTryCount = 0
|
||||
if(needCopy&&!isDestory) {
|
||||
BleManager.copy2ScanList(this)
|
||||
@@ -201,13 +210,13 @@ class OchBluetoothGattCallback(device: BluetoothDevice) : BluetoothGattCallback(
|
||||
waitSendData.remove(text)
|
||||
if(waitSendData.isEmpty()){
|
||||
OchChainLogManager.writeChainLogBluetooth("写数据","写15s超时")
|
||||
closeAndDestory()
|
||||
closeAndDestory(true)
|
||||
}
|
||||
sendData()
|
||||
}
|
||||
}else{
|
||||
OchChainLogManager.writeChainLogBluetooth("发现服务","server 为null 没有链接服务")
|
||||
closeAndDestory()
|
||||
closeAndDestory(false)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,7 +230,7 @@ class OchBluetoothGattCallback(device: BluetoothDevice) : BluetoothGattCallback(
|
||||
}else {
|
||||
if (isDestory) {
|
||||
OchChainLogManager.writeChainLogBluetooth("连接","已销毁")
|
||||
closeAndDestory()
|
||||
closeAndDestory(false)
|
||||
return
|
||||
}
|
||||
connect()
|
||||
@@ -229,6 +238,10 @@ class OchBluetoothGattCallback(device: BluetoothDevice) : BluetoothGattCallback(
|
||||
}
|
||||
}
|
||||
|
||||
fun getSendData():MutableMap<String,UUID>{
|
||||
return waitSendData
|
||||
}
|
||||
|
||||
/**
|
||||
* 链接成功后发送
|
||||
* 写超时后 写下一条
|
||||
@@ -253,13 +266,14 @@ class OchBluetoothGattCallback(device: BluetoothDevice) : BluetoothGattCallback(
|
||||
if(currentTryCount>maxTryCount||isDestory){
|
||||
// 重新扫描该设备
|
||||
OchChainLogManager.writeChainLogBluetooth("连接","超过次数")
|
||||
closeAndDestory()
|
||||
closeAndDestory(false)
|
||||
return
|
||||
}
|
||||
currentTryCount += 1
|
||||
val temp = currentTryCount
|
||||
closeConn(false)
|
||||
currentTryCount = temp
|
||||
connectStartTime = System.currentTimeMillis()
|
||||
mBluetoothGatt = _device.connectGatt(AbsMogoApplication.getApp(), false, this, TRANSPORT_LE)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user