如何通过蓝牙实现Arduino向Android(App Inventor)传输数据?附门禁灯光控制代码
Hey Dan, let's figure out how to get your Arduino sending data to your App Inventor Android app over Bluetooth—perfect for tracking your lock and light statuses. I'll break this down into Arduino code tweaks and App Inventor setup, tailored to your existing project.
1. Arduino端:修改代码以发送状态数据
First, you'll need a Bluetooth module like HC-05 or HC-06 (the most common picks for this kind of project). Let's wire it up first:
- Connect Bluetooth VCC to Arduino 5V (or 3.3V if your module supports it)
- Bluetooth GND to Arduino GND
- Bluetooth RX to Arduino digital pin 2 (we'll use this as Arduino's TX)
- Bluetooth TX to Arduino digital pin 3 (we'll use this as Arduino's RX)
Now, let's update your existing code to include Bluetooth communication and send status updates whenever the lock or light changes:
#include <Servo.h> #include <SoftwareSerial.h> // Add this library for Bluetooth communication // Bluetooth setup: RX on pin 3, TX on pin 2 SoftwareSerial bluetooth(3, 2); Servo myservo; int pos = 0; int led = 13; int led2 = 12; void setup() { myservo.attach(9); // Attach servo to pin 9 (adjust if your wiring is different!) pinMode(led, OUTPUT); pinMode(led2, OUTPUT); digitalWrite(led, HIGH); // Start with lock closed (red LED on) digitalWrite(led2, LOW); bluetooth.begin(9600); // Match your Bluetooth module's baud rate (default 9600 for HC-05) bluetooth.println("LOCK_LOCKED"); // Send initial lock status to the app } void loop() { // Check for incoming commands from Android (your original control logic) if (bluetooth.available() > 0) { char data = bluetooth.read(); if (data == 'U') { // Unlock command from app for (pos = 0; pos <= 90; pos += 1) { myservo.write(pos); delay(15); } digitalWrite(led, LOW); digitalWrite(led2, HIGH); bluetooth.println("LOCK_UNLOCKED"); // Send unlock status to app } if (data == 'L') { // Lock command from app for (pos = 90; pos >= 0; pos -= 1) { myservo.write(pos); delay(15); } digitalWrite(led, HIGH); digitalWrite(led2, LOW); bluetooth.println("LOCK_LOCKED"); // Send lock status to app } // Add light control logic if needed if (data == '1') { digitalWrite(led2, HIGH); bluetooth.println("GREEN_LED_ON"); } if (data == '0') { digitalWrite(led2, LOW); bluetooth.println("GREEN_LED_OFF"); } } }
Key notes here:
- We use
SoftwareSerialto handle Bluetooth since the Uno's hardware serial is reserved for programming. - Every time the lock or LED state changes, we send a clear, human-readable string (like
LOCK_UNLOCKED)—this makes parsing super easy in App Inventor. - Double-check your servo's attached pin (I used pin 9, adjust if yours is connected elsewhere).
2. App Inventor端:设置蓝牙接收
Now let's set up your App Inventor project to receive and display the status data:
Step 1: Add Components
Head to the Designer tab and drag in these components:
- BluetoothClient (under Connectivity) – this is the modern, reliable replacement for the old Bluetooth component.
- Button (name it
btnConnect) – to initiate the Bluetooth connection. - Label (name it
lblStatus) – to show the latest status from Arduino. - ListPicker (optional but helpful) – to let you select paired Bluetooth devices easily.
Step 2: Build the Logic (Blocks Tab)
Here's the core logic to get data from Arduino:
Connect to Bluetooth:
- When
btnConnectis clicked, useBluetoothClient.Connectwith your module's name (default HC-05 name is "HC-05", or whatever you renamed it to). - Alternatively, use the ListPicker to show paired devices and connect to the selected one.
- When
Receive and Display Data:
- Add the
BluetoothClient.ReceivedTextevent—this triggers every time the app gets data from Arduino. - Update
lblStatus.Textwith the received text, or add conditional logic to make the UI match the state (like changing label colors).
- Add the
Example block breakdown:
- When
btnConnect.Click:BluetoothClient1.Connect("HC-05") - When
BluetoothClient1.ReceivedText(text):set lblStatus.Text to text // Optional: Add visual feedback if text = "LOCK_UNLOCKED" then set lblStatus.TextColor to green else if text = "LOCK_LOCKED" then set lblStatus.TextColor to red end if
Important App Inventor Tips
- Pair your Android device with the HC-05 first using the default pin (1234 or 0000) before opening the app.
- App Inventor adds Bluetooth permissions automatically, but double-check in project settings if you run into issues.
- Use
println()in Arduino (notprint())—App Inventor'sReceivedTextevent triggers on newline characters, ensuring each status update is sent as a complete, separate message.
3. Test the Setup
- Upload the modified Arduino code to your board.
- Power up the Arduino and Bluetooth module.
- Open your App Inventor app on a real Android device (emulators don't support Bluetooth).
- Connect to the Bluetooth module via the app.
- Send commands (like 'U' for unlock) and check if the status updates appear in the label. You should see Arduino send back the correct state every time you change something!
内容的提问来源于stack exchange,提问作者Dan

