|
实验步骤
1、添加一个绑定与否的状态变量(simpleBLEPeripheral.c)
[cpp] view plai copy
- //GUA
- typedef enum
- {
- PAIRSTATUS_PAIRED = 0,
- PAIRSTATUS_NO_PAIRED,
- }PAIRSTATUS;
- static PAIRSTATUS gPairStatus = PAIRSTATUS_NO_PAIRED;//配对状态,默认是没配对
2、修改绑定初始化(simpleBLEPeripheral.c的SimpleBLEPeripheral_Init函数中)
[cpp] view plain copy
- // Setup the GAP Bond Manager
- {
- uint32_t passkey = 0; // passkey "000000"
- //uint8_t pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ;
- uint8 pairMode = GAPBOND_PAIRING_MODE_INITIATE;
- uint8_t mitm = TRUE;
- uint8_t ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY;
- uint8_t bonding = TRUE;
-
- GAPBondMgr_SetParameter(GAPBOND_DEFAULT_PASSCODE, sizeof(uint32_t),
- &passkey);
- GAPBondMgr_SetParameter(GAPBOND_PAIRING_MODE, sizeof(uint8_t), &pairMode);
- GAPBondMgr_SetParameter(GAPBOND_MITM_PROTECTION, sizeof(uint8_t), &mitm);
- GAPBondMgr_SetParameter(GAPBOND_IO_CAPABILITIES, sizeof(uint8_t), &ioCap);
- GAPBondMgr_SetParameter(GAPBOND_BONDING_ENABLED, sizeof(uint8_t), &bonding);
- }
注:
1)GAPBOND_PAIRING_MODE_WAIT_FOR_REQ 这个宏是表示等待对方发起配对请求的模式,如果主从机都是这种模式,则双方都不会发起配对请求。 因此主从机的其中一方必须要配置成GAPBOND_PAIRING_MODE_INITIATE方可进行配对。 从机中往往有机密数据,因此从机端需要配置成GAPBOND_PAIRING_MODE_INITIATE,否则如果配对请求的主动权交予主机,主机一旦不发起请求,那也就无配对过程,导致从机的数据公之于众了。
2)GAPBOND_IO_CAP_DISPLAY_ONLY 这个宏是只显示密码,将密码显示在LCD上给主机看,主机也就知道密码了。 如果设备有按键可以输入密码,也可以选择GAPBOND_IO_CAP_KEYBOARD_ONLY。
3、注册回调函数(simpleBLEPeripheral.c中) 1)注册回调函数 [cpp] view plain copy
- // GAP Bond Manager Callbacks
- static gapBondCBs_t simpleBLEPeripheral_BondMgrCBs =
- {
- GUA_PasscodeCB, //密码回调
- GUA_PairStateCB //绑定状态回调
- };
2)定义配对相关函数、绑定相关函数
[cpp] view plain copy
- //******************************************************************************
- //name: GUA_PasscodeCB
- //introduce: 配对回调
- //parameter: none
- //return: none
- //author: 甜甜的大香瓜
- //email: 897503845@qq.com
- //QQ group 香瓜BLE之CC2640(557278427)
- //changetime: 2016.09.01
- //******************************************************************************
- static void GUA_PasscodeCB(uint8 *deviceAddr,uint16 connectionHandle,uint8 uiInputs,uint8 uiOutputs)
- {
- // Note: the only parameter used here is connectionHandle, but this has
- // already been obtained by gapConnHandle so the argument is not stored.
-
- // Queue the event.
- SimpleBLEPeripheral_enqueueMsg(SBP_GUA_PASSCODE_EVT, 0, NULL);
- }
-
- //******************************************************************************
- //name: GUA_PasscodeEvt
- //introduce: 配对处理
- //parameter: none
- //return: none
- //author: 甜甜的大香瓜
- //email: 897503845@qq.com
- //QQ group 香瓜BLE之CC2640(557278427)
- //changetime: 2016.09.01
- //******************************************************************************
- static void GUA_PasscodeEvt(uint16 connectionHandle)
- {
- uint32 passcode;
-
- //设置密码
- passcode = 456890;
-
- //发送密码响应给主机
- GAPBondMgr_PasscodeRsp( connectionHandle, SUCCESS, passcode );
- }
-
- //******************************************************************************
- //name: GUA_PairStateCB
- //introduce: 绑定回调
- //parameter: none
- //return: none
- //author: 甜甜的大香瓜
- //email: 897503845@qq.com
- //QQ group 香瓜BLE之CC2640(557278427)
- //changetime: 2016.09.01
- //******************************************************************************
- static void GUA_PairStateCB(uint16 connHandle, uint8 state, uint8 status)
- {
- uint8_t *pData;
-
- // Note: connHandle is not used, so the variable is not stored.
-
- // Allocate message data
- if ((pData = ICall_malloc(sizeof(uint8_t))))
- {
- *pData = status;
-
- // Queue the event.
- SimpleBLEPeripheral_enqueueMsg(SBP_GUA_PAIRING_STATE_EVT, state, pData);
- }
- }
-
- //******************************************************************************
- //name: GUA_PairStateEvt
- //introduce: 绑定处理
- //parameter: none
- //return: none
- //author: 甜甜的大香瓜
- //email: 897503845@qq.com
- //QQ group 香瓜BLE之CC2640(557278427)
- //changetime: 2016.09.01
- //******************************************************************************
- static void GUA_PairStateEvt(uint8_t state, uint8_t status)
- {
- //主机发起连接,会进入开始配对状态
- if ( state == GAPBOND_PAIRING_STATE_STARTED )
- {
- gPairStatus = PAIRSTATUS_NO_PAIRED;
- }
-
- //当主机提交密码后,会进入配对完成状态
- else if ( state == GAPBOND_PAIRING_STATE_COMPLETE )
- {
- //配对成功
- if ( status == SUCCESS )
- {
- gPairStatus = PAIRSTATUS_PAIRED;
- }
-
- //已配对过
- else if(status == SMP_PAIRING_FAILED_UNSPECIFIED)
- {
- gPairStatus = PAIRSTATUS_PAIRED;
- }
-
- //配对失败
- else
- {
- gPairStatus = PAIRSTATUS_NO_PAIRED;
- }
-
- //配对失败则断开连接
- if(gPairStatus == PAIRSTATUS_NO_PAIRED)
- {
- GAPRole_TerminateConnection();
- }
- }
- else if ( state == GAPBOND_PAIRING_STATE_BONDED )
- {
- if ( status == SUCCESS )
- {
-
- }
- }
- }
3)声明回调函数
[cpp] view plain copy
- //GUA
- static void GUA_PasscodeCB(uint8 *deviceAddr,uint16 connectionHandle,uint8 uiInputs,uint8 uiOutputs);
- static void GUA_PairStateCB(uint16 connHandle, uint8 state, uint8 status);
- static void GUA_PairStateEvt(uint8_t state, uint8_t status);
- static void GUA_PasscodeEvt(uint16 connectionHandle);
4、修改应用层消息处理
1)添加事件宏(simpleBLEPeripheral.c中)
[cpp] view plain copy
- //GUA
- #define SBP_GUA_PASSCODE_EVT 0x0100 //配对事件
- #define SBP_GUA_PAIRING_STATE_EVT 0x0200 //绑定事件
2)添加事件处理(替换simpleBLEPeripheral.c中的SimpleBLEPeripheral_processAppMsg函数)
[cpp] view plain copy
- static void SimpleBLEPeripheral_processAppMsg(sbpEvt_t *pMsg)
- {
- switch (pMsg->hdr.event)
- {
- case SBP_STATE_CHANGE_EVT:
- SimpleBLEPeripheral_processStateChangeEvt((gaprole_States_t)pMsg->
- hdr.state);
- break;
-
- case SBP_CHAR_CHANGE_EVT:
- SimpleBLEPeripheral_processCharValueChangeEvt(pMsg->hdr.state);
- break;
-
- // Passcode CB event.
- case SBP_GUA_PASSCODE_EVT:
- GUA_PasscodeEvt(0);
- break;
-
- // Pairing state CB event.
- case SBP_GUA_PAIRING_STATE_EVT:
- GUA_PairStateEvt(pMsg->hdr.state, *pMsg->pData);
-
- ICall_free(pMsg->pData);
- break;
-
- default:
- // Do nothing.
- break;
- }
- }
5、修改消息传递的相关函数(simpleBLEPeripheral.c中)
1)替换消息传递函数(simpleBLEPeripheral.c中SimpleBLEPeripheral_enqueueMsg函数)
[cpp] view plain copy
- static void SimpleBLEPeripheral_enqueueMsg(uint8_t event, uint8_t state, uint8_t *pData)
- {
- sbpEvt_t *pMsg;
-
- // Create dynamic pointer to message.
- if ((pMsg = ICall_malloc(sizeof(sbpEvt_t))))
- {
- pMsg->hdr.event = event;
- pMsg->hdr.state = state;
- pMsg->pData = pData;
-
- // Enqueue the message.
- Util_enqueueMsg(appMsgQueue, sem, (uint8*)pMsg);
- }
- }
2)修改声明
[cpp] view plain copy
- static void SimpleBLEPeripheral_enqueueMsg(uint8_t event, uint8_t state, uint8_t *pData);
3)修改剩余相关函数
在所有调用到SimpleBLEPeripheral_enqueueMsg函数的协议栈原有代码中,添加第三个参数NULL,如:
[cpp] view plain copy
- SimpleBLEPeripheral_enqueueMsg(SBP_STATE_CHANGE_EVT, newState, NULL);
在此不一一列举,可rebuild all后自行修改。
4)替换结构体sbpEvt_t
[cpp] view plain copy
- // App event passed from profiles.
- typedef struct
- {
- appEvtHdr_t hdr; // event header.
- uint8_t *pData; // Event data.
- } sbpEvt_t;
为传递绑定状态而增加pData数据。
实验结果
用手机app连接,第一次弹窗选中“配对”,第二次弹窗输入“456890”密码,即可通信正常使用。
因此,实验成功。
|
|