实例介绍
【实例简介】
蓝牙开发书籍,包含HCI socket、 L2CAP socket等接口介绍
【实例截图】
【目录】
Table of Contents Preface .......................................................................................................................................................vi 1. About this book.............................................................................................................................vi 2. Audience .......................................................................................................................................vi 3. Organization of This Book............................................................................................................vi 4. Acknowledgments.........................................................................................................................vi 4.1. Albert’s acknowledgments ............................................................................................. vii 4.2. Larry’s acknowledgments............................................................................................... vii 1. Introduction............................................................................................................................................1 1.1. Understanding Bluetooth as a software developer......................................................................1 1.2. Bluetooth Programming Concepts..............................................................................................2 1.2.1. Choosing a communication partner................................................................................2 1.2.2. Choosing a transport protocol ........................................................................................4 1.2.3. Port numbers and the Service Discovery Protocol .........................................................6 1.2.4. Communicating using sockets......................................................................................10 1.3. Useful things to know about Bluetooth.....................................................................................12 1.3.1. Communications range.................................................................................................12 1.3.2. Communications Speed................................................................................................13 1.3.3. Radio Frequencies and Channel Hopping ....................................................................13 1.3.4. Bluetooth networks - piconets, scatternets, masters, and slaves...................................14 1.3.5. Bluetooth Profiles RFCs............................................................................................15 2. Bluetooth programming with Python - PyBluez...............................................................................17 2.1. Choosing a communication partner ..........................................................................................17 2.2. Communicating with RFCOMM ..............................................................................................18 2.3. Communicating with L2CAP....................................................................................................20 2.3.1. Maximum Transmission Unit.......................................................................................21 2.3.2. Best-effort transmission................................................................................................22 2.4. Service Discovery Protocol.......................................................................................................22 2.4.1. Dynamically allocating port numbers ..........................................................................24 2.4.2. Advertising a service ....................................................................................................24 2.4.3. Searching for and browsing services............................................................................26 2.5. Advanced usage ........................................................................................................................27 2.5.1. Asynchronous socket programming with select.......................................................27 2.5.2. Asynchronous device discovery ...................................................................................28 2.5.3. The _bluetooth module ............................................................................................29 3. C programming with libbluetooth ................................................................................................32 3.1. Choosing a communication partner ..........................................................................................32 3.1.1. Compiling the example.................................................................................................33 3.1.2. Representing Bluetooth addresses................................................................................33 3.1.3. Choosing a local Bluetooth adapter..............................................................................34 3.1.4. Scanning for nearby devices.........................................................................................34 3.1.5. Determining the user-friendly name of a nearby device ..............................................35 3.1.6. Error handling...............................................................................................................36 3.2. RFCOMM sockets ....................................................................................................................36 3.2.1. Addressing structures ...................................................................................................39 3.2.2. Establishing a connection.............................................................................................39 iii 3.2.3. Using a connected socket .............................................................................................40 3.3. L2CAP sockets..........................................................................................................................41 3.3.1. Byte ordering................................................................................................................43 3.3.2. Maximum Transmission Unit.......................................................................................44 3.4. Service Discovery Protocol.......................................................................................................44 3.4.1. Dynamically assigned port numbers ............................................................................45 3.4.2. SDP data structures.......................................................................................................45 3.4.3. Advertising a service ....................................................................................................47 3.4.4. Searching and browsing for a service...........................................................................51 3.5. Advanced BlueZ programming.................................................................................................56 3.5.1. Asynchronous socket programming with select.......................................................56 3.5.2. HCI sockets ..................................................................................................................56 3.5.3. L2CAP Best-effort transmission ..................................................................................57 3.5.4. SCO audio sockets........................................................................................................60 4. Bluetooth development tools...............................................................................................................61 4.1. hciconfig ...............................................................................................................................61 4.2. hcitool....................................................................................................................................64 4.3. sdptool....................................................................................................................................65 4.4. hcidump....................................................................................................................................67 4.5. l2ping......................................................................................................................................68 4.6. rfcomm......................................................................................................................................69 4.7. uuidgen....................................................................................................................................70 4.8. Obtaining BlueZ and PyBluez ..................................................................................................70 5. Microsoft Windows..............................................................................................................................71 5.1. Programming with the Microsoft Bluetooth API......................................................................71 5.1.1. Header files and linked libraries...................................................................................72 5.1.2. Initializing the Windows Sockets API..........................................................................72 5.1.3. Error checking ..............................................................................................................72 5.1.4. Data structures..............................................................................................................73 5.2. Choosing a remote device .........................................................................................................74 5.2.1. Representing Bluetooth addresses as strings................................................................77 5.3. RFCOMM sockets ....................................................................................................................78 5.4. Service Discovery Protocol.......................................................................................................83 5.4.1. Advertising a service ....................................................................................................83 5.4.2. Searching for services...................................................................................................84 6. Other platforms and programming languages .................................................................................86 6.1. Symbian OS / Nokia Series 60..................................................................................................86 6.1.1. Capabilities and Limitations.........................................................................................87 6.1.2. Choosing a device.........................................................................................................87 6.1.3. Bluetooth sockets..........................................................................................................88 6.2. OS X..........................................................................................................................................90 6.3. Java - JSR 82.............................................................................................................................91 6.3.1. Choosing a device.........................................................................................................92 6.3.2. RFCOMM.....................................................................................................................92 6.3.3. OBEX ...........................................................................................................................92 iv List of Tables 1-1. A comparison of the requirements that would lead us to choose certain protocols. Best-effort streams communication is not shown because it reduces to best-effort datagram communication. ...............5 1-2. Port numbers and their terminology for various protocols...................................................................6 1-3. The three Bluetooth power classes.....................................................................................................12 2-1. select events....................................................................................................................................28 4-1. Inquiry Scan and Page Scan ...............................................................................................................63
【核心代码】
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
int set_flush_timeout(bdaddr_t *ba, int timeout)
{
int err = 0, dd;
struct hci_conn_info_req *cr = 0;
struct hci_request rq = { 0 };
struct {
uint16_t handle;
uint16_t flush_timeout;
} cmd_param;
struct {
uint8_t status;
uint16_t handle;
} cmd_response;
// find the connection handle to the specified bluetooth device
cr = (struct hci_conn_info_req*) malloc(
sizeof(struct hci_conn_info_req)
sizeof(struct hci_conn_info));
bacpy( &cr->bdaddr, ba );
cr->type = ACL_LINK;
dd = hci_open_dev( hci_get_route( &cr->bdaddr ) );
if( dd < 0 ) {
err = dd;
goto cleanup;
}
err = ioctl(dd, HCIGETCONNINFO, (unsigned long) cr );
if( err ) goto cleanup;
// build a command packet to send to the bluetooth microcontroller
cmd_param.handle = cr->conn_info->handle;
cmd_param.flush_timeout = htobs(timeout);
rq.ogf = OGF_HOST_CTL;
rq.ocf = 0x28;
rq.cparam = &cmd_param;
rq.clen = sizeof(cmd_param);
rq.rparam = &cmd_response;
rq.rlen = sizeof(cmd_response);
rq.event = EVT_CMD_COMPLETE;
// send the command and wait for the response
err = hci_send_req( dd, &rq, 0 );
if( err ) goto cleanup;
if( cmd_response.status ) {
err = -1;
errno = bt_error(cmd_response.status);
}
cleanup:
free(cr);
if( dd >= 0) close(dd);
return err;
}
int main(int argc, char **argv)
{
bdaddr_t target;
int timeout;
if( argc < 3 ) {
fprintf(stderr, "usage: set-flush-to <addr> <timeout>\n");
exit(2);
}
str2ba( argv[1], &target );
timeout = atoi( argv[2] );
return set_flush_timeout( &target, timeout );
}
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论