Linux与Arduino Nano ESP32的BLE串口通信实现问题求助
问题:Linux与Arduino Nano ESP32的BLE串口通信实现
背景
- 此前通过Arduino Uno搭配HC-05 SPP模块,借助RFCOMM套接字实现了Linux笔记本与Arduino的蓝牙串口通信,参考过《An Introduction to Bluetooth Programming》文档。
- 近期切换到仅支持BLE的Arduino Nano ESP32(基于ESP32-S3),无法实现BLE串口通信。
- 已查阅《ESP32 Basics: Bluetooth Classic》(但该开发板不支持经典蓝牙)、法语教程《Utilisation du Bluetooth Low Energy avec bluez》及Bluez官方文档,未找到有效解决方法。
当前使用代码
Arduino端代码
/** * Bluetooth LE Serial Simple Example * * Outputs the text "Hello!" to the Bluetooth LE Serial port every second. * * Avinab Malla * 24 July 2022 **/ #include <BleSerial.h> BleSerial ble; void setup() { //Start the BLE Serial //Enter the Bluetooth name here ble.begin("BleSerialTest"); } void loop() { //The usage is similar to Serial ble.println("Hello!"); delay(1000); }
Linux端代码
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <bluetooth/bluetooth.h> #include <bluetooth/hci.h> #include <bluetooth/hci_lib.h> #include <sys/socket.h> int main() { struct sockaddr_hci addr; int sock; char *bta = "34:85:18:7B:3C:05"; if ((sock = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0) { perror("socket() failure"); return errno; } memset(&addr, 0, sizeof(addr)); addr.hci_family = AF_BLUETOOTH; addr.hci_dev = hci_devid("34:85:18:7B:3C:05"); addr.hci_channel = HCI_CHANNEL_CONTROL; int status = bind(sock, (struct sockaddr*)&addr, sizeof(addr)); if (status < 0) { perror("connect() failure"); return errno; } else { printf("bind() success\n"); } char msg[65536] = {0}; ssize_t n; while (1) { if ((n = recv(sock, msg, sizeof(msg), 0)) < 0) { perror("recv() failure"); } else { printf("Received %ld bytes\n", n); } } close(sock); return EXIT_SUCCESS; }
运行现状
- Linux端代码执行后输出
bind() success,随后陷入停滞,无法接收Arduino发送的数据。 - 最初尝试用
connect替换bind时,报错connect() failure: Operation not supported。
需求
寻求可行的Linux与Arduino Nano ESP32之间的BLE串口通信实现方案。
内容的提问来源于stack exchange,提问作者1615
相关产品推荐
相关产品推荐

