GAPBOND_PAIRING_MODE_WAIT_FOR_REQ
改为GAPBOND_PAIRING_MODE_INITIATE
1
2
static void passcodeCB(uint8 *deviceAddr,uint16 connectionHandle,uint8 uiInputs,uint8 uiOutputs);
static void pairStateCB(uint16 connectionHandle, uint8 state, uint8 status);
\Projects\ble\Profiles\Roles\gapbondmgr.h
中
1
2
3
4
5
typedef enum
{
PAIRSTATUS_PAIRED = 0,
PAIRSTATUS_NO_PAIRED,
}PAIRSTATUS;
1
PAIRSTATUS gPairStatus;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
static void passcodeCB(uint8 *deviceAddr,uint16 connectionHandle,uint8 uiInputs,uint8 uiOutputs)
{
uint32 passcode;
#if 0
uint8 str[7];
LL_Rand( ((uint8 *) &passcode), sizeof( uint32 ));//产生随机密码
passcode %= 1000000;
#else
passcode = 456890;//固定密码
#endif
//在LCD上显示
if ( uiOutputs != 0 )
{
#if (defined HAL_LCD) && (HAL_LCD == TRUE)
HalLcdWriteString( "Passcode:", HAL_LCD_LINE_1 );
HalLcdWriteString( (char *) _ltoa(passcode, str, 10), HAL_LCD_LINE_2 );
#endif
}
//发送密码请求
GAPBondMgr_PasscodeRsp( connectionHandle, SUCCESS, passcode );
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
static void pairStateCB(uint16 connectionHandle, uint8 state, uint8 status)
{
//主机发起连接,会进入开始配对状态
if ( state == GAPBOND_PAIRING_STATE_STARTED )
{
#if (defined HAL_LCD) && (HAL_LCD == TRUE)
HalLcdWriteString( "Pairing started", HAL_LCD_LINE_1 );
#endif
gPairStatus = PAIRSTATUS_NO_PAIRED;
}
//当主机提交密码后,会进入配对完成状态
else if ( state == GAPBOND_PAIRING_STATE_COMPLETE )
{
//配对成功
if ( status == SUCCESS )
{
#if (defined HAL_LCD) && (HAL_LCD == TRUE)
HalLcdWriteString( "Pairing success", HAL_LCD_LINE_1 );
#endif
gPairStatus = PAIRSTATUS_PAIRED;
}
//已配对过
else if(status == SMP_PAIRING_FAILED_UNSPECIFIED)
{
#if (defined HAL_LCD) && (HAL_LCD == TRUE)
HalLcdWriteString( "Paired device", HAL_LCD_LINE_1 );
#endif
gPairStatus = PAIRSTATUS_PAIRED;
}
//配对失败
else
{
#if (defined HAL_LCD) && (HAL_LCD == TRUE)
HalLcdWriteStringValue( "Pairing fail", status, 10, HAL_LCD_LINE_1 );
#endif
gPairStatus = PAIRSTATUS_NO_PAIRED;
}
//配对失败则断开连接
if(gPairStatus == PAIRSTATUS_NO_PAIRED)
{
GAPRole_TerminateConnection();
}
}
else if ( state == GAPBOND_PAIRING_STATE_BONDED )
{
if ( status == SUCCESS )
{
#if (defined HAL_LCD) && (HAL_LCD == TRUE)
HalLcdWriteString( "Bonding success", HAL_LCD_LINE_1 );
#endif
}
}
}
1
2
passcodeCB, // 密码回调
pairStateCB // 绑定状态回调
456890
,连接成功