You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android无法读取Arduino通过蓝牙发送的传感器整数值求助

Hey there! Let's work through this Bluetooth sensor data issue together—no response after connecting is super frustrating, but we can break it down step by step.

First, Let's Validate Your Arduino Setup

Before diving into Android code, let's make sure your Arduino is actually sending data correctly:

  • Add a newline to your sensor output: If you're using Serial.print(sensorValue); instead of Serial.println(sensorValue);, your Android app might be stuck waiting for a delimiter to know when a full value ends. The println() adds a newline character, which makes it way easier for Android to parse each reading.
  • Double-check Bluetooth module wiring: Most modules (like HC-05/06) require cross-wiring: Arduino TX → Bluetooth RX, Arduino RX → Bluetooth TX. If you have these reversed, no data will get through. Also confirm VCC is set to the right voltage (5V for HC-05, 3.3V for HC-06) and GND is connected properly.
  • Verify data in Serial Monitor: Open the Arduino IDE's Serial Monitor, set the baud rate to match your Serial.begin(X) value, and make sure you see sensor values popping up. If nothing shows here, fix your sensor reading code first (e.g., analogRead(A0) for analog sensors) before worrying about Bluetooth.

Now Troubleshoot Your Android App

If Arduino is sending data correctly, let's tackle the Android side—these are the most common pitfalls:

1. Never Read Bluetooth Data on the Main Thread

Android blocks network/Bluetooth operations on the main thread to avoid freezing the app. If you're trying to read the input stream directly in onCreate() or a button click handler, your app will either crash silently or get stuck. Instead, spin up a background thread:

// After successfully connecting to the Bluetooth socket
new Thread(() -> {
    try {
        BufferedReader reader = new BufferedReader(
            new InputStreamReader(bluetoothSocket.getInputStream())
        );
        String sensorLine;
        while ((sensorLine = reader.readLine()) != null) {
            // Update TextView on the main thread—UI changes can't happen in background threads
            runOnUiThread(() -> yourTextView.setText("Sensor Value: " + sensorLine));
        }
    } catch (IOException e) {
        e.printStackTrace();
        // Optional: Show a toast to the user that the connection dropped
        runOnUiThread(() -> Toast.makeText(this, "Bluetooth connection lost", Toast.LENGTH_SHORT).show());
    }
}).start();

2. Check Bluetooth Permissions (Critical for Newer Android Versions)

Make sure your AndroidManifest.xml has all required permissions, and you're requesting them at runtime:

<!-- Base permissions for all versions -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<!-- Android 12+ requires these additional permissions -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

For Android 6.0+, you need to request dangerous permissions (like BLUETOOTH_CONNECT on 12+) when the app starts, not just declare them in the manifest. If you skip this, the app might connect to the Bluetooth module but be blocked from reading data.

3. Confirm You're Using the Correct UUID

Most Bluetooth serial modules (HC-05/06) use the standard SPP (Serial Port Profile) UUID: 00001101-0000-1000-8000-00805F9B34FB. If you're using a custom UUID in your createRfcommSocketToServiceRecord() call, switch to this one—using the wrong UUID can lead to a successful connection but no data transfer.

4. Validate the Bluetooth Socket Connection

Before trying to read data, add a log or check to confirm the socket is actually connected:

if (bluetoothSocket.isConnected()) {
    Log.d("Bluetooth", "Socket connected successfully");
} else {
    Log.e("Bluetooth", "Socket failed to connect");
    // Handle connection failure here
}

If the socket isn't connected, your read code will never execute—so fix the connection logic first.

Quick Sanity Check

Grab a free Bluetooth serial terminal app from the Play Store (like Serial Bluetooth Terminal) and connect to your module. If you see sensor values there, your Arduino setup is good, and the problem is definitely in your Android code. If you don't see values, go back to fixing the Arduino/Bluetooth module connection.

内容的提问来源于stack exchange,提问作者chilipepper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:22:32