Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions GodotBluetooth/android/src/GodotBluetooth.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.util.Log;
import android.widget.Toast;
import android.content.Intent;
// import android.os.SystemClock;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -86,9 +87,10 @@ public void handleMessage(Message msg) {
if(msg.what == MESSAGE_READ){
String newData = (String) msg.obj;
receivedData.append(newData);
// Log.i(TAG, receivedData.toString());

int endElement = receivedData.indexOf("}");
if(endElement > 0) {
if(endElement >= 0) {
String completeData = receivedData.substring(0, endElement);
int dataSize = completeData.length();

Expand Down Expand Up @@ -340,19 +342,30 @@ public ConnectedThread(BluetoothSocket newSocket) {

public void run() {

byte[] buffer = new byte[1024];
int bytes;

while (true) {
try {
bytes = mmInStream.read(buffer);
String externalData = new String(buffer, 0, bytes);
localHandler.obtainMessage(MESSAGE_READ, bytes, -1, externalData).sendToTarget();
}

catch (IOException e) {
break;
}
// byte[] buffer = new byte[1024];
int bytes; // bytes returned from read()
int availableBytes = 0;

while (true) {
try {
availableBytes = mmInStream.available();
if(availableBytes > 0){
byte[] buffer = new byte[availableBytes]; // buffer store for the stream
// Read from the InputStream
//Log.d(TAG, "read");
bytes = mmInStream.read(buffer);

if( bytes > 0 ){
// Send the obtained bytes to the UI activity
String externalData = new String(buffer, 0, bytes);
localHandler.obtainMessage(MESSAGE_READ, bytes, -1, externalData).sendToTarget();
}
}
} catch (IOException e) {
Log.e("Error reading", e.getMessage());
e.printStackTrace();
break;
}
}
}

Expand Down