Kotlin未从HC-06蓝牙模块接收完整数据包
在Kotlin,我遇到了一个问题,数据没有在一个数据包中完全加载到Android的缓冲区中。我正在从HC-06蓝牙模块发送数据包。 当我收到它时,它以两个包的形式出现。micro正在发送CR和LF,并在传输结束时发送Kotlin未从HC-06蓝牙模块接收完整数据包,kotlin,Kotlin,在Kotlin,我遇到了一个问题,数据没有在一个数据包中完全加载到Android的缓冲区中。我正在从HC-06蓝牙模块发送数据包。 当我收到它时,它以两个包的形式出现。micro正在发送CR和LF,并在传输结束时发送 I/UI_Msg: < I/UI_Msg: 7:16> I/UI_Msg: < I/UI_Msg: 7:16> etc 把它弄出来。一次读取一个字节的缓冲区,一次生成字符串。检查字符是否有效传输 private fun beginListe
I/UI_Msg: <
I/UI_Msg: 7:16>
I/UI_Msg: <
I/UI_Msg: 7:16>
etc
把它弄出来。一次读取一个字节的缓冲区,一次生成字符串。检查<和>字符是否有效传输
private fun beginListenForData() {
stopThread = false
val thread = Thread(Runnable {
while (!Thread.currentThread().isInterrupted && !stopThread) {
try {
val byteCount = inputStream!!.available()
if (byteCount > 0) {
val rawBytes = ByteArray(1)
inputStream!!.read(rawBytes,0,1)
val string = String(rawBytes, StandardCharsets.UTF_8)
if (string== "<") {
m_Headder=true
}
if (m_Headder==true){
stringBuilder = stringBuilder.plus(string)
if (string ==">") {
m_Received=stringBuilder
Log.i(TAG, "string:" + stringBuilder)
stringBuilder = ""
m_Headder = false
ProcessMessage(m_Received)
}
}
}
} catch (ex: IOException) {
stopThread = true
}
}
})
thread.start()
}
private fun beginListenForData(){
stopThread=false
val thread=线程(可运行{
而(!Thread.currentThread().isInterrupted&!stopThread){
试一试{
val byteCount=inputStream!!.available()
如果(字节数>0){
val rawBytes=ByteArray(1)
inputStream!!.read(原始字节,0,1)
val string=string(rawBytes,StandardCharsets.UTF_8)
如果(字符串==“”){
m_Received=stringBuilder
Log.i(标签,“字符串:”+stringBuilder)
stringBuilder=“”
m_header=错误
ProcessMessage(已接收m_)
}
}
}
}捕获(例如:IOException){
stopThread=true
}
}
})
thread.start()
}
private fun beginListenForData() {
stopThread = false
val thread = Thread(Runnable {
while (!Thread.currentThread().isInterrupted && !stopThread) {
try {
val byteCount = inputStream!!.available()
if (byteCount > 0) {
val rawBytes = ByteArray(1)
inputStream!!.read(rawBytes,0,1)
val string = String(rawBytes, StandardCharsets.UTF_8)
if (string== "<") {
m_Headder=true
}
if (m_Headder==true){
stringBuilder = stringBuilder.plus(string)
if (string ==">") {
m_Received=stringBuilder
Log.i(TAG, "string:" + stringBuilder)
stringBuilder = ""
m_Headder = false
ProcessMessage(m_Received)
}
}
}
} catch (ex: IOException) {
stopThread = true
}
}
})
thread.start()
}