本帖最后由 wangyufeng2500 于 2015-12-10 12:38 编辑
因为项目里面需要集成IBeacon功能,所以花时间研究了下,和大家分享一下,并附上实现的代码
什么是IBeacon就不解释了,能做什么也不再这里解释了。
说白了就是利用蓝牙广播的数据包做的**,蓝牙广播格式如下
1 byte preamble(1字节做报头) 4 byte access address(4字节做地址) 39 bytes advertising channel PDU(39个字节用于PDU数据包) 3 bytes CRC(3个字节用于CRC数据校验) 而我们要用的就是其中的39字节的数据包,我们只需要其中30个字节的数据包就可以,我们自定义数据包符合IBeacon协议就可以,那协议是什么,如下: iBeacon 标示:02 01 06 1A FF 4C 00 02 15UUID: B9 40 7F 30 F5 F8 46 6E AF F9 25 55 6B 57 FE 6Dmajor:00 49 minor:00 0A 一米时候的信号值:C5 (算法是256-距离一米是的信号值)其中我们需要设置的就是UUID Major Minor 和信号值 ,这个是手机搜索到之后区分模块ID,距离的必要参数,详情可以百度 其中IBeacon的表示部分不要动就可以了。测试软件我使用的是ios平台的Locate iB测试结果如下:
能够显示到ID和里面Major Minor值,并且可以显示实时的距离还是很可以的,第二个图是距离到达设置之后自动推送信息。
void StartBroadcast(void)
{
/* Define the Broadcast advertisement parameters - including the BD address,
* advertisement type, advertisement interval etc.
*/
CYBLE_GAPP_DISC_PARAM_T myAdvParameters =
{
/* Minimum Advertisement interval = 100 ms */
CYBLE_GAP_ADV_ADVERT_INTERVAL_NONCON_MIN,
/* Maximum Advertisement interval = Minimum = 100 ms */
CYBLE_GAP_ADV_ADVERT_INTERVAL_NONCON_MIN,
/* Advertisement type - Non-connectable undirected (broadcasting) */
CYBLE_GAPP_NON_CONNECTABLE_UNDIRECTED_ADV,
/* Own BD address type - Public */
CYBLE_GAP_ADDR_TYPE_PUBLIC,
/* Address type of peer for directed advertisement - not applicable */
CYBLE_GAP_ADDR_TYPE_PUBLIC,
/* Address for directed advertisement - not applicable */
{0},
/* Channel map - advertise on all 3 channels */
0x07,
/* Advertisement filter policy - not applicable */
0
};
/* Define the advertisement data for the broadcast - this includes the
* advertisement flags and the device name advertised. The device name
* used for Broadcasting is different from the device name used when
* we want to connect to another device.
*/
CYBLE_GAPP_DISC_DATA_T myAdvData =
{
{
0x02,
0x01,
0x04,
0x1A,
0xFF,
0x4C, 0x00, 0x02, 0x15, 0xB9, 0x40, 0x7F, 0x30, 0xF5, 0xF8, 0x46, 0x6E, 0xAF, 0xF9, 0x25, 0x55, 0x6B, 0x57, 0xFE, 0x6D, 0x00, 0x49, 0x00, 0x0A, 0xC5
},
30 /* Total bytes in the advertisement packet */
};
/* There is no scan response data configured for this broadcast */
CYBLE_GAPP_SCAN_RSP_DATA_T myScanRespData =
{
{0}, /* Scan response packet */
0 /* Length of the scan response packet */
};
/* Top level structure for holding all advertisement parameters */
CYBLE_GAPP_DISC_MODE_INFO_T discoveryModeInfo =
{
CYBLE_GAPP_NONE_DISC_BROADCAST_MODE, /* Discoverable mode */
&myAdvParameters, /* Advertisement parameters */
&myAdvData, /* Advertisement data */
&myScanRespData, /* Scan response data */
0 /* Advertisement timeout: none */
};
/* Start broadcasting */
CyBle_GappEnterDiscoveryMode(&discoveryModeInfo);
}
|