CC254X IBEACON开发

来源:互联网 发布:linux oracle tns配置 编辑:程序博客网 时间:2024/06/13 22:23

       在进行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作用不大。

IBEACON协议了解后,下面就是对程序进行设计了一下代码是百度上面公开的代码,可以参考设置,广播时间可以自由设定:
准备工作一台 PCIAR 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};修改下面关键字UID: E2C56DB5-DFFB-48D2-B060-D0F5A71096E0Major: 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};接下来修改广播类型,将下面代码//uint8 advType = GAP_ADTYPE_ADV_NONCONN_IND;// use non-connectable advertisementsuint8 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接下来修改 GAP// Set the GAP Role ParametersGAPRole_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 ParametersGAPRole_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 );

1 0
原创粉丝点击