CH58x Observer BLE示例讲解
Observer:又称为Scanner,可以监听广播数据或者搜索周围设备,
Observer:观察者角色例程,定时扫描,如果扫描结果不为空,则打印扫描到的广播地址
#define GAPROLE_MAX_SCAN_RES 0x30E //!< Maximum number of discover scan results to receive. Default is 0 = unlimited. 最大扫描个数
// Setup Observer Profile
{
uint8_t scanRes = DEFAULT_MAX_SCAN_RES;
GAPRole_SetParameter(GAPROLE_MAX_SCAN_RES, sizeof(uint8_t), &scanRes);
}
#define TGAP_DISC_SCAN 2 //!< Minimum time to perform scanning,Setting this parameter to 0 turns off the timeout.Default 10.24seconds. (n * 0.625 mSec) 执行扫描的最短时间 // Setup GAP GAP_SetParamValue(TGAP_DISC_SCAN, DEFAULT_SCAN_DURATION);
/** * @brief Start the device in Observer role. This function is typically * called once during system startup. * * @param pAppCallbacks - pointer to application callbacks * * @return SUCCESS: Operation successful.<BR> * bleAlreadyInRequestedMode: Device already started.<BR> */ extern bStatus_t GAPRole_ObserverStartDevice( gapRoleObserverCB_t *pAppCallbacks );
if(events & START_DEVICE_EVT)
{
// Start the Device
GAPRole_ObserverStartDevice((gapRoleObserverCB_t *)&ObserverRoleCB); //启动扫描并指定回调函数
return (events ^ START_DEVICE_EVT);
}
初始化完成,触发扫描开启
static void ObserverEventCB(gapRoleEvent_t *pEvent)
{
switch(pEvent->gap.opcode)
{
case GAP_DEVICE_INIT_DONE_EVENT:
{
PRINT("Begin %d\n",RTC_GetCycle32k());
GAPRole_ObserverStartDiscovery(DEFAULT_DISCOVERY_MODE,
DEFAULT_DISCOVERY_ACTIVE_SCAN,
DEFAULT_DISCOVERY_WHITE_LIST);
PRINT("Discovering...\n");
}
扫描设备可以获取广播包和扫描应答包,如下:
case GAP_DEVICE_INFO_EVENT:
{
ObserverAddDeviceInfo(pEvent->deviceInfo.addr, pEvent->deviceInfo.addrType);
uint8 i;
for(i=0;i<pEvent->deviceInfo.dataLen;i++)
{
PRINT(" %x",pEvent->deviceInfo.pEvtData[i]);
}
PRINT("\n");
}
其中 pEvent->deviceInfo 还有以下设备信息可以获取(不建立连接获取设备信息)
typedef struct
{
tmos_event_hdr_t hdr; //!< GAP_MSG_EVENT and status
uint8_t opcode; //!< GAP_DEVICE_INFO_EVENT
uint8_t eventType; //!< Advertisement Type: @ref GAP_ADVERTISEMENT_REPORT_TYPE_DEFINES
uint8_t addrType; //!< address type: @ref GAP_ADDR_TYPE_DEFINES
uint8_t addr[B_ADDR_LEN]; //!< Address of the advertisement or SCAN_RSP
int8_t rssi; //!< Advertisement or SCAN_RSP RSSI
uint8_t dataLen; //!< Length (in bytes) of the data field (evtData)
uint8_t *pEvtData; //!< Data field of advertisement or SCAN_RSP
} gapDeviceInfoEvent_t;
扫描到达设定的最大个数或者扫描超时后,会进入如下条件
case GAP_DEVICE_DISCOVERY_EVENT:
{
PRINT("Discovery over...\n");
PRINT("over %d\n",RTC_GetCycle32k()); //打印下一次进入该状态的时间,通过32K计数进行换算
// Display discovery results
if(pEvent->discCmpl.numDevs > 0)
{
int i, j;
// Increment index of current result (with wraparound)
for(j = 0; j < pEvent->discCmpl.numDevs; j++)
{
PRINT("Device %d : ", j);
for(i = 0; i < 6; i++)
{
PRINT("%x ", pEvent->discCmpl.pDevList[j].addr[i]);
}
PRINT("\n");
}
}
GAPRole_ObserverStartDiscovery(DEFAULT_DISCOVERY_MODE,
DEFAULT_DISCOVERY_ACTIVE_SCAN,
DEFAULT_DISCOVERY_WHITE_LIST);//启动下一轮扫描
PRINT("Discovering...\n ");
}
看运行结果
07 ff 4c 00 12 02 00 01 Discovery over... over 580245 Device 0 : d7 b1 3c e4 c2 84 Device 1 : 39 73 12 e8 6c 14 Device 2 : 9c bd de 7b 5b da Device 3 : ab 9b af ff dd d Device 4 : 60 76 7b 50 d8 41 Device 5 : ce 9d 87 65 f2 12 Device 6 : 19 4b fd 86 be 4a Device 7 : 2b 79 c6 7a 68 1c Discovering... 1e ff 06 00 01 09 20 02 07 07 1e 1f 39 d6 3e 40 ad 11 db d0 b7 ab 59 34 af 1a 61 20 93 f6 e5 略... 0a ff ff ff e9 21 25 17 c7 d4 ff 13 09 57 58 30 35 31 39 31 31 30 32 32 32 31 30 30 30 31 36 //打印的广播包或者扫描应答包 Discovery over... over 677021 Device 0 : b 87 92 83 4f 21 //打印的mac地址 Device 1 : 8a e2 47 ea 70 e2 Device 2 : c5 e8 8 6b 80 3c Device 3 : c5 e8 8 6b 80 3c Device 4 : 8 c7 45 34 5b 42 Device 5 : 60 76 7b 50 d8 41 Device 6 : 19 4b fd 86 be 4a Device 7 : c2 81 b9 53 1b 6d Discovering...
32K 计数 (677021-580245)/32000 =3.02s
// Scan duration in (625us)
#define DEFAULT_SCAN_DURATION 4800 代码种设定的扫描间隔 4800*0.625=3s 与上述运行时间一致

浙公网安备 33010602011771号