我正在做我的項目在Android和Arduino上,我能夠通過藍牙從Android發送消息到Arduino,但我正在努力通過藍牙從Arduino到Android的消息。請幫助完成project.Thank你提前Android Arduino和藍牙
接收代碼:
私有類ReadInput實現Runnable {
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;
}
}
顯示您的代碼以及您到目前爲止所嘗試的內容。 – Mangesh
私有類ReadInput實現Runnable {0} {0} {0} {0}私有布爾bStop = false; 私人主題t; public ReadInput(){ t = new Thread(this,「Input Thread」); t.start(); } public boolean isRunning(){ return t.isAlive(); } @Override public void run(){ InputStream inputStream; –
try {inputIdStream = mBTSocket.getInputStream(); while(!bStop)byte [] buffer = new byte [256]; (inputStream.available()> 0)inputStream.read(buffer); int i = 0;對於(i = 0; i