标签:
1 选择从节点,建立连接
1 if ( !simpleBLEScanning && simpleBLEScanRes > 0)
2 {
3 simpleBLEScanIdx++;
4 if ( simpleBLEScanIdx >= simpleBLEScanRes )
5 {
6 simpleBLEScanIdx = 0;
7 }
8 uint8 addrType;
9 uint8 *peerAddr;
10
11 if ( simpleBLEScanRes > 0 )
12 {
13 peerAddr = simpleBLEDevList[simpleBLEScanIdx].addr;
14 addrType = simpleBLEDevList[simpleBLEScanIdx].addrType;
15
16 simpleBLEState = BLE_STATE_CONNECTING;
17
18 GAPCentralRole_EstablishLink( DEFAULT_LINK_HIGH_DUTY_CYCLE,
19 DEFAULT_LINK_WHITE_LIST,
20 addrType, peerAddr );//发起连接建立请求
21 }
连接好以后调用simpleBLECentralEventCB——>GAP_DEVICE_DISCOVERY_EVENT
1 LCD_WRITE_STRING_VALUE( "Devices Found", simpleBLEScanRes,
2 10, HAL_LCD_LINE_1 );
3 if ( simpleBLEScanRes > 0 )
4 {
5 LCD_WRITE_STRING( "<- To Select", HAL_LCD_LINE_2 );
6
7 }
2 主节点向从节点通信
主节点向特征值6中写入要传输数据:
1 attWriteReq_t req;
2
3 req.handle = 0x0035; //特征值6的handle
4 req.len = 15; //value的长度为15,这要和从节点匹配好
5 req.value[0] = 0x00;
6 req.sig = 0;
7 req.cmd = 0;
8
9 GATT_WriteCharValue( simpleBLEConnHandle, &req, simpleBLETaskId );
从节点在simpleProfileChangeCB——>SIMPLEPROFILE_CHAR6中接收:
1 case SIMPLEPROFILE_CHAR6: //特征值6 2 char newchar[15]; 3 SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR6, &newchar ); 4 5 HalUARTWrite(0,&newchar[0],15); 6 7 8 break;
注意handle值设对,长度匹配在simpleGATTprofile.h中设好:
1 // Length of Characteristic 5 in bytes 2 #define SIMPLEPROFILE_CHAR5_LEN 5 3 #define SIMPLEPROFILE_CHAR6_LEN 15 4 #define SIMPLEPROFILE_CHAR7_LEN 15
3 从节点向主节点通信
从节点主动传输:
1 static attHandleValueNoti_t pReport ;//声明attHandleValueNoti_t这个结构体 2 uint16 noti_cHandle; //存放handle 3 4 pReport.handle = 0x2E; //特征值4的handle 5 //pReport.handle = simpleProfileAttrTbl[11].handle;//读取notification对应的handle,不知道为什么不好使?? 6 GAPRole_GetParameter( 0x30E, ¬i_cHandle);//获取Connection Handle 7 pReport.len = 16;//数据长度 8 9 pReport.value[0] =0x01;//赋值 10 pReport.value[1] =0x02; 11 GATT_Notification(noti_cHandle,&pReport,FALSE); 12
主节点在simpleBLECentralProcessGATTMsg中接收:
1 else if ( ( pMsg->method == ATT_HANDLE_VALUE_NOTI ) ) //通知 2 { 3 if( pMsg->msg.handleValueNoti.handle == 0x002E) //CHAR4的通知 串口打印 4 { 5 6 7 HalUARTWrite(0,&pMsg->msg.handleValueNoti.value[0],16 ); 8 }
网上说主节点需要先使能通知char4才能接收notification,但我现在的程序可以直接接收,不知道为什么??
标签:
原文地址:http://www.cnblogs.com/fullest-life/p/5432743.html