在對ble進行應用的時候,每個使用者的需求可能不盡相同。這裡著重介紹從機Broadcaster例程,只廣播不連線。
使用該例程時可以在手機使用APP上對Broadcaster進行偵錯。
安卓端在應用市場搜尋BLE偵錯助手下載使用,使用時要開啟提示所需開啟的許可權。
將Broadcaster例程燒錄到DEMO板中。
燒錄後發現一個藍芽名稱為abc的裝置沒有connect(連線)的選項,只能廣播我無法連線。
接下來主要的程式拆分討論:相對於peripheral例程,Broadcaster是比較精簡的。這裡直接從掃描應答包開始討論,在APP上我們看到裝置的是名稱是abc,對比一下peripheral的名稱為Simple Peripheral。
此時我們應該會有個疑問Broadcaster掃描應答包中的名稱應該是Broadcaster,為什麼APP上顯示的是abc呢?
這樣就可以解釋為什麼裝置名稱不是Broadcaster而是abc,這個例程只有廣播的功能,所以掃描應答包的裝置名是不會顯示出來的。
其中對 GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initial_advertising_enable);進行更多的討論
/*------------------------------------------------------------------- * FUNCTIONS - GAPRole API */ /** * @brief Set a GAP Role parameter. * * @note You can call this function with a GAP Parameter ID and it will set a GAP Parameter. * * @param param - Profile parameter ID: @ref GAPROLE_PROFILE_PARAMETERS * @param len - length of data to write * @param pValue - pointer to data to write. This is dependent on the parameter ID and * WILL be cast to the appropriate data type (example: data type of uint16_t * will be cast to uint16_t pointer). * * @return SUCCESS or INVALIDPARAMETER (invalid paramID) */ extern bStatus_t GAPRole_SetParameter( uint16_t param, uint16_t len, void *pValue );
GAPRole_SetParameter後的三個引數值分別是組態檔引數 ID、要寫入的資料長度、指向要寫入的資料的指標。
組態檔引數在lib檔案裡。
#define GAPROLE_PROFILEROLE 0x300 //!< Reading this parameter will return GAP Role type. Read Only. Size is uint8_t. #define GAPROLE_IRK 0x301 //!< Identity Resolving Key. Read/Write. Size is uint8_t[KEYLEN]. Default is all 0, which means that the IRK will be randomly generated. #define GAPROLE_SRK 0x302 //!< Signature Resolving Key. Read/Write. Size is uint8_t[KEYLEN]. Default is all 0, which means that the SRK will be randomly generated. #define GAPROLE_SIGNCOUNTER 0x303 //!< Sign Counter. Read/Write. Size is uint32_t. Default is 0. #define GAPROLE_BD_ADDR 0x304 //!< Device's Address. Read Only. Size is uint8_t[B_ADDR_LEN]. This item is read from the controller. #define GAPROLE_ADVERT_ENABLED 0x305 //!< Enable/Disable Advertising. Read/Write. Size is uint8_t. Default is TRUE=Enabled. #define GAPROLE_ADVERT_DATA 0x306 //!< Advertisement Data. Read/Write. Max size is B_MAX_ADV_EXT_LEN. Default to all 0. #define GAPROLE_SCAN_RSP_DATA 0x307 //!< Scan Response Data. Read/Write. Max size is B_MAX_ADV_EXT_LEN. Defaults to all 0. #define GAPROLE_ADV_EVENT_TYPE 0x308 //!< Advertisement Type. Read/Write. Size is uint8_t. Default is GAP_ADTYPE_ADV_IND. #define GAPROLE_ADV_DIRECT_TYPE 0x309 //!< Direct Advertisement Address Type. Read/Write. Size is uint8_t. Default is ADDRTYPE_PUBLIC. #define GAPROLE_ADV_DIRECT_ADDR 0x30A //!< Direct Advertisement Address. Read/Write. Size is uint8_t[B_ADDR_LEN]. Default is NULL. #define GAPROLE_ADV_CHANNEL_MAP 0x30B //!< Which channels to advertise on. Read/Write Size is uint8_t. Default is GAP_ADVCHAN_ALL #define GAPROLE_ADV_FILTER_POLICY 0x30C //!< Filter Policy. Ignored when directed advertising is used. Read/Write. Size is uint8_t. Default is GAP_FILTER_POLICY_ALL. #define GAPROLE_STATE 0x30D //!< Reading this parameter will return GAP Peripheral Role State. Read Only. Size is uint8_t. #define GAPROLE_MAX_SCAN_RES 0x30E //!< Maximum number of discover scan results to receive. Default is 0 = unlimited. #define GAPROLE_MIN_CONN_INTERVAL 0x311 //!< Minimum Connection Interval to allow (n * 1.25ms). Range: 7.5 msec to 4 seconds (0x0006 to 0x0C80). Read/Write. Size is uint16_t. Default is 7.5 milliseconds (0x0006). #define GAPROLE_MAX_CONN_INTERVAL 0x312 //!< Maximum Connection Interval to allow (n * 1.25ms). Range: 7.5 msec to 4 seconds (0x0006 to 0x0C80). Read/Write. Size is uint16_t. Default is 4 seconds (0x0C80). // v5.x #define GAPROLE_PHY_TX_SUPPORTED 0x313 //!< The transmitter PHYs that the Host prefers the Controller to use.Default is GAP_PHY_BIT_ALL #define GAPROLE_PHY_RX_SUPPORTED 0x314 //!< The receiver PHYs that the Host prefers the Controller to use.Default is GAP_PHY_BIT_ALL #define GAPROLE_PERIODIC_ADVERT_DATA 0x315 //!< Periodic advertisement Data. Read/Write. Max size is B_MAX_ADV_PERIODIC_LEN. Default to all 0. #define GAPROLE_PERIODIC_ADVERT_ENABLED 0x316 //!< bit0:Enable/Disable Periodic Advertising. Read/Write. Size is uint8_t. Default is FALSE=Disable. //!< bit1:Include the ADI field in AUX_SYNC_IND PDUs
這段程式碼為TMOS事件,TMOS的講解可以參照這篇部落格WCH TMOS用法詳解 - debugdabiaoge - 部落格園 (cnblogs.com)
廣播流程與狀態函數,
GAPROLE_STARTED的定義可以在lib庫中看到
#define GAPROLE_STATE_ADV_MASK (0xF) //!< advertising states mask #define GAPROLE_STATE_ADV_SHIFT (0x0) //!< advertising states shift #define GAPROLE_INIT 0 //!< Waiting to be started #define GAPROLE_STARTED 1 //!< Started but not advertising #define GAPROLE_ADVERTISING 2 //!< Currently Advertising #define GAPROLE_WAITING 3 //!< Device is started but not advertising, is in waiting period before advertising again #define GAPROLE_CONNECTED 4 //!< In a connection #define GAPROLE_CONNECTED_ADV 5 //!< In a connection + advertising #define GAPROLE_ERROR 6 //!< Error occurred - invalid state
這只是最基礎的討論,如有問題請指正!
如轉載請標明出處!文章可能被無良網站搬運。某些網站拿著別人的文章寫著「我的程式設計學習分享」。
禁止寫著我的程式設計學習分享的網站轉載。