t507嵌入式linux经典蓝牙通讯demo
//
/* 开启蓝牙 echo 1 > /sys/class/rfkill/rfkill0/state rtk_hciattach -n -s 115200 /dev/ttySAC1 rtk_h5 & hciconfig -a hciconfig hci0 up 打开 hciconfig hci0 piscan 使自身可以被发现 # hciconfig hci0 sspmode 1 设置蓝牙适配器以启用简单配对/ sdptool add SP 配置好以后,需要手动添加SPP服务 /usr/libexec/bluetooth/bluetoothd -C -n & 这个能读取配置文件中的名称 /var/lib/bluetooth/54:F2:9F:B8:44:BD/settings 正确的启动顺序 echo 1 > /sys/class/rfkill/rfkill0/state rtk_hciattach -n -s 115200 /dev/ttySAC1 rtk_h5 & 等待3秒 hciconfig hci0 up /usr/libexec/bluetooth/bluetoothd -C -n & sdptool add SP hciconfig hci0 piscan hciconfig -a hcitool scan 编译此处是 经典蓝牙的demo 与普通服务器的搭建基本一致 aarch64-linux-gnu-gcc -I/opt/EmbedSky/TQT507/CoreA/longan/out/t507/evb/longan/buildroot/host/aarch64-buildroot-linux-gnu/sysroot/usr/include -g bt_data_demo.c -o 22_bt_data_demo -lpthread && cp ./22_bt_data_demo /home/book/nfs_rootfs */ #include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/time.h> #include <time.h> #define mydebug printf("[%s %s] %s: %s: %d\n", __DATE__, __TIME__, __FILE__, __func__, __LINE__); #define mydebugMsg(msg) printf("[%s %s] %s: %s: %d msg:%s\n", __DATE__, __TIME__, __FILE__, __func__, __LINE__, msg); #define mydebugNum(msg) printf("[%s %s] %s: %s: %d num:%d\n", __DATE__, __TIME__, __FILE__, __func__, __LINE__, msg); #define mydebugHex(buf, len) \ do \ { \ unsigned char *__buf_ptr = (unsigned char *)(buf); \ printf("[%s %s] %s: %s: %d %s: ", __DATE__, __TIME__, __FILE__, __func__, __LINE__, #buf); \ for (int i = 0; i < (len); i++) \ { \ printf("%02X ", __buf_ptr[i]); \ } \ printf("\n"); \ } while (0); #define mydebugUs \ do \ { \ struct timeval tv_start; \ gettimeofday(&tv_start, NULL); \ uint64_t start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec); \ printf("[%ld] %s: %s: %d\n", start_ms, __FILE__, __func__, __LINE__); \ } while (0); // static uint64_t start_ms_mydebugCalusStart__inner_used=0; #define getustimestampCalusCurrent() ({ struct timeval tv_start; gettimeofday(&tv_start, NULL); (tv_start.tv_sec * 1000000 + tv_start.tv_usec); }) #define mydebugCalusStart static uint64_t start_ms_mydebugCalusStart__inner_used = getustimestampCalusCurrent(); #define mydebugCalusUpdate(msg) \ { \ start_ms_mydebugCalusStart__inner_used = getustimestampCalusCurrent() - start_ms_mydebugCalusStart__inner_used; \ printf("%s: %s: %d \t us:[%ld] %s:%s\n", __FILE__, __func__, __LINE__, start_ms_mydebugCalusStart__inner_used, #msg, msg); \ } #define mydebugCalusEnd(msg) \ { \ uint64_t start_ms_mydebugCalusEnd = getustimestampCalusCurrent() - start_ms_mydebugCalusStart__inner_used; \ printf("%s: %s: %d \t us:[%ld] %s:%s\n", __FILE__, __func__, __LINE__, start_ms_mydebugCalusEnd, #msg, msg); \ } #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <bluetooth/bluetooth.h> #include <bluetooth/rfcomm.h> #include <pthread.h> #define BT_CHANNEL 1 #define MAX_DATA_SIZE 1024 int client; pthread_mutex_t fastmutexcp = PTHREAD_MUTEX_INITIALIZER; // 互斥锁 int bt_send_info(int socket, void *buf, int len) { mydebugMsg(buf); int status = 0; pthread_mutex_lock(&fastmutexcp); // 互斥锁上锁 status = write(socket, buf, len); if (status == -1) { perror("write"); } pthread_mutex_unlock(&fastmutexcp); return status; } static void *timer_send_data(void *arg) { int _client = (int)arg; int count=0; char buf[64]; mydebug; while (_client == client) { count+=1; snprintf(buf,64,"timer_timer_timer_timer_timer_timer:%d",count); int status = bt_send_info(_client, buf, strlen(buf)); sleep(3); } } void receive_data(int socket) { char buffer[MAX_DATA_SIZE] = {0}; int bytes_read; mydebug; while (1) { // 从RFCOMM socket接收数据 bytes_read = read(socket, buffer, sizeof(buffer)); if (bytes_read > 0) { printf("Received data: %s\n", buffer); // 处理接收到的数据 char buf[] = "recv ok!"; int status = bt_send_info(socket, buf, strlen(buf)); if (status == -1) { break; } } else if (bytes_read == 0) { // 远程设备关闭了连接 printf("Remote device disconnected.\n"); break; } else { // 发生错误 perror("read"); break; } } } int main() { struct sockaddr_rc loc_addr = {0}, rem_addr = {0}; char buf[MAX_DATA_SIZE] = {0}; int s; mydebug; // 创建RFCOMM socket s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); if (s == -1) { perror("socket"); exit(1); } // 设置本地设备的地址 loc_addr.rc_family = AF_BLUETOOTH; loc_addr.rc_bdaddr = *BDADDR_ANY; loc_addr.rc_channel = BT_CHANNEL; // 将RFCOMM socket绑定到本地设备 if (bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)) == -1) { perror("bind"); close(s); exit(1); } // 监听RFCOMM socket上的连接请求 if (listen(s, 1) < 0) { perror("listen"); close(s); exit(1); } while (1) { // 等待连接请求并接受连接 mydebug; socklen_t opt = sizeof(rem_addr); client = accept(s, (struct sockaddr *)&rem_addr, &opt); if (client == -1) { perror("accept"); close(s); exit(1); } // 定时发送 pthread_t pthid_handle; pthread_create(&pthid_handle, NULL, timer_send_data, (void *)client); // 连接已建立,接收数据 receive_data(client); close(client); client=-1; } // 关闭连接和socket close(s); client = -1; return 0; }
//
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构