Bluetooth SPP communication bug fixed.

This commit is contained in:
dxl
2020-04-28 12:23:30 +08:00
parent 477c07514b
commit c7df53edef
3 changed files with 32 additions and 6 deletions
@@ -1,7 +1,11 @@
package cn.proxgrind.com;
import android.annotation.SuppressLint;
import android.util.Log;
import androidx.annotation.NonNull;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -33,6 +37,26 @@ public class SppHasBlock extends AbsBluetoothSpp {
@Override
public InputStream getInput() {
// return new InternalIn();
return inputStream;
}
/*class InternalIn extends InputStream {
@Override
public int read() throws IOException {
if (inputStream == null) return -1;
return inputStream.read();
}
@Override
public int read(@NonNull byte[] b, int off, int len) throws IOException {
if (inputStream == null) return -1;
int lenRecv = inputStream.available();
if (lenRecv <= 0) return -1;
Log.d("***", "长度:" + lenRecv);
return inputStream.read(b, off, lenRecv);
//Log.d("****", "接收: " + HexUtil.toHexString(recvMsg, offset, length));
}
}*/
}
@@ -21,7 +21,7 @@ void c_close() {
int c_read(uint8_t *pbtRx, size_t szRx, int timeout) {
if (sp != INVALID_SERIAL_PORT) {
uart_reconfigure_timeouts(218);
uart_reconfigure_timeouts(388);
size_t recvLen = 0;
size_t *pRecvLen = &recvLen;
if (uart_receive(sp, pbtRx, szRx, pRecvLen) == NFC_SUCCESS) {
@@ -35,7 +35,9 @@ int c_read(uint8_t *pbtRx, size_t szRx, int timeout) {
int c_write(const uint8_t *pbtTx, size_t szTx, int timeout) {
if (sp != INVALID_SERIAL_PORT) {
uart_reconfigure_timeouts((uint32_t) timeout);
return uart_send(sp, pbtTx, szTx);
if (uart_send(sp, pbtTx, szTx) == NFC_SUCCESS) {
return szTx;
}
}
sp = INVALID_SERIAL_PORT;
return -1;
@@ -346,7 +346,8 @@ int uart_receive(const serial_port sp, uint8_t *pbtRx, uint32_t pszMaxRxLen, uin
// Stop if the OS has some troubles reading the data
if (res <= 0) {
return NFC_EIO;
} else if (res > 0) {
} else if (res > 0 && pszMaxRxLen > 255) { // 只有USB通信下才有大于255个字节的通信请求
*pszRxLen += res;
/*
* TODO Look me!
*
@@ -357,11 +358,10 @@ int uart_receive(const serial_port sp, uint8_t *pbtRx, uint32_t pszMaxRxLen, uin
* so we need to return the data directly without waiting for enough 265 bytes.
*
* 这个UART实现是从PM3的开源库里拷贝过来的。
* PM3的通信过程跟LIBNFC的实现有非常大的区别(LIBNFC总是要求265个字节左右的数据),
* PM3的通信过程跟LIBNFC的实现有非常大的区别(LIBNFC某些驱动实现总是要求255个字节左右的数据),
* 如果我们一直堵塞等待,将会导致程序非常缓慢,因此我们需要将数据直接返回,
* 而不等待足够的265个字节。
* 而不等待足够的255以上个字节。
* */
*pszRxLen += res;
return NFC_SUCCESS;
}