fix: gatt connect transport bug
This commit is contained in:
parent
55299209fd
commit
9b8b922ea7
|
@ -3,6 +3,7 @@ package icu.fur93.esp32_car.repository
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.bluetooth.BluetoothAdapter
|
import android.bluetooth.BluetoothAdapter
|
||||||
import android.bluetooth.BluetoothDevice
|
import android.bluetooth.BluetoothDevice
|
||||||
|
import android.bluetooth.BluetoothDevice.TRANSPORT_LE
|
||||||
import android.bluetooth.BluetoothGatt
|
import android.bluetooth.BluetoothGatt
|
||||||
import android.bluetooth.BluetoothGattCallback
|
import android.bluetooth.BluetoothGattCallback
|
||||||
import android.bluetooth.BluetoothGattCharacteristic
|
import android.bluetooth.BluetoothGattCharacteristic
|
||||||
|
@ -12,6 +13,7 @@ import android.bluetooth.le.ScanCallback
|
||||||
import android.bluetooth.le.ScanResult
|
import android.bluetooth.le.ScanResult
|
||||||
import android.bluetooth.le.ScanSettings
|
import android.bluetooth.le.ScanSettings
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.widget.Toast
|
||||||
import icu.fur93.esp32_car.entity.BleDevice
|
import icu.fur93.esp32_car.entity.BleDevice
|
||||||
import icu.fur93.esp32_car.viewmodel.CarCommands
|
import icu.fur93.esp32_car.viewmodel.CarCommands
|
||||||
import icu.fur93.esp32_car.viewmodel.CarState
|
import icu.fur93.esp32_car.viewmodel.CarState
|
||||||
|
@ -26,6 +28,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
// 蓝牙通信接口
|
// 蓝牙通信接口
|
||||||
interface BluetoothRepository {
|
interface BluetoothRepository {
|
||||||
|
@ -43,6 +46,10 @@ enum class ConnectionState {
|
||||||
DISCONNECTING
|
DISCONNECTING
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val serviceUUID = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
|
||||||
|
val rxCharUUID = "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
|
||||||
|
val txCharUUID = "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
|
||||||
|
|
||||||
// 蓝牙通信实现
|
// 蓝牙通信实现
|
||||||
class BluetoothRepositoryImpl(
|
class BluetoothRepositoryImpl(
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
|
@ -121,7 +128,6 @@ class BluetoothRepositoryImpl(
|
||||||
@SuppressLint("MissingPermission")
|
@SuppressLint("MissingPermission")
|
||||||
fun connectToDevice(device: BluetoothDevice) {
|
fun connectToDevice(device: BluetoothDevice) {
|
||||||
_connectionState.value = ConnectionState.CONNECTING
|
_connectionState.value = ConnectionState.CONNECTING
|
||||||
|
|
||||||
device.connectGatt(
|
device.connectGatt(
|
||||||
context,
|
context,
|
||||||
false,
|
false,
|
||||||
|
@ -131,6 +137,12 @@ class BluetoothRepositoryImpl(
|
||||||
status: Int,
|
status: Int,
|
||||||
newState: Int
|
newState: Int
|
||||||
) {
|
) {
|
||||||
|
if (status != BluetoothGatt.GATT_SUCCESS) {
|
||||||
|
scope.launch(Dispatchers.Main) {
|
||||||
|
Toast.makeText(context, "连接失败,错误码: $status", Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
when (newState) {
|
when (newState) {
|
||||||
BluetoothProfile.STATE_CONNECTED -> {
|
BluetoothProfile.STATE_CONNECTED -> {
|
||||||
bluetoothGatt = gatt
|
bluetoothGatt = gatt
|
||||||
|
@ -159,7 +171,7 @@ class BluetoothRepositoryImpl(
|
||||||
gatt.services?.forEach { service ->
|
gatt.services?.forEach { service ->
|
||||||
service.characteristics?.forEach { characteristic ->
|
service.characteristics?.forEach { characteristic ->
|
||||||
// 这里需要根据你的设备具体的UUID来匹配
|
// 这里需要根据你的设备具体的UUID来匹配
|
||||||
if (characteristic.uuid.toString() == "YOUR_CHARACTERISTIC_UUID") {
|
if (characteristic.uuid == UUID.fromString(rxCharUUID)) {
|
||||||
rxCharacteristic = characteristic
|
rxCharacteristic = characteristic
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -172,9 +184,12 @@ class BluetoothRepositoryImpl(
|
||||||
characteristic: BluetoothGattCharacteristic,
|
characteristic: BluetoothGattCharacteristic,
|
||||||
value: ByteArray
|
value: ByteArray
|
||||||
) {
|
) {
|
||||||
onReceivePacket(value)
|
if (characteristic.uuid == UUID.fromString(txCharUUID)) {
|
||||||
|
onReceivePacket(value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
TRANSPORT_LE
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,6 +224,8 @@ class BluetoothRepositoryImpl(
|
||||||
if (!isValidPacket(packet)) return
|
if (!isValidPacket(packet)) return
|
||||||
|
|
||||||
when (packet[2].toUByte().toUInt()) {
|
when (packet[2].toUByte().toUInt()) {
|
||||||
|
|
||||||
|
// `01 0C E0 电机A_IN_1_2 电机A_PWM 电机B_IN_1_2 电机B_PWM 电机C_IN_1_2 电机C_PWM 电机D_IN_1_2 电机D_PWM FE`
|
||||||
CarCommands.CMD_STATUS_MOTOR -> updateMotorStatus(packet)
|
CarCommands.CMD_STATUS_MOTOR -> updateMotorStatus(packet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue