【问题标题】:How can I connect an Android app to Raspberry Pi, via Bluetooth, in order to send a text file of numbers?如何通过蓝牙将 Android 应用程序连接到 Raspberry Pi,以发送数字文本文件?
【发布时间】:2016-10-03 19:00:25
【问题描述】:

我正在使用 Android Studio 中的 Android 应用程序和在 NOOBS 上运行的 Raspberry Pi 3。我想让应用程序从文本文件中查询 Pi 中的数字,或者如果需要,只查询整个文本文件。

到目前为止,我已经按照本教程:(http://www.instructables.com/id/Control-Raspberry-Pi-GPIO-Using-an-App/?ALLSTEPS) 制作了一个应用程序,该应用程序操作连接到 Raspberry Pi 的 GPIO 的 LED。我成功了,但这并没有使用蓝牙。我尝试它主要是为了确认我实际上可以在 Android 应用程序和 Raspberry Pi 之间做点什么。

然后我尝试了这个教程:(http://www.instructables.com/id/Android-Bluetooth-Control-LED-Part-2/?ALLSTEPS),它创建了一个通过蓝牙连接的 Android 应用程序,以控制 LED。然而,本教程使用 Arduino,而不是 Raspberry Pi。因此,Arduino 草图无法在 Pi 上运行。我尝试编写自己的 C 程序,模仿 Arduino 草图正在做的事情,但它对我不起作用。我在本教程中的尝试失败了。我将在下面发布我尝试编写的代码,以防有人可以帮助我修复我的 C 程序,并可能再尝试一下。

我编写的用于通过 Android 应用蓝牙连接控制 Raspberry Pi 上的 LED 的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wiringSerial.h>
#include <wiringPi.h>

#define LED 11 // Pin 17

void ledOn();
void ledOff();

char command[4];
char strin[4];
int ledon = 0;
int strtolVal;
int fd;
char sgc[4];
int sgci;

void ledOn() {
    digitalWrite(LED, 1);
}

void ledOff() {
    digitalWrite(LED, 0);
}

int main() {
    fd = serialOpen("/dev/rfcomm1", 115200);
    wiringPiSetup();
    pinMode(11, OUTPUT); // physical #11, GPIO #17

    while(1) {

        if(serialDataAvail(fd) > 0) {
            sgci = serialGetchar(fd);
        }

        sgci = serialGetchar(fd);
        sgc[0] = sgci + '0';

        if(strcmp(sgc, "0") == 0) {
            ledOff();
        } else if (strcmp(sgc, "1") == 0) {
            ledOn();
        }   
    } // end while loop
} // end main()

控制 LED 不是我的最终目标,所以我不太担心我有一个可以工作的 Android 应用程序来控制 LED,而更担心我可以通过蓝牙在 Android 应用程序和 Pi 之间实现一些工作。因此,我的新目标是能够在 Android 应用程序中显示文本,这些文本是通过蓝牙从 Pi 接收的。

我一直在寻找解决这个问题的方法,要么是连接不是通过蓝牙,要么是应用程序连接到了 Pi 以外的其他东西。如果不知何故,那里有什么东西完全符合我的要求,那么我为我糟糕的搜索技巧道歉。

【问题讨论】:

  • 只告诉你它不适合你并不是很丰富。你应该准确地告诉你应该发生什么。而发生了什么或发生了什么。
  • 你为什么要提供这么糟糕的信息?我们对此无能为力。你应该准确地告诉你的代码做什么或不做什么。你需要帮助不是吗?

标签: android linux bluetooth raspberry-pi3


【解决方案1】:

几分钟前我就类似的话题提出了另一个类似的问题: Data transfer via bluetooth between paired Android and Raspberry PI

【讨论】:

  • 感谢您的建议。我查看了代码,发现它对于蓝牙连接建立和通信很有用,但我很好奇你使用该应用程序的设置是什么。您是使用两台安卓设备连接在一起(如 Google 示例中所示),还是使用一台安卓设备连接到计算机(如树莓派)?
  • 我用了两部安卓手机或者安卓手机和HC05模块。唯一的区别是,hc05 有不同的 UUID
  • // HC-05 UUID "00001101-0000-1000-8000-00805F9B34FB" public static final UUID HC_05_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");跨度>
猜你喜欢
  • 2019-06-08
  • 1970-01-01
  • 2014-09-15
  • 2019-05-16
  • 2014-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多