您好,登錄后才能下訂單哦!
最近搞藍牙,自己找網上的例子嘗試編譯,中間遇到了一些坑,記錄一下:
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <stdlib.h> #include <poll.h> #include <bluetooth/bluetooth.h> #include <bluetooth/hci.h> #include <bluetooth/hci_lib.h> #include <bluetooth/l2cap.h> void * Read_thread(void* pSK); int main(int argc, char** argv) { int iRel = 0; int sk = 0; struct sockaddr_l2 local_addr; struct sockaddr_l2 remote_addr; int len; int nsk = 0; pthread_t nth = 0; struct l2cap_options opts; int optlen = 0; int slen = 0; char str[16] = {0}; if(argc < 2) { printf("\nUsage:%s psm\n", argv[0]); exit(0); } // create l2cap socket sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); //發送數據,使用SOCK_SEQPACKET為好 if(sk < 0) { perror("\nsocket():"); exit(0); } //bind local_addr.l2_family = PF_BLUETOOTH; local_addr.l2_psm = htobs(atoi(argv[argc -1])); //last psm bacpy(&local_addr.l2_bdaddr, BDADDR_ANY); iRel = bind(sk, (struct sockaddr *)&local_addr, sizeof(struct sockaddr)); if(iRel < 0) { perror("\nbind()"); exit(0); } //get opts // in mtu 和 out mtu.每個包的最大值 memset(&opts, 0, sizeof(opts)); optlen = sizeof(opts); getsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &opts, &optlen); printf("\nomtu:[%d]. imtu:[%d]. flush_to:[%d]. mode:[%d]\n", opts.omtu, opts.imtu, opts.flush_to, opts.mode); //set opts. default value opts.omtu = 0; opts.imtu = 672; if (setsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &opts, sizeof(opts)) < 0) { perror("\nsetsockopt():"); exit(0); } //listen iRel = listen(sk, 10); if(iRel < 0) { perror("\nlisten()"); exit(0); } len = sizeof(struct sockaddr_l2); while(1) { memset(&remote_addr, 0, sizeof(struct sockaddr_l2)); nsk = accept(sk, (struct sockaddr*)(&remote_addr), &len); if(nsk < 0) { perror("\naccept():"); continue; } ba2str(&(remote_addr.l2_bdaddr), str); printf("\npeer bdaddr:[%s].\n", str); //得到peer的信息 iRel = pthread_create(&nth, NULL, Read_thread, &nsk); if(iRel != 0) { perror("pthread_create():"); continue; } pthread_detach(nth); // 分離之 } return 0; } void * Read_thread(void* pSK) { //struct pollfd fds[10]; struct pollfd fds[100]; char buf[1024] = {0}; int iRel = 0; int exit_val = 0; //fds[0].fd = *(int*)pSK; //fds[0].events = POLLIN | POLLHUP; fds[0].fd = (int)(*(int*)pSK); fds[0].events = POLLIN | POLLHUP; while(1) { if(poll(fds, 1, -1) < 0) { perror("\npoll():"); } if(fds[0].revents & POLLHUP) { //hang up printf("\n[%d] Hang up\n", *(int*)pSK); close(*(int*)pSK); pthread_exit(&exit_val); break; } if(fds[0].revents & POLLIN) { memset(buf, 0 , 1024); //read data iRel = recv(*(int*)pSK, buf, 572, 0); //printf("\nHandle[%d] Receive [%d] data:[%s]", *(int*)pSK, iRel, buf); } } return 0; }
頭文件:
#include <bluetooth/bluetooth.h> #include <bluetooth/hci.h> #include <bluetooth/hci_lib.h> #include <bluetooth/l2cap.h>
需要從以下網址下載兩個文件:
http://www.bluez.org/download/
其中,頭文件在bluez-5.38.tar.xz文件中有。
鏈接需要庫文件libbluetooth.so.2
這個文件就要通過bluez-libs-3.36.tar.gz編譯生成,執行兩條命令就可以生成:
./configure make
將生成的庫文件拷貝到/usr/lib目錄下就可以了。
gcc編譯的時候要加編譯選項,這樣才能編譯成功:
gcc server.c -o server -I bluez-5.38/lib/ -lbluetooth -lpthread
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。