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

能否将树莓派蓝牙设备连接的Python代码转换为C语言?

树莓派蓝牙GATT连接:Python转C实现方案

可行性

完全可行。你提供的Python代码依赖pygatt(底层调用BlueZ的gatttool),而树莓派默认使用的蓝牙栈BlueZ本身提供了原生C语言API,可直接实现GATT客户端的连接、特征订阅、通知接收等功能。

C语言实现方案

核心是使用BlueZ的GATT Client API,编译时需要链接BlueZ的相关库。以下是对应你Python逻辑的完整C实现:

代码示例

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/gatt.h>

// 处理蓝牙通知的回调函数(对应Python中的handle_data)
void notification_callback(uint16_t handle, const uint8_t* value, size_t len, void* user_data) {
    printf("Received data: ");
    for (size_t i = 0; i < len; i++) {
        printf("%02X", value[i]);
    }
    printf("\n");
}

int main() {
    int hci_dev_id, sock;
    bdaddr_t target_mac;
    uint16_t conn_handle;
    gatt_char_inst* target_char = NULL;

    // 转换目标设备MAC地址(替换为你的设备地址)
    str2ba("AC:23:3F:AA:36:7C", &target_mac);

    // 打开HCI蓝牙设备
    hci_dev_id = hci_get_route(NULL);
    sock = hci_open_dev(hci_dev_id);
    if (sock < 0) {
        perror("Failed to open HCI device");
        return EXIT_FAILURE;
    }

    // 建立GATT连接
    conn_handle = gatt_connect(sock, &target_mac, BDADDR_LE_PUBLIC, 0, 0, 0);
    if (conn_handle == 0) {
        perror("Failed to connect to Bluetooth device");
        close(sock);
        return EXIT_FAILURE;
    }
    printf("Connected to device (connection handle: %d)\n", conn_handle);

    // 查找目标GATT特征(替换为你的特征UUID)
    uuid_t char_uuid;
    str2uuid("7f280002-8204-f393-e0a9-e50e24dcca9e", &char_uuid);
    target_char = gatt_find_char(sock, conn_handle, NULL, &char_uuid, NULL);
    if (!target_char) {
        perror("Failed to find target characteristic");
        gatt_disconnect(sock, conn_handle);
        close(sock);
        return EXIT_FAILURE;
    }

    // 注册通知回调,订阅特征更新
    if (gatt_register_notification(sock, conn_handle, target_char->value_handle, notification_callback, NULL) < 0) {
        perror("Failed to register notification");
        gatt_disconnect(sock, conn_handle);
        close(sock);
        free(target_char);
        return EXIT_FAILURE;
    }
    printf("Subscribed to characteristic notifications\n");

    // 保持进程运行,等待通知(对应Python中的无限睡眠循环)
    while (1) {
        sleep(10);
    }

    // 清理资源(实际会被上面的循环阻塞,仅作流程完整性展示)
    gatt_unregister_notification(sock, conn_handle, target_char->value_handle);
    free(target_char);
    gatt_disconnect(sock, conn_handle);
    close(sock);
    return EXIT_SUCCESS;
}

编译与运行

  1. 先安装BlueZ开发库:
sudo apt install libbluetooth-dev
  1. 编译代码:
gcc -o ble_gatt_client ble_gatt_client.c `pkg-config --cflags --libs bluez`
  1. 确保蓝牙服务正常运行:
sudo systemctl start bluetooth
  1. 运行程序(需root权限):
sudo ./ble_gatt_client

与Python代码的对应关系

  • pygatt.GATTToolBackend.start() → C中hci_open_dev打开HCI设备
  • adapter.connect() → gatt_connect建立GATT连接
  • device.subscribe(..., callback=handle_data) → gatt_register_notification注册回调函数
  • 主循环睡眠 → C中while(1) sleep(10)保持进程存活

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 22:45:38