接下來會使用MIT的App Inventor 2開發藍牙(Bluetooth)Android app,傳送資料到Arduino Mega2560上,
使用的藍牙模組是HC-06,目前App Inventor 2並不支援BLE,但要享受無線控制的樂趣也並不是一定要使用BLE,
重點是動手作的樂趣,使用App Inventor 2,Arduino,以及HC-06,如果你想要自己組一台遙控車,相信也不是一件難事。
關於雙A(Android與arduino)的藍牙模組溝通,網路上也有不少文章,都可以參考。
由於之前介紹過App Inventor 2如何使用,這裡就僅貼上Designer與Blocks的內容:
寫完之後封裝成apk,然後download到Android手機上
然後是Arduino的Code,upload到Arduino Mega2560上。
//H06 anduino code
#include <SoftwareSerial.h>
#include <Wire.h>
SoftwareSerial H06BT(10,11); //Rx,Tx
void setup() {
Serial.begin(9600);
H06BT.begin(9600); // 指定H06的baud rate
}
void loop() {
byte cmmd[20];
int insize;
while(1) {
if ((insize=(H06BT.available()))>0)
{
Serial.print("input size = ");
Serial.println(insize);
for (int i=0; i<insize;i++) {
Serial.print(cmmd[i]=char(H06BT.read())); // 讀取H06收到的資料
Serial.println("");
}
}
}
}
測試結果
(1)手機app輸入
(2)Arduino上COM port顯示