Skip to content

Commit 3e5966e

Browse files
committed
1、添加writeType;2、优化写数据队列
1 parent 8241588 commit 3e5966e

File tree

14 files changed

+190
-86
lines changed

14 files changed

+190
-86
lines changed

.idea/sonarlint/issuestore/0/2/0254c09bbc78bfe46588e0739059e691f7f0a967

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ android {
1111
applicationId "com.bhm.ble"
1212
minSdk 24
1313
targetSdk 34
14-
versionCode 221
15-
versionName "2.2.1"
14+
versionCode 230
15+
versionName "2.3.0"
1616

1717
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1818
}

ble/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66

77
//ext.GROUP = "com.lute.ble"
88
//ext.POM_ARTIFACT_ID = "BleCore"
9-
//ext.VERSION_NAME = "2.2.1"
9+
//ext.VERSION_NAME = "2.3.0"
1010
////引用gradle_upload.gradle
1111
//apply from: "${project.rootDir}/maven_upload.gradle"
1212

@@ -19,7 +19,7 @@ afterEvaluate {
1919
// 这里头是artifacts的配置信息,不填会采用默认的
2020
groupId = 'com.github.buhuiming'
2121
artifactId = 'BleCore'
22-
version = '2.2.1'
22+
version = '2.3.0'
2323

2424
from components.release
2525
artifact androidSourcesJar //打包源码,去除这行打的包将看不到源码
@@ -62,5 +62,5 @@ android {
6262

6363
dependencies {
6464
// implementation "org.jetbrains.kotlin:kotlin-stdlib:1.8.10"
65-
implementation 'androidx.lifecycle:lifecycle-common:2.6.2'
65+
implementation 'androidx.lifecycle:lifecycle-common:2.8.3'
6666
}

ble/src/main/java/com/bhm/ble/BleManager.kt

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,6 @@ class BleManager private constructor() {
361361
/**
362362
* notify
363363
*/
364-
@Synchronized
365364
fun notify(bleDevice: BleDevice,
366365
serviceUUID: String,
367366
notifyUUID: String,
@@ -406,7 +405,6 @@ class BleManager private constructor() {
406405
/**
407406
* stop notify
408407
*/
409-
@Synchronized
410408
fun stopNotify(
411409
bleDevice: BleDevice,
412410
serviceUUID: String,
@@ -451,7 +449,6 @@ class BleManager private constructor() {
451449
/**
452450
* indicate
453451
*/
454-
@Synchronized
455452
fun indicate(bleDevice: BleDevice,
456453
serviceUUID: String,
457454
indicateUUID: String,
@@ -496,7 +493,6 @@ class BleManager private constructor() {
496493
/**
497494
* stop indicate
498495
*/
499-
@Synchronized
500496
fun stopIndicate(
501497
bleDevice: BleDevice,
502498
serviceUUID: String,
@@ -515,7 +511,6 @@ class BleManager private constructor() {
515511
/**
516512
* 读取信号值
517513
*/
518-
@Synchronized
519514
fun readRssi(bleDevice: BleDevice, bleRssiCallback: BleRssiCallback.() -> Unit) {
520515
checkInitialize()
521516
bleBaseRequest?.readRssi(bleDevice, bleRssiCallback)
@@ -531,7 +526,6 @@ class BleManager private constructor() {
531526
/**
532527
* 设置mtu
533528
*/
534-
@Synchronized
535529
fun setMtu(bleDevice: BleDevice, mtu: Int, bleMtuChangedCallback: BleMtuChangedCallback.() -> Unit) {
536530
checkInitialize()
537531
if (mtu > 512) {
@@ -553,7 +547,6 @@ class BleManager private constructor() {
553547
* [BluetoothGatt.CONNECTION_PRIORITY_LOW_POWER] (低功耗)
554548
*
555549
*/
556-
@Synchronized
557550
fun setConnectionPriority(bleDevice: BleDevice, connectionPriority: Int): Boolean {
558551
checkInitialize()
559552
if (connectionPriority != BluetoothGatt.CONNECTION_PRIORITY_BALANCED &&
@@ -567,7 +560,6 @@ class BleManager private constructor() {
567560
/**
568561
* 读特征值数据
569562
*/
570-
@Synchronized
571563
fun readData(bleDevice: BleDevice,
572564
serviceUUID: String,
573565
readUUID: String,
@@ -584,6 +576,7 @@ class BleManager private constructor() {
584576
serviceUUID: String,
585577
writeUUID: String,
586578
data: ByteArray,
579+
writeType: Int? = null,
587580
bleWriteCallback: BleWriteCallback.() -> Unit) {
588581
writeData(
589582
bleDevice,
@@ -592,6 +585,7 @@ class BleManager private constructor() {
592585
SparseArray<ByteArray>(1).apply {
593586
put(0, data)
594587
},
588+
writeType,
595589
bleWriteCallback
596590
)
597591
}
@@ -600,14 +594,14 @@ class BleManager private constructor() {
600594
* 写数据
601595
* 注意:因为分包后每一个包,可能是包含完整的协议,所以分包由业务层处理,组件只会根据包的长度和mtu值对比后是否拦截
602596
*/
603-
@Synchronized
604597
fun writeData(bleDevice: BleDevice,
605598
serviceUUID: String,
606599
writeUUID: String,
607600
dataArray: SparseArray<ByteArray>,
601+
writeType: Int? = null,
608602
bleWriteCallback: BleWriteCallback.() -> Unit) {
609603
checkInitialize()
610-
bleBaseRequest?.writeData(bleDevice, serviceUUID, writeUUID, dataArray, bleWriteCallback)
604+
bleBaseRequest?.writeData(bleDevice, serviceUUID, writeUUID, dataArray, writeType, bleWriteCallback)
611605
}
612606

613607
/**
@@ -625,6 +619,8 @@ class BleManager private constructor() {
625619
data: ByteArray,
626620
skipErrorPacketData: Boolean = false,
627621
retryWriteCount: Int = 0,
622+
retryDelayTime: Long = 0L,
623+
writeType: Int? = null,
628624
bleWriteCallback: BleWriteCallback.() -> Unit) {
629625
writeQueueData(
630626
bleDevice,
@@ -635,6 +631,8 @@ class BleManager private constructor() {
635631
},
636632
skipErrorPacketData,
637633
retryWriteCount,
634+
retryDelayTime,
635+
writeType,
638636
bleWriteCallback
639637
)
640638
}
@@ -648,13 +646,14 @@ class BleManager private constructor() {
648646
* @param skipErrorPacketData 是否跳过数据长度为0的数据包
649647
* @param retryWriteCount 写失败后重试的次数
650648
*/
651-
@Synchronized
652649
fun writeQueueData(bleDevice: BleDevice,
653650
serviceUUID: String,
654651
writeUUID: String,
655652
dataArray: SparseArray<ByteArray>,
656653
skipErrorPacketData: Boolean = false,
657654
retryWriteCount: Int = 0,
655+
retryDelayTime: Long = 0L,
656+
writeType: Int? = null,
658657
bleWriteCallback: BleWriteCallback.() -> Unit) {
659658
checkInitialize()
660659
bleBaseRequest?.writeQueueData(
@@ -664,14 +663,15 @@ class BleManager private constructor() {
664663
dataArray,
665664
skipErrorPacketData,
666665
retryWriteCount,
666+
retryDelayTime,
667+
writeType,
667668
bleWriteCallback
668669
)
669670
}
670671

671672
/**
672673
* 获取所有已连接设备集合(不包含其他应用连接的设备、系统连接的设备)
673674
*/
674-
@Synchronized
675675
fun getAllConnectedDevice(): MutableList<BleDevice>? {
676676
checkInitialize()
677677
return bleBaseRequest?.getAllConnectedDevice()
@@ -681,7 +681,6 @@ class BleManager private constructor() {
681681
* 获取系统已连接设备集合,确保已获取到权限
682682
*/
683683
@SuppressLint("MissingPermission")
684-
@Synchronized
685684
fun getSystemAllConnectedDevice(): MutableList<BluetoothDevice>? {
686685
checkInitialize()
687686
if (!BleUtil.isPermission(application)) {
@@ -695,7 +694,6 @@ class BleManager private constructor() {
695694
* 这个回调会独立存在,与[connect]的bleConnectCallback、[notify]的bleNotifyCallback、
696695
* [indicate]的bleIndicateCallback、[setMtu]的bleMtuChangedCallback不冲突
697696
*/
698-
@Synchronized
699697
fun addBleEventCallback(bleDevice: BleDevice, bleEventCallback: BleEventCallback.() -> Unit) {
700698
checkInitialize()
701699
bleBaseRequest?.addBleEventCallback(bleDevice, bleEventCallback)
@@ -704,7 +702,6 @@ class BleManager private constructor() {
704702
/**
705703
* 移除该设备的连接回调
706704
*/
707-
@Synchronized
708705
fun removeBleConnectCallback(bleDevice: BleDevice) {
709706
checkInitialize()
710707
bleBaseRequest?.removeBleConnectCallback(bleDevice)
@@ -713,7 +710,6 @@ class BleManager private constructor() {
713710
/**
714711
* 替换该设备的连接回调
715712
*/
716-
@Synchronized
717713
fun replaceBleConnectCallback(bleDevice: BleDevice, bleConnectCallback: BleConnectCallback.() -> Unit) {
718714
checkInitialize()
719715
bleBaseRequest?.replaceBleConnectCallback(bleDevice, bleConnectCallback)
@@ -729,7 +725,6 @@ class BleManager private constructor() {
729725
/**
730726
* 移除该设备的Indicate回调
731727
*/
732-
@Synchronized
733728
fun removeBleIndicateCallback(bleDevice: BleDevice, indicateUUID: String) {
734729
checkInitialize()
735730
bleBaseRequest?.removeBleIndicateCallback(bleDevice, indicateUUID)
@@ -738,7 +733,6 @@ class BleManager private constructor() {
738733
/**
739734
* 移除该设备的Notify回调
740735
*/
741-
@Synchronized
742736
fun removeBleNotifyCallback(bleDevice: BleDevice, notifyUUID: String) {
743737
checkInitialize()
744738
bleBaseRequest?.removeBleNotifyCallback(bleDevice, notifyUUID)
@@ -747,7 +741,6 @@ class BleManager private constructor() {
747741
/**
748742
* 移除该设备的Read回调
749743
*/
750-
@Synchronized
751744
fun removeBleReadCallback(bleDevice: BleDevice, readUUID: String) {
752745
checkInitialize()
753746
bleBaseRequest?.removeBleReadCallback(bleDevice, readUUID)
@@ -756,7 +749,6 @@ class BleManager private constructor() {
756749
/**
757750
* 移除该设备的MtuChanged回调
758751
*/
759-
@Synchronized
760752
fun removeBleMtuChangedCallback(bleDevice: BleDevice) {
761753
checkInitialize()
762754
bleBaseRequest?.removeBleMtuChangedCallback(bleDevice)
@@ -765,7 +757,6 @@ class BleManager private constructor() {
765757
/**
766758
* 移除该设备的Rssi回调
767759
*/
768-
@Synchronized
769760
fun removeBleRssiCallback(bleDevice: BleDevice) {
770761
checkInitialize()
771762
bleBaseRequest?.removeBleRssiCallback(bleDevice)
@@ -775,7 +766,6 @@ class BleManager private constructor() {
775766
* 移除该设备的Write回调
776767
* bleWriteCallback为空,则会移除writeUUID下的所有callback
777768
*/
778-
@Synchronized
779769
fun removeBleWriteCallback(bleDevice: BleDevice,
780770
writeUUID: String,
781771
bleWriteCallback: BleWriteCallback? = null
@@ -796,7 +786,6 @@ class BleManager private constructor() {
796786
/**
797787
* 移除该设备回调,BleConnectCallback除外
798788
*/
799-
@Synchronized
800789
fun removeAllCharacterCallback(bleDevice: BleDevice) {
801790
checkInitialize()
802791
bleBaseRequest?.removeAllCharacterCallback(bleDevice)

ble/src/main/java/com/bhm/ble/control/BleTaskQueue.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ import com.bhm.ble.data.TimeoutCancelException
1515
import com.bhm.ble.log.BleLogger
1616
import kotlinx.coroutines.CancellationException
1717
import kotlinx.coroutines.CoroutineScope
18+
import kotlinx.coroutines.DelicateCoroutinesApi
1819
import kotlinx.coroutines.Dispatchers
20+
import kotlinx.coroutines.ExperimentalCoroutinesApi
1921
import kotlinx.coroutines.SupervisorJob
2022
import kotlinx.coroutines.TimeoutCancellationException
2123
import kotlinx.coroutines.cancel
2224
import kotlinx.coroutines.delay
2325
import kotlinx.coroutines.launch
26+
import kotlinx.coroutines.newSingleThreadContext
2427
import kotlinx.coroutines.withTimeout
28+
import java.util.concurrent.ExecutorService
2529

2630

2731
/**
@@ -34,14 +38,21 @@ internal class BleTaskQueue(private val tag: String = "") {
3438

3539
private var mCoroutineScope: CoroutineScope? = null
3640

41+
/*
42+
* 只有一个线程的线程池,不使用CoroutineScope(Dispatchers.IO),是因为不同设备的请求可能
43+
* 使用相同的线程,从而导致一个设备的请求被阻塞,影响其他设备的请求。
44+
*/
45+
@OptIn(ExperimentalCoroutinesApi::class, DelicateCoroutinesApi::class)
46+
private val threadContext = newSingleThreadContext(tag)
47+
3748
private val taskList = BleTaskList()
3849

3950
init {
4051
initLoop()
4152
}
4253

4354
private fun initLoop() {
44-
mCoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
55+
mCoroutineScope = CoroutineScope(threadContext)
4556
}
4657

4758
/**
@@ -225,5 +236,6 @@ internal class BleTaskQueue(private val tag: String = "") {
225236
taskList.clear()
226237
mCoroutineScope?.cancel()
227238
mCoroutineScope = null
239+
(threadContext.executor as ExecutorService).shutdown()
228240
}
229241
}

ble/src/main/java/com/bhm/ble/data/BleWriteData.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ internal data class BleWriteData(
3434
var isWriting: AtomicBoolean = AtomicBoolean(false),
3535
var isWriteFail: AtomicBoolean = AtomicBoolean(false),
3636
var bleWriteCallback: BleWriteCallback,
37+
var writeType: Int? = null
3738
) {
3839
override fun equals(other: Any?): Boolean {
3940
if (this === other) return true
@@ -49,6 +50,7 @@ internal data class BleWriteData(
4950
if (!data.contentEquals(other.data)) return false
5051
if (isWriting != other.isWriting) return false
5152
if (bleWriteCallback != other.bleWriteCallback) return false
53+
if (writeType != other.writeType) return false
5254

5355
return true
5456
}
@@ -61,7 +63,11 @@ internal data class BleWriteData(
6163
result = 31 * result + totalPackage
6264
result = 31 * result + data.contentHashCode()
6365
result = 31 * result + isWriting.hashCode()
66+
result = 31 * result + isWriteFail.hashCode()
6467
result = 31 * result + bleWriteCallback.hashCode()
68+
result = 31 * result + (writeType ?: 0)
6569
return result
6670
}
71+
72+
6773
}

0 commit comments

Comments
 (0)