Ich mache mein Projekt auf Android und Arduino, ich bin in der Lage, die Nachricht von Android zu Arduino über Bluetooth zu senden, aber kämpfe um die Nachricht von Arduino zu Android über Bluetooth zu bekommen project.Thank Sie im VorausAndroid Arduino und Bluetooth
Empfangen Code:
private Klasse ReadInput Runnable implementiert {
private boolean bStop = false;
private Thread t;
public ReadInput() {
t = new Thread(this, "Input Thread");
t.start();
}
public boolean isRunning() {
return t.isAlive();
}
@Override
public void run() {
InputStream inputStream;
try {
inputStream = mBTSocket.getInputStream();
while (!bStop) {
byte[] buffer = new byte[256];
if (inputStream.available() > 0) {
inputStream.read(buffer);
int i = 0;
/*
* This is needed because new String(buffer) is taking the entire buffer i.e. 256 chars on Android 2.3.4 http://stackoverflow.com/a/8843462/1287554
*/
for (i = 0; i < buffer.length && buffer[i] != 0; i++) {
}
final String strInput = new String(buffer, 0, i);
/*
* If checked then receive text, better design would probably be to stop thread if unchecked and free resources, but this is a quick fix
*/
if (chkReceiveText.isChecked()) {
mTxtReceive.post(new Runnable() {
@Override
public void run() {
mTxtReceive.append(strInput);
//Uncomment below for testing
//mTxtReceive.append("\n");
//mTxtReceive.append("Chars: " + strInput.length() + " Lines: " + mTxtReceive.getLineCount() + "\n");
int txtLength = mTxtReceive.getEditableText().length();
if (txtLength > mMaxChars) {
mTxtReceive.getEditableText().delete(0, txtLength - mMaxChars);
}
if (chkScroll.isChecked()) { // Scroll only if this is checked
scrollView.post(new Runnable() { // Snippet from http://stackoverflow.com/a/4612082/1287554
@Override
public void run() {
scrollView.fullScroll(View.FOCUS_DOWN);
}
});
}
}
});
}
}
Thread.sleep(500);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void stop() {
bStop = true;
}
}
Zeigen Sie Ihren Code und was Sie bisher versucht haben. – Mangesh
private Klasse ReadInput implementiert Runnable { private boolean bStop = false; private Thread t; public ReadInput() { t = neuer Thread (this, "Input Thread"); t.start(); } public boolean isRunning() { Rückgabe t.isAlive(); } @Override öffentliche void run() { InputStream inputStream; –
versuchen { inputStream = mBTSocket.getInputStream(); while (! BStop) { byte [] buffer = neues Byte [256]; if (inputStream.available()> 0) { inputStream.read (Puffer); int i = 0; /* –