android - 如何通过蓝牙将 Android 应用程序连接到 Raspberry Pi,以便发送数字文本文件?

标签 android linux bluetooth raspberry-pi3

我正在 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 以外的东西。如果不知何故,那里确实有我正在寻找的东西,那么我为我糟糕的搜索技巧道歉。

最佳答案

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

关于android - 如何通过蓝牙将 Android 应用程序连接到 Raspberry Pi,以便发送数字文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39838606/

相关文章:

c# - 如何在 C# 中加入蓝牙?

java - Android Studio java.lang.IllegalStateException

android - Centos 视频优化器

python - 安装 pygame 时出现 easy_install 错误

php - 在Linux上通过PHP运行外部程序

android - 带有 Inputstream.read 的 Android 2.1 蓝牙 SPP 错误

Android 蓝牙 SCO 与日产汽车 radio

android - ImageView动画帮助

java - 自定义 setMessage 对话框 TextView 无法转换为 java.lang.CharSequence

linux - 基于文件系统中位置的 Shell 提示符