From 7e32ba5845088ea7b9c9b6230d89afcf82d8024a Mon Sep 17 00:00:00 2001 From: Shanyaliux <445502929@qq.com> Date: Mon, 21 Feb 2022 10:55:20 +0800 Subject: [PATCH] upgrade printPossibleBleUUID --- .../java/world/shanya/serialportsample/MainActivity.kt | 6 +++--- .../src/main/java/world/shanya/serialport/SerialPort.kt | 9 ++++++--- .../world/shanya/serialport/connect/SerialPortConnect.kt | 7 ++++--- .../world/shanya/serialport/tools/SerialPortTools.java | 4 +--- .../shanya/serialport/tools/SerialPortToolsByKotlin.kt | 2 +- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/world/shanya/serialportsample/MainActivity.kt b/app/src/main/java/world/shanya/serialportsample/MainActivity.kt index 37eb8d1..fc8bb83 100644 --- a/app/src/main/java/world/shanya/serialportsample/MainActivity.kt +++ b/app/src/main/java/world/shanya/serialportsample/MainActivity.kt @@ -23,7 +23,7 @@ class MainActivity : AppCompatActivity() { val config = SerialPortConfig() config.debug = true - config.autoConnect = false + config.autoConnect = true config.openConnectionTypeDialogFlag = false config.autoReconnect = false config.reconnectAtIntervals = 10000 @@ -71,12 +71,12 @@ class MainActivity : AppCompatActivity() { serialPort.printPossibleBleUUID() } -// serialPort.setSendDataType(SerialPort.SEND_HEX) + serialPort.setSendDataType(SerialPort.SEND_HEX) // serialPort.setReadDataType(SerialPort.READ_HEX) buttonSend.setOnClickListener { - serialPort.sendData("0f ff") + serialPort.sendData("0FFF") } } } \ No newline at end of file diff --git a/serialport/src/main/java/world/shanya/serialport/SerialPort.kt b/serialport/src/main/java/world/shanya/serialport/SerialPort.kt index abdf0ad..d4accdf 100644 --- a/serialport/src/main/java/world/shanya/serialport/SerialPort.kt +++ b/serialport/src/main/java/world/shanya/serialport/SerialPort.kt @@ -629,9 +629,12 @@ class SerialPort private constructor() { LogUtil.log("请先连接BLE设备之后,再执行此函数!") return } - for (gattCharacteristic in SerialPortConnect.gattCharacteristicList) { - LogUtil.log("PossibleBleUUID", gattCharacteristic.key) - LogUtil.log("Properties", gattCharacteristic.value.toString(2)) + for (gattService in SerialPortConnect.gattServiceList) { + LogUtil.log("Service", gattService.key) + for (gattCharacteristic in gattService.value) { + LogUtil.log(" Characteristic", gattCharacteristic.key) + LogUtil.log(" Properties", gattCharacteristic.value.toString(2)) + } } LogUtil.log("Properties 具体含义请查询官网, https://shanyaliux.cn/serialport") } diff --git a/serialport/src/main/java/world/shanya/serialport/connect/SerialPortConnect.kt b/serialport/src/main/java/world/shanya/serialport/connect/SerialPortConnect.kt index ff617e2..e0b6934 100644 --- a/serialport/src/main/java/world/shanya/serialport/connect/SerialPortConnect.kt +++ b/serialport/src/main/java/world/shanya/serialport/connect/SerialPortConnect.kt @@ -78,6 +78,8 @@ internal object SerialPortConnect { internal var bluetoothGatt: BluetoothGatt? = null internal var gattCharacteristicList = HashMap() + + internal var gattServiceList = HashMap>() /** * bluetoothGattCallback BLE设备连接回调 * @Author Shanya @@ -116,7 +118,6 @@ internal object SerialPortConnect { gattCharacteristicList.clear() gatt?.services?.let { for (gattService in it) { - val gattCharacteristics = gattService.characteristics for (gattCharacteristic in gattCharacteristics) { val uuid = gattCharacteristic.uuid.toString() @@ -142,6 +143,7 @@ internal object SerialPortConnect { } } } + gattServiceList[gattService.uuid.toString()] = gattCharacteristicList } } } @@ -187,8 +189,7 @@ internal object SerialPortConnect { super.onCharacteristicChanged(gatt, characteristic) val value = characteristic?.value if (value != null && value.isNotEmpty()) { -// val receivedData = String(value) - var receivedData = if (SerialPort.readDataType == SerialPort.READ_STRING) { + val receivedData = if (SerialPort.readDataType == SerialPort.READ_STRING) { SerialPortTools.bytes2string(value, "GBK") } else { val sb = StringBuilder() diff --git a/serialport/src/main/java/world/shanya/serialport/tools/SerialPortTools.java b/serialport/src/main/java/world/shanya/serialport/tools/SerialPortTools.java index 6123ae7..662a623 100644 --- a/serialport/src/main/java/world/shanya/serialport/tools/SerialPortTools.java +++ b/serialport/src/main/java/world/shanya/serialport/tools/SerialPortTools.java @@ -47,6 +47,7 @@ public static void bleSendData(BluetoothGatt gatt, BluetoothGattCharacteristic g if (gattCharacteristic == null) { throw new RuntimeException("BLE发送UUID不正确,请检查!"); } + LogUtil.INSTANCE.log("BLE设备发送数据", data); byte[] buff = SerialPortTools.string2bytes(data, "GBK"); int len = buff.length; int[] lens = dataSeparate(len); @@ -62,13 +63,10 @@ public static void bleSendData(BluetoothGatt gatt, BluetoothGattCharacteristic g } if (lens[1] != 0) { String str = new String(buff, 20 * lens[0], lens[1]); - LogUtil.INSTANCE.log("data", str); gattCharacteristic.setValue(str); gatt.writeCharacteristic(gattCharacteristic); } }).start(); - - } /** diff --git a/serialport/src/main/java/world/shanya/serialport/tools/SerialPortToolsByKotlin.kt b/serialport/src/main/java/world/shanya/serialport/tools/SerialPortToolsByKotlin.kt index a96f224..213cd47 100644 --- a/serialport/src/main/java/world/shanya/serialport/tools/SerialPortToolsByKotlin.kt +++ b/serialport/src/main/java/world/shanya/serialport/tools/SerialPortToolsByKotlin.kt @@ -252,7 +252,7 @@ object DataUtil { } fun arrayListByte2ByteArray(data: String): ByteArray { - return DataUtil.string2hex(data)?.toList()!!.toByteArray() + return string2hex(data)?.toList()!!.toByteArray() } }