打印
[资料分享]

CC254X IBEACON开发(转载)

[复制链接]
518|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
coshi|  楼主 | 2019-6-21 12:09 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
在进行IBEACON开发过程走了不少的弯路,总结起来其实很简单,它就是一个广播模式,不停的发送数据发送间隔在200ms到10s之间都可以,不过发送越频繁,功耗自然就越大。

      我们先来解析一下IBEACON协议,建议参考[qiank10]的博文http://m.blog.csdn.net/blog/qiank10/38050717,其实简单说,该协议就是简单的广播数据格式,接收端按照统一的格式对数据进行解析。

这就是解析出来的数据, 广播版对于Advertising类型的数据包而言Access Address均为0x8E89BED6。所以这部分可以直接跳过,比较重要的是后面的PDU,PDU的结构是前面两个字节是Hander,剩下的字节就是Payload(其实这些蓝牙4.0标准协议包里面都有解释)。对于IBEACON而言,Hander是固定不变的,是ADV_IND,不过微信摇一摇是不支持该格式的。这点我没找到原因。TxAdd指示了IBeacon Mac Address的类型,1表示Random Address,0表示Public Address。RxAdd在IBEACON协议中没什么作用,就不解释了哈。对于IBEACON协议,最重要的是Playload,它由两部分组成,一部分是IBeacon的Mac地址;另一部分即IBeacon的数据域AdvData,在IBEACON中,重要的是AdvData,AdvData是由多个AD Structure组成的,IBeacon的AdvData也不例外,它包含了两个AD Structure。第一个为02 01 1A,第二个为1A FF 4C 00 02 15 ... 00 02 C5。对于第一个AD Structure,其第一个字节02表示了其剩余部分的长度,第二个字节01表示了该Structure的类型为Flag,而1A就是相应的flags的值了。对于第二个AD Structure,其第一个字节1A表示了其剩余部分的长度,第二个字节FF表示了该Structure的类型为Manufacturer Specific Data,即由制造商规定的数据,BLE协议规定该类型的Structure的开始两个byte为制造商标识,这里是4C 00,即苹果公司标识。到这里,BLE协议的约束范围才真正结束,第二个Structure中剩余的部分则是由IBeacon协议定义的数据。PDU之后是CRC,CRC根据PDU的内容计算得到,该部分的意义在于校验,对IBEACON作用不大。


使用特权

评论回复

相关帖子

沙发
coshi|  楼主 | 2019-6-21 12:09 | 只看该作者
IBEACON协议了解后,下面就是对程序进行设计了一下代码是百度上面公开的代码,可以参考设置,广播时间可以自由设定:
准备工作
一台 PC
IAR Embedded Workbench 集成开发环境,可以用30天试用版本。
支持 蓝牙 4.0 的智能手机一部,并安装下列应用之一
Android Google Play Store.
iPhone  App Store.
CC2540 开发板
CCDebugger 下载器
创建 iBeacon 工程文档
安装 TI 官方的 CC254x 开发环境
复制 C:\Texas Instruments\BLE-CC254x-1.3.2\Projects\ble\SimpleBLEBroadcaster 文件夹
粘贴到:C:\Texas Instruments\BLE-CC254x-1.3.2\Projects\ble\iBeacon
运行 IAR Embedded Workbench,点击 File > Open > Workspace
修改源代码
simpleBLEBroadcaster.c
// GAP - Advertisement data (max size = 31 bytes, though this is
// best kept short to conserve power while advertisting)
static uint8 advertData[] =
{
  // Flags; this sets the device to use limited discoverable
  // mode (advertises for 30 seconds at a time) instead of general
  // discoverable mode (advertises indefinitely)
  0x02,   // length of this data
  GAP_ADTYPE_FLAGS,
  GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,

  // three-byte broadcast of the data "1 2 3"
  0x04,   // length of this data including the data type byte
  GAP_ADTYPE_MANUFACTURER_SPECIFIC,      // manufacturer specific advertisement data type
  1,
  2,
  3
};


使用特权

评论回复
板凳
coshi|  楼主 | 2019-6-21 12:09 | 只看该作者
修改下面关键字
UID: E2C56DB5-DFFB-48D2-B060-D0F5A71096E0
Major: 1 (0x0001)
Minor: 1 (0x0001)
Measured Power: -59 (0xc5)
// GAP - Advertisement data (max size = 31 bytes, though this is
// best kept short to conserve power while advertisting)
static uint8 advertData[] =
{
  // 25 byte ibeacon advertising data
  // Preamble: 0x4c000215
  // UUID: E2C56DB5-DFFB-48D2-B060-D0F5A71096E0
  // Major: 1 (0x0001)
  // Minor: 1 (0x0001)
  // Measured Power: -59 (0xc5)
  0x1A, // length of this data including the data type byte
  GAP_ADTYPE_MANUFACTURER_SPECIFIC, // manufacturer specific advertisement data type
  0x4c,
  0x00,
  0x02,
  0x15,
  0xe2,
  0xc5,
  0x6d,
  0xb5,
  0xdf,
  0xfb,
  0x48,
  0xd2,
  0xb0,
  0x60,
  0xd0,
  0xf5,
  0xa7,
  0x10,
  0x96,
  0xe0,
  0x00,
  0x01,
  0x00,
  0x01,
  0xc5
};


使用特权

评论回复
地板
coshi|  楼主 | 2019-6-21 12:10 | 只看该作者
接下来修改广播类型,将下面代码
//uint8 advType = GAP_ADTYPE_ADV_NONCONN_IND;// use non-connectable advertisements
uint8 advType = GAP_ADTYPE_ADV_DISCOVER_IND; // use scannable unidirected advertisements

修改为
uint8 advType = GAP_ADTYPE_ADV_NONCONN_IND;   // use non-connectable advertisements
//uint8 advType = GAP_ADTYPE_ADV_DISCOVER_IND; // use scannable unidirected advertisements


使用特权

评论回复
5
coshi|  楼主 | 2019-6-21 12:10 | 只看该作者
接下来修改 GAP
// Set the GAP Role Parameters
GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable );
GAPRole_SetParameter( GAPROLE_ADVERT_OFF_TIME, sizeof( uint16 ), &gapRole_AdvertOffTime );

GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, sizeof ( scanRspData ), scanRspData );
GAPRole_SetParameter( GAPROLE_ADVERT_DATA, sizeof( advertData ), advertData );
GAPRole_SetParameter( GAPROLE_ADV_EVENT_TYPE, sizeof( uint8 ), &advType );

因为 iBeacon 必须不间断广播,并且不响应任何数据请求,所以我们要修改 GAPROLE_ADVERT_OFF_TIME 和 GAPROLE_SCAN_RSP_DATA。
// Set the GAP Role Parameters
GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable );
//GAPRole_SetParameter( GAPROLE_ADVERT_OFF_TIME, sizeof( uint16 ), &gapRole_AdvertOffTime );

//GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, sizeof ( scanRspData ), scanRspData );
GAPRole_SetParameter( GAPROLE_ADVERT_DATA, sizeof( advertData ), advertData );
GAPRole_SetParameter( GAPROLE_ADV_EVENT_TYPE, sizeof( uint8 ), &advType );


使用特权

评论回复
6
dirtwillfly| | 2019-6-25 22:40 | 只看该作者
感谢分享

使用特权

评论回复
7
susceptibility| | 2019-6-28 13:45 | 只看该作者
感谢分享!学习下

使用特权

评论回复
8
coshi|  楼主 | 2019-7-25 14:26 | 只看该作者
非常感谢支持

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

95

主题

3301

帖子

4

粉丝