Skip to content

Commit 4b75d04

Browse files
committed
truncate buffer to length of data read
before this change, the entire buffer containing even previous data was sent, making it difficult to extract the incoming data
1 parent 2fca082 commit 4b75d04

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Diff for: lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/service/BluetoothService.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.IOException;
2525
import java.io.InputStream;
2626
import java.io.OutputStream;
27+
import java.util.Arrays;
2728
import java.util.UUID;
2829

2930
import co.lujun.lmbluetoothsdk.base.BaseListener;
@@ -355,9 +356,14 @@ public void run() {
355356
int bytes;
356357
while (true) {
357358
try {
358-
bytes = mmInStream.read(buffer);
359-
if (mBluetoothListener != null){
360-
((BluetoothListener)mBluetoothListener).onReadData(mmSocket.getRemoteDevice(), buffer);
359+
if (mmInStream.available()>0) {
360+
bytes = mmInStream.read(buffer);
361+
if (bytes>0) {
362+
byte[] data = Arrays.copyOf(buffer,bytes);
363+
if (mBluetoothListener != null) {
364+
((BluetoothListener) mBluetoothListener).onReadData(mmSocket.getRemoteDevice(), data);
365+
}
366+
}
361367
}
362368
} catch (IOException e) {
363369
setState(co.lujun.lmbluetoothsdk.base.State.STATE_DISCONNECTED);

0 commit comments

Comments
 (0)