蓝牙 bluez 的编程 C C++
简介
bluez目录有一个libbluetooth.a文件 有一个目录 lib目录里面存储这网络连接的部分代码 基于库的代码编程. 在linux下如果自带了蓝牙,可以通过gcc **.c -lbluetooth 直接编出一个可执行文件.
参考文献
下面的连接提供了一个测试代码.
另外,关于 ble 使用库的方式一直出现失败的情况,是因为,没有设置ble的相关参数,具体可以参考
中的enable_advertising函数int device_id = hci_get_route(NULL); int device_handle = 0; if ((device_handle = hci_open_dev(device_id)) < 0) { perror("Could not open device"); exit(1); } le_set_advertising_parameters_cp adv_params_cp = { 0 }; adv_params_cp.min_interval = htobs(advertising_interval); adv_params_cp.max_interval = htobs(advertising_interval); adv_params_cp.chan_map = 7; uint8_t status; struct hci_request rq = { 0 }; rq.ogf = OGF_LE_CTL; rq.ocf = OCF_LE_SET_ADVERTISING_PARAMETERS; rq.cparam = &adv_params_cp; rq.clen = LE_SET_ADVERTISING_PARAMETERS_CP_SIZE; rq.rparam = &status; rq.rlen = 1; int ret = hci_send_req(device_handle, &rq, 1000); if (ret < 0) { hci_close_dev(device_handle); fprintf(stderr, "Can't send request %s (%d)\n", strerror(errno), errno); return (1); } le_set_advertise_enable_cp advertise_cp = { 0 }; advertise_cp.enable = 0x01; memset(&rq, 0, sizeof(rq)); rq.ogf = OGF_LE_CTL; rq.ocf = OCF_LE_SET_ADVERTISE_ENABLE; rq.cparam = &advertise_cp; rq.clen = LE_SET_ADVERTISE_ENABLE_CP_SIZE; rq.rparam = &status; rq.rlen = 1; ret = hci_send_req(device_handle, &rq, 1000); if (ret < 0) { hci_close_dev(device_handle); fprintf(stderr, "Can't send request %s (%d)\n", strerror(errno), errno); return (1); }