我正在使用 BLE Zero 项目、并尝试在睡眠模式下增加广播间隔。 但当我尝试将时间间隔增加到7.5秒以上时、连接始终会失败。 我将使用" GapAdv_setParam"更改间隔值。 我还需要修改什么内容吗、或者我缺少什么内容吗? 如果我不熟悉 BLE、请提供帮助。
static void ProjectZero_processGapMessage(gapEventHdr_t *pMsg)
{
switch(pMsg->opcode)
{
case GAP_DEVICE_INIT_DONE_EVENT:
{
bStatus_t status = FAILURE;
gapDeviceInitDoneEvent_t *pPkt = (gapDeviceInitDoneEvent_t *)pMsg;
if(pPkt->hdr.status == SUCCESS)
{
// Store the system ID
uint8_t systemId[DEVINFO_SYSTEM_ID_LEN];
// use 6 bytes of device address for 8 bytes of system ID value
systemId[0] = pPkt->devAddr[0];
systemId[1] = pPkt->devAddr[1];
systemId[2] = pPkt->devAddr[2];
// set middle bytes to zero
systemId[4] = 0x00;
systemId[3] = 0x00;
// shift three bytes up
systemId[7] = pPkt->devAddr[5];
systemId[6] = pPkt->devAddr[4];
systemId[5] = pPkt->devAddr[3];
// Set Device Info Service Parameter
DevInfo_SetParameter(DEVINFO_SYSTEM_ID, DEVINFO_SYSTEM_ID_LEN,
systemId);
// Display device address
// Need static so string persists until printed in idle thread.
static uint8_t addrStr[3 * B_ADDR_LEN + 1];
util_arrtohex(pPkt->devAddr, B_ADDR_LEN, addrStr, sizeof addrStr,
UTIL_ARRTOHEX_REVERSE);
Log_info1("GAP is started. Our address: " \
ANSI_COLOR(FG_GREEN) "%s" ANSI_COLOR(ATTR_RESET),
(uintptr_t)addrStr);
// Setup and start Advertising
// For more information, see the GAP section in the User's Guide:
// software-dl.ti.com/.../
// Temporary memory for advertising parameters for set #1. These will be copied
// by the GapAdv module
GapAdv_params_t advParamLegacy = GAPADV_PARAMS_LEGACY_SCANN_CONN;
// Create Advertisement set #1 and assign handle
status = GapAdv_create(&ProjectZero_advCallback, &advParamLegacy,
&advHandleLegacy);
APP_ASSERT(status == SUCCESS);
Log_info1("Name in advertData array: " \
ANSI_COLOR(FG_YELLOW) "%s" ANSI_COLOR(ATTR_RESET),
(uintptr_t)util_getLocalNameStr(advertData,
sizeof(advertData)));
// Load advertising data for set #1 that is statically allocated by the app
status = GapAdv_loadByHandle(advHandleLegacy, GAP_ADV_DATA_TYPE_ADV,
sizeof(advertData), advertData);
APP_ASSERT(status == SUCCESS);
// Load scan response data for set #1 that is statically allocated by the app
status =
GapAdv_loadByHandle(advHandleLegacy, GAP_ADV_DATA_TYPE_SCAN_RSP,
sizeof(scanRspData),
scanRspData);
APP_ASSERT(status == SUCCESS);
// Set event mask for set #1
status = GapAdv_setEventMask(advHandleLegacy,
GAP_ADV_EVT_MASK_START_AFTER_ENABLE |
GAP_ADV_EVT_MASK_END_AFTER_DISABLE |
GAP_ADV_EVT_MASK_SET_TERMINATED);
//newAdvInt*625= time interval in us
//changed by Ankit
uint32_t newAdvInt = 11200;//8000; //3200;
GapAdv_setParam(advHandleLegacy, GAP_ADV_PARAM_PRIMARY_INTERVAL_MIN, &newAdvInt);
GapAdv_setParam(advHandleLegacy, GAP_ADV_PARAM_TX_POWER,20);
newAdvInt = 11200;
GapAdv_setParam(advHandleLegacy, GAP_ADV_PARAM_PRIMARY_INTERVAL_MAX, &newAdvInt);
// Enable legacy advertising for set #1
status =
GapAdv_enable(advHandleLegacy, GAP_ADV_ENABLE_OPTIONS_USE_MAX, 0);
// advhandle=advHandleLegacy;
// GapAdv_enable(advHandleLegacy, GAP_ADV_ENABLE_OPTIONS_USE_DURATION, 2000);
APP_ASSERT(status == SUCCESS);
}
break;
}
case GAP_LINK_ESTABLISHED_EVENT:
{
gapEstLinkReqEvent_t *pPkt = (gapEstLinkReqEvent_t *)pMsg;
// Display the amount of current connections
Log_info2("Link establish event, status 0x%02x. Num Conns: %d",
pPkt->hdr.status,
linkDB_NumActive());
if(pPkt->hdr.status == SUCCESS)
{
// Add connection to list
ProjectZero_addConn(pPkt->connectionHandle);
// Display the address of this connection
static uint8_t addrStr[3 * B_ADDR_LEN + 1];
util_arrtohex(pPkt->devAddr, B_ADDR_LEN, addrStr, sizeof addrStr,
UTIL_ARRTOHEX_REVERSE);
Log_info1("Connected. Peer address: " \
ANSI_COLOR(FG_GREEN)"%s"ANSI_COLOR(ATTR_RESET),
(uintptr_t)addrStr);
}
if(linkDB_NumActive() < MAX_NUM_BLE_CONNS)
{
Log_info1("Continue to Advertise, %d possible connection remain", MAX_NUM_BLE_CONNS - linkDB_NumActive());
// Start advertising since there is room for more connections
GapAdv_enable(advHandleLegacy, GAP_ADV_ENABLE_OPTIONS_USE_MAX, 0);
}
else
{
Log_info1("Max Number of Connection reach: %d, Adv. will not be enable again", linkDB_NumActive());
}
}
break;
case GAP_LINK_TERMINATED_EVENT:
{
gapTerminateLinkEvent_t *pPkt = (gapTerminateLinkEvent_t *)pMsg;
// Display the amount of current connections
Log_info0("Device Disconnected!");
Log_info1("Num Conns: %d", linkDB_NumActive());
// Remove the connection from the list and disable RSSI if needed
ProjectZero_removeConn(pPkt->connectionHandle);
// GapAdv_enable will return success only if the maximum number of connections
// has been reached, and adv was not re-enable in GAP_LINK_ESTABLISHED_EVENT
// switch case.
// If less connection were in used, Advertisement will have been restart in
// the GAP_LINK_ESTABLISHED_EVENT switch case and calling GapAdv_enable will
// just return an error.
if ( GapAdv_enable(advHandleLegacy, GAP_ADV_ENABLE_OPTIONS_USE_MAX, 0) == SUCCESS)
{
Log_info1("Restart Advertising, %d possible connection remain", MAX_NUM_BLE_CONNS - linkDB_NumActive());
}
}
break;
case GAP_UPDATE_LINK_PARAM_REQ_EVENT:
ProjectZero_handleUpdateLinkParamReq(
(gapUpdateLinkParamReqEvent_t *)pMsg);
break;
case GAP_LINK_PARAM_UPDATE_EVENT:
ProjectZero_handleUpdateLinkEvent((gapLinkUpdateEvent_t *)pMsg);
break;
case GAP_PAIRING_REQ_EVENT:
// Disable advertising so that the peer device can be added to
// the resolving list
GapAdv_disable(advHandleLegacy);
break;
default:
break;
}
}