Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9086932
  • 博文数量: 1732
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 19830
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1732)

文章存档

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: 其他平台

2015-09-07 16:41:58

http://blog.csdn.net/kangear/article/details/37961769

这个工具据说是基于BlueZ,但是Android4.2以后不再采用BlueZ取而代之的是BlueDroid,具体详见《Android 4.2蓝牙介绍-Android中的Bluetooth》。但是一般解决方案中都是两手准备,在正常使用的时候是BlueDroid,而在调试的硬件时候还可以方便的使用基于BlueZhcittoolBlueDroid不利于调试硬件,这是我在公元20140719时的观点。


# hcitool                                                      

hcitool - HCI Tool ver 4.93

Usage:

        hcitool [options] <command> [command parameters]

Options:

        --help  Display help

        -i dev  HCI device

Commands:

        dev     Display local devices

        inq     Inquire remote devices

        scan    Scan for remote devices

        name    Get name from remote device 

        info    Get information from remote device

        spinq   Start periodic inquiry

        epinq   Exit periodic inquiry

        cmd     Submit arbitrary HCI commands

        con     Display active connections

        cc      Create connection to remote device

        dc      Disconnect from remote device

        sr      Switch master/slave role

        cpt     Change connection packet type

        rssi    Display connection RSSI

        lq      Display link quality

        tpl     Display transmit power level

        afh     Display AFH channel map

        lp      Set/display link policy settings

        lst     Set/display link supervision timeout

        auth    Request authentication

        enc     Set connection encryption

        key     Change connection link key

        clkoff  Read clock offset

        clock   Read local or remote clock

        lescan  Start LE scan

        lewladd Add device to LE White List

        lewlrm  Remove device from LE White List

        lewlsz  Read size of LE White List

        lewlclr Clear LE White list

        lecc    Create a LE Connection

        ledc    Disconnect a LE Connection

        lecup   LE Connection Update

 

For more information on the usage of each command use:

        hcitool <command> --help



功能:获取本设备接口和MAC地址

功能:获取远程设备mac地址clock offset以及class.

功能:扫描远程设备

功能:获取指定MAC地址远程设备的名字

功能:获取远程设备的详情

功能:???

功能:???

功能:向蓝牙设备输入命令

功能:查看当前连接信息

功能:???

功能:???

功能:???

功能:查看远程设备的信号增益(强度)

功能:查看支持的POLICY

功能:远程设备的xxx等级

功能:xxxx

用C写蓝牙通讯程序:扫描、socket,读取、发送
http://blog.chinaunix.net/uid-23686726-id-3238621.html

在Linux下,通过bluez 蓝牙库可以用C语言轻松实现蓝牙通信。在ubuntu下可以用 apt-get install libbluetooth-dev 安装该库。下面是几个简单示例。

一个简单的扫描程序,得到周边的蓝牙从机设备名和地址:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/socket.h>
  5. #include <bluetooth/bluetooth.h>
  6. #include <bluetooth/hci.h>
  7. #include <bluetooth/hci_lib.h>

  8. int main(int argc, char **argv)
  9. {
  10.     inquiry_info *ii = NULL;
  11.     int max_rsp, num_rsp;
  12.     int dev_id, sock, len, flags;
  13.     int i;
  14.     char addr[19] = { 0 };
  15.     char name[248] = { 0 };

  16.     dev_id = hci_get_route(NULL);
  17.     sock = hci_open_dev( dev_id );
  18.     if (dev_id < 0 || sock < 0) {
  19.         perror("opening socket");
  20.         exit(1);
  21.     }

  22.     len = 8;
  23.     max_rsp = 255;
  24.     flags = IREQ_CACHE_FLUSH;
  25.     ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info));
  26.     
  27.     num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
  28.     if( num_rsp < 0 ) perror("hci_inquiry");

  29.     for (= 0; i < num_rsp; i++) {
  30.         ba2str(&(ii+i)->bdaddr, addr);
  31.         memset(name, 0, sizeof(name));
  32.         if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name), 
  33.             name, 0) < 0)
  34.         strcpy(name, "[unknown]");
  35.         printf("%s %s\n", addr, name);
  36.     }

  37.     free( ii );
  38.     close( sock );
  39.     return 0;
  40. }

读和写示例:

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/socket.h>
  4. #include <bluetooth/bluetooth.h>
  5. #include <bluetooth/rfcomm.h>

  6. int main(int argc, char **argv)
  7. {
  8.     struct sockaddr_rc addr = { 0 };
  9.     int s, status, len=0;
  10.     char dest[18] = "00:12:01:31:01:13";
  11.     char buf[256];
  12.     // allocate a socket
  13.     s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

  14.     // set the connection parameters (who to connect to)
  15.     addr.rc_family = AF_BLUETOOTH;
  16.     addr.rc_channel = (uint8_t) 1;
  17.     str2ba( dest, &addr.rc_bdaddr );

  18.     // connect to server
  19.     status = connect(s, (struct sockaddr *)&addr, sizeof(addr));

  20.     
  21.     if(status){
  22.         printf(" failed to connect the device!\n");
  23.         return -1;
  24.     }

  25.     
  26.     do{
  27.         len = read(s, buf, sizeof buf);
  28.     
  29.      if( len>) {
  30.          buf[len]=0;
  31.          printf("%s\n",buf);
  32.          write(s, buf, strlen(buf));
  33.      }
  34.     }while(len>0);

  35.     close(s);
  36.     return 0;
  37. }



阅读(4194) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~