[英]Android Arduino and Bluetooth
我正在Android和Arduino上做我的項目,我能夠通過藍牙將消息從Android發送到Arduino,但正在努力通過藍牙從Arduino將消息發送到Android。請幫助完成項目。在此先感謝您
接收代碼:
私有類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;
}
}
嘗試將arduino的rx放入藍牙模塊的tx,將藍牙模塊的tx放入arduino的rx。 您正在使用哪個arduino,並且正在使用什么藍牙模塊? 是HC05H嗎?
如果可能,還顯示您的代碼。
只使用Serial.print("");
您可以在Arduino和Android之間發送字符串。 有關更多信息, 請參考此鏈接 。
我已經使用藍牙和Arduino進行了類似的項目,並且我有一個具有完整工作代碼的github: https : //github.com/Primaelq/Mapping-Robot/blob/master/Companion%20App/Eye-BotCompanionApp/app/ src / main / java / studio / eye / a / eye_botcompanionapp / BluetoothService.java這是BluetoothService類,您應該看看連接的線程方法。 隨意使用任何代碼或提出任何問題。
希望對您有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.