嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元微信扫码支付:2 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
蓝牙开发书籍,包含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 );
}