日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

蓝牙程序编译

發布時間:2025/3/18 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 蓝牙程序编译 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近搞藍牙,自己找網上的例子嘗試編譯,中間遇到了一些坑,記錄一下:

#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?socketsk?=?socket(PF_BLUETOOTH,?SOCK_SEQPACKET,?BTPROTO_L2CAP);??//發送數據,使用SOCK_SEQPACKET為好if(sk?<?0){perror("\nsocket():");exit(0);}//bindlocal_addr.l2_family?=?PF_BLUETOOTH;local_addr.l2_psm?=?htobs(atoi(argv[argc?-1]));??//last?psmbacpy(&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?valueopts.omtu?=?0;opts.imtu?=?672;if?(setsockopt(sk,?SOL_L2CAP,?L2CAP_OPTIONS,?&opts,?sizeof(opts))?<?0){perror("\nsetsockopt():");exit(0);}//listeniRel?=?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?upprintf("\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?dataiRel?=?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


轉載于:https://blog.51cto.com/duallay/1760446

總結

以上是生活随笔為你收集整理的蓝牙程序编译的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。