Skip to content

Commit f9a59f5

Browse files
author
xiaoyunfei
committed
修改 discover service 和 read
1 parent 94d2d43 commit f9a59f5

File tree

13 files changed

+190
-113
lines changed

13 files changed

+190
-113
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.shon.ble.call
2+
3+
import android.bluetooth.BluetoothGatt
4+
import com.shon.ble.call.callback.DiscoverCallback
5+
6+
class DiscoverCall(address:String,val gatt: BluetoothGatt):BleCall<DiscoverCallback>(address)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.shon.ble.call.callback
2+
3+
abstract class DiscoverCallback:BleCallback<Boolean>
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
package com.shon.ble.call.callback
22

3-
interface ReadCallback:BleCallback<ByteArray>
3+
interface ReadCallback:BleCallback<ByteArray>{
4+
fun onExecute()
5+
}

bluetooth/src/main/java/com/shon/ble/call/data/CharacteristicData.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@ internal data class MTUDataMessage(
4747
val address: String,
4848
val mtu: Int,
4949
val status: Int = 10000
50+
)
51+
52+
internal data class DiscoverMessage(
53+
val gatt: BluetoothGatt?,
54+
val status: Int,
5055
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.shon.ble.call.executor
2+
3+
import org.greenrobot.eventbus.EventBus
4+
5+
abstract class BaseExecutor<T, M> constructor(val callback: T) {
6+
7+
open fun execute(){
8+
EventBus.getDefault().register(this)
9+
}
10+
11+
open fun onReceiverMessage(message: M) {
12+
13+
EventBus.getDefault().unregister(this)
14+
}
15+
16+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.shon.ble.call.executor
2+
3+
import android.annotation.SuppressLint
4+
import android.bluetooth.BluetoothGatt
5+
import android.text.TextUtils
6+
import com.shon.ble.call.callback.DiscoverCallback
7+
import com.shon.ble.call.data.DiscoverMessage
8+
import org.greenrobot.eventbus.Subscribe
9+
import org.greenrobot.eventbus.ThreadMode
10+
11+
internal class DiscoverExecutor(
12+
private val address: String,
13+
private val gatt: BluetoothGatt,
14+
private val discoverCallback: DiscoverCallback
15+
) :
16+
BaseExecutor<DiscoverCallback, DiscoverMessage>(discoverCallback) {
17+
@SuppressLint("MissingPermission")
18+
override fun execute() {
19+
super.execute()
20+
gatt.discoverServices()
21+
}
22+
23+
@Subscribe(threadMode = ThreadMode.BACKGROUND)
24+
override fun onReceiverMessage(message: DiscoverMessage) {
25+
message.gatt?.let {
26+
val mac = it.device.address
27+
if (TextUtils.equals(address, mac)) {
28+
super.onReceiverMessage(message)
29+
if (message.status == BluetoothGatt.GATT_SUCCESS) {
30+
discoverCallback.onResult(true)
31+
} else {
32+
discoverCallback.onResult(false)
33+
}
34+
35+
}
36+
}
37+
38+
39+
}
40+
}

bluetooth/src/main/java/com/shon/ble/call/executor/ReadExecutor.kt

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,36 @@ import android.annotation.SuppressLint
44
import android.bluetooth.BluetoothGatt
55
import android.bluetooth.BluetoothGattCharacteristic
66
import android.text.TextUtils
7+
import com.shon.ble.call.callback.ReadCallback
78
import com.shon.ble.call.data.ReadDataMessage
8-
import kotlinx.coroutines.CoroutineScope
9-
import kotlinx.coroutines.Dispatchers
10-
import kotlinx.coroutines.launch
119
import org.greenrobot.eventbus.Subscribe
1210
import org.greenrobot.eventbus.ThreadMode
1311
import java.util.*
1412

13+
1514
internal class ReadExecutor(
1615
private val address: String,
17-
private val gatt: BluetoothGatt?,
18-
private val gattCharacteristic: BluetoothGattCharacteristic?,
19-
) : IEventBus<ByteArray, ReadDataMessage>() {
20-
16+
private val gatt: BluetoothGatt,
17+
private val gattCharacteristic: BluetoothGattCharacteristic,
18+
private val readCallback: ReadCallback
19+
) : BaseExecutor<ReadCallback, ReadDataMessage>(readCallback) {
2120

2221
@SuppressLint("MissingPermission")
23-
override suspend fun execute(): ByteArray? {
24-
if (gatt == null || gattCharacteristic == null) {
25-
channel.send(null)
26-
channel.close()
27-
return channel.receive()
28-
}
29-
22+
override fun execute() {
23+
super.execute()
24+
readCallback.onExecute()
3025
gatt.readCharacteristic(gattCharacteristic)
31-
32-
return channel.receive()
3326
}
3427

3528
@Subscribe(threadMode = ThreadMode.BACKGROUND)
36-
override fun receiverMessage(message: ReadDataMessage) {
37-
super.receiverMessage(message)
38-
CoroutineScope(Dispatchers.IO).launch {
39-
onCharacteristicRead(
40-
message.gatt, message.characteristic,
41-
message.status
42-
)
43-
}
44-
}
45-
46-
private suspend fun onCharacteristicRead(
47-
gatt: BluetoothGatt?,
48-
characteristic: BluetoothGattCharacteristic?,
49-
status: Int
50-
) {
29+
override fun onReceiverMessage(message: ReadDataMessage) {
30+
val status = message.status
31+
val gattResult = message.gatt
32+
val characteristic = message.characteristic
5133
if (status != BluetoothGatt.GATT_SUCCESS) {
52-
channel.send(null)
53-
channel.close()
5434
return
5535
}
56-
if (gatt == null || characteristic == null) {
57-
channel.send(null)
58-
channel.close()
36+
if (gattResult == null || characteristic == null) {
5937
return
6038
}
6139
val mac = gatt.device.address
@@ -64,14 +42,16 @@ internal class ReadExecutor(
6442
return
6543
}
6644

67-
val mUuid: String = gattCharacteristic?.uuid.toString().lowercase(Locale.getDefault())
45+
val mUuid: String = gattCharacteristic.uuid.toString().lowercase(Locale.getDefault())
6846
val uuid: String = characteristic.uuid.toString().lowercase(Locale.getDefault())
6947

7048
if (!TextUtils.equals(mUuid, uuid)) {
7149
return
7250
}
51+
super.onReceiverMessage(message)
7352
val value: ByteArray = characteristic.value
74-
channel.send(value)
75-
channel.close()
53+
readCallback.onResult(value)
54+
7655
}
77-
}
56+
57+
}

bluetooth/src/main/java/com/shon/ble/dispatcher/BleDispatcher.kt

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.shon.ble.dispatcher
22

3+
import android.bluetooth.BluetoothGatt
34
import android.bluetooth.BluetoothGattCharacteristic
45
import android.bluetooth.BluetoothManager
56
import com.shon.ble.util.BleLog
67
import com.shon.ble.call.*
8+
import com.shon.ble.call.callback.ReadCallback
79
import com.shon.ble.call.callback.SendCallback
810
import com.shon.ble.call.executor.*
911
import com.shon.ble.getGattCharacteristic
@@ -12,6 +14,8 @@ import kotlinx.coroutines.Dispatchers
1214
import kotlinx.coroutines.async
1315
import kotlinx.coroutines.channels.Channel
1416
import kotlinx.coroutines.launch
17+
import kotlin.coroutines.resume
18+
import kotlin.coroutines.suspendCoroutine
1519

1620
internal class BleDispatcher internal constructor(private val manager: BluetoothManager) {
1721
private val channel = Channel<BleCall<*>>(Channel.Factory.UNLIMITED)
@@ -48,6 +52,7 @@ internal class BleDispatcher internal constructor(private val manager: Bluetooth
4852
val execute = MTUExecutor(address, call.gatt, call.mtu).execute()
4953
call.getCallBack().onResult(execute)
5054
}
55+
is DiscoverCall -> discoverService(call)
5156
is EnableNotifyCall -> enableNotifyService(call)
5257
is ReadDataCall -> readData(call)
5358
is WriteDataCall -> {
@@ -71,12 +76,23 @@ internal class BleDispatcher internal constructor(private val manager: Bluetooth
7176
call.getCallBack().onResult(execute)
7277
}
7378

79+
80+
private suspend fun discoverService(call: DiscoverCall) {
81+
val address = call.address
82+
val gatt = call.gatt
83+
val discoverResult = executeDiscoverCall(address, gatt)
84+
call.getCallBack().onResult(discoverResult)
85+
}
86+
87+
7488
private suspend fun readData(call: ReadDataCall) {
7589
val gattCharacteristic = call.getCharacteristic()
76-
val readExecutor = ReadExecutor(call.address, call.gatt, gattCharacteristic)
77-
val readData = readExecutor.execute()
7890
val callBack = call.getCallBack()
79-
callBack.onResult(readData)
91+
gattCharacteristic ?: return
92+
val executeReadCallResult = executeReadCall(call.address, call.gatt, gattCharacteristic) {
93+
callBack.onExecute()
94+
}
95+
callBack.onResult(executeReadCallResult)
8096
}
8197

8298
private suspend fun writeData(call: WriteDataCall): Boolean? {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.shon.ble.dispatcher
2+
3+
import android.bluetooth.BluetoothGatt
4+
import android.bluetooth.BluetoothGattCharacteristic
5+
import com.shon.ble.call.callback.DiscoverCallback
6+
import com.shon.ble.call.callback.ReadCallback
7+
import com.shon.ble.call.executor.DiscoverExecutor
8+
import com.shon.ble.call.executor.ReadExecutor
9+
import kotlin.coroutines.resume
10+
import kotlin.coroutines.suspendCoroutine
11+
12+
13+
internal suspend fun executeDiscoverCall(address: String, gatt: BluetoothGatt): Boolean {
14+
return suspendCoroutine { coroutine ->
15+
val discoverExecutor = DiscoverExecutor(address, gatt, object : DiscoverCallback() {
16+
override fun onResult(value: Boolean?) {
17+
value?.let {
18+
coroutine.resume(it)
19+
}
20+
}
21+
22+
})
23+
discoverExecutor.execute()
24+
}
25+
}
26+
27+
internal suspend fun executeReadCall(
28+
address: String,
29+
gatt: BluetoothGatt,
30+
gattCharacteristic: BluetoothGattCharacteristic,
31+
onExecute: () -> Unit = {}
32+
): ByteArray {
33+
34+
return suspendCoroutine { coroutine ->
35+
val readExecutor = ReadExecutor(address, gatt, gattCharacteristic, object : ReadCallback {
36+
override fun onResult(value: ByteArray?) {
37+
value?.let {
38+
coroutine.resume(it)
39+
}
40+
}
41+
42+
override fun onExecute() {
43+
onExecute.invoke()
44+
}
45+
})
46+
readExecutor.execute()
47+
}
48+
}

bluetooth/src/main/java/com/shon/ble/ext/GattExt.kt

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)