Skip to content

Commit

Permalink
upgrade printPossibleBleUUID
Browse files Browse the repository at this point in the history
  • Loading branch information
Shanyaliux committed Feb 21, 2022
1 parent fdbf52d commit 7e32ba5
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ internal object SerialPortConnect {
internal var bluetoothGatt: BluetoothGatt? = null

internal var gattCharacteristicList = HashMap<String, Int>()

internal var gattServiceList = HashMap<String, HashMap<String, Int>>()
/**
* bluetoothGattCallback BLE设备连接回调
* @Author Shanya
Expand Down Expand Up @@ -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()
Expand All @@ -142,6 +143,7 @@ internal object SerialPortConnect {
}
}
}
gattServiceList[gattService.uuid.toString()] = gattCharacteristicList
}
}
}
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();


}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ object DataUtil {
}

fun arrayListByte2ByteArray(data: String): ByteArray {
return DataUtil.string2hex(data)?.toList()!!.toByteArray()
return string2hex(data)?.toList()!!.toByteArray()
}
}

0 comments on commit 7e32ba5

Please sign in to comment.