TI中文支持网
TI专业的中文技术问题搜集分享网站

蓝牙通信异常,可以连接不能通信

哪位大神帮我看看看,以下程序只能连接,通信不了,经查主要是
simpleBLECharHdl这个值一直是零,不知道为什么??求解

void SimpleBLECentral_Init( uint8 task_id )
{simpleBLETaskId = task_id;//UARTUartInit();RegisterForSerial( simpleBLETaskId );keyinitfor2540();	//按键初始化;HalUARTWrite(0,"SendSuccessL",12);// Setup Central Profile{uint8 scanRes = DEFAULT_MAX_SCAN_RES;GAPCentralRole_SetParameter ( GAPCENTRALROLE_MAX_SCAN_RES, sizeof( uint8 ), &scanRes );}// Setup GAPGAP_SetParamValue( TGAP_GEN_DISC_SCAN, DEFAULT_SCAN_DURATION );GAP_SetParamValue( TGAP_LIM_DISC_SCAN, DEFAULT_SCAN_DURATION );GGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, (uint8 *) simpleBLEDeviceName );// Setup the GAP Bond Manager{uint32 passkey = DEFAULT_PASSCODE;uint8 pairMode = DEFAULT_PAIRING_MODE;uint8 mitm = DEFAULT_MITM_MODE;uint8 ioCap = DEFAULT_IO_CAPABILITIES;uint8 bonding = DEFAULT_BONDING_MODE;GAPBondMgr_SetParameter( GAPBOND_DEFAULT_PASSCODE, sizeof( uint32 ), &passkey );GAPBondMgr_SetParameter( GAPBOND_PAIRING_MODE, sizeof( uint8 ), &pairMode );GAPBondMgr_SetParameter( GAPBOND_MITM_PROTECTION, sizeof( uint8 ), &mitm );GAPBondMgr_SetParameter( GAPBOND_IO_CAPABILITIES, sizeof( uint8 ), &ioCap );GAPBondMgr_SetParameter( GAPBOND_BONDING_ENABLED, sizeof( uint8 ), &bonding );}// Initialize GATT ClientVOID GATT_InitClient();#ifdef WATCH_DOG//WatchDog InitializeWDCTL |= 0x00;WDCTL |= 0x08;
#endif// Register to receive incoming ATT Indications/NotificationsGATT_RegisterForInd( simpleBLETaskId );// Initialize GATT attributesGGS_AddService( GATT_ALL_SERVICES );// GAPGATTServApp_AddService( GATT_ALL_SERVICES ); // GATT attributes// Register for all key events - This app will handle all key events//RegisterForKeys( simpleBLETaskId );// makes sure LEDs are offHalLedSet( (HAL_LED_1 | HAL_LED_2), HAL_LED_MODE_OFF );// Setup a delayed profile startuposal_set_event( simpleBLETaskId, START_DEVICE_EVT );
}

/*********************************************************************
 * @fnSimpleBLECentral_ProcessEvent
 *
 * @briefSimple BLE Central Application Task event processor.  This function
 *is called to process all events for the task.  Events
 *include timers, messages and any other user defined events.
 *
 * @paramtask_id  - The OSAL assigned task ID.
 * @paramevents - events to process.  This is a bit map and can
 *contain more than one event.
 *
 * @return  events not processed
 */
uint8 conn_counter = 0;


uint16 SimpleBLECentral_ProcessEvent( uint8 task_id, uint16 events )
{VOID task_id; // OSAL required parameter that isn't used in this functionif ( events & SYS_EVENT_MSG ){uint8 *pMsg;if ( (pMsg = osal_msg_receive( simpleBLETaskId )) != NULL ){simpleBLECentral_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );// Release the OSAL messageVOID osal_msg_deallocate( pMsg );}// return unprocessed eventsreturn (events ^ SYS_EVENT_MSG);}if ( events & START_DEVICE_EVT ){// Start the DeviceVOID GAPCentralRole_StartDevice( (gapCentralRoleCB_t *) &simpleBLERoleCB );// Register with bond manager after starting deviceGAPBondMgr_Register( (gapBondCBs_t *) &simpleBLEBondCB );

	
	//osal_start_timerEx(simpleBLETaskId,START_DISCOVERY_EVT,500);return ( events ^ START_DEVICE_EVT );}if ( events & START_DISCOVERY_EVT ){simpleBLECentralStartDiscovery();//osal_start_timerEx(simpleBLETaskId,START_SCAN_EVT,500);return ( events ^ START_DISCOVERY_EVT );}if ( events & START_SCAN_EVT ){//自动开始搜索
		HalUARTWrite(0,"scaning\n",10 );if ( !simpleBLEScanning & simpleBLEScanRes == 0 ){simpleBLEScanning = TRUE;simpleBLEScanRes = 0;GAPCentralRole_StartDiscovery( DEFAULT_DISCOVERY_MODE,DEFAULT_DISCOVERY_ACTIVE_SCAN,DEFAULT_DISCOVERY_WHITE_LIST );}else{
//////#ifdef DEBUGHalUARTWrite(0,"no scan\n", 8 ); 
//////#else 
//////
//////HalUARTWrite(0,no_scan,7);
//////#endif
//////
//////osal_start_timerEx( simpleBLETaskId, SYSTEM_RESET_EVT,200);}return ( events ^ START_SCAN_EVT );}if ( events & GATT_SEND_LOW_EVT ){
//if ( ( simpleBLEState == BLE_STATE_CONNECTED  ) && ( simpleBLECharHdl != 0 ) ) //写char6
//{
//uint8 ValueBuf[SIMPLEPROFILE_CHAR6_LEN];
//
//for(uint8 i=0;i<=GATTlenLow;i++)
//{
//ValueBuf[i] = GATTstrLow[i];
//}
//
//gattPrepareWriteReq_t req;
//req.handle = simpleBLECharHdl;
//req.len = SIMPLEPROFILE_CHAR6_LEN;
//req.offset = 0;
//req.pValue = osal_msg_allocate(SIMPLEPROFILE_CHAR6_LEN);
//osal_memcpy(req.pValue,ValueBuf,SIMPLEPROFILE_CHAR6_LEN);
//GATT_WriteLongCharValue( simpleBLEConnHandle, &req, simpleBLETaskId );  
//
////HalUARTWrite(0,"SendSuccessL",12);
//}
//else
//{
//HalUARTWrite(0,"Not Connect\n", 12 );
//}return(events ^ GATT_SEND_LOW_EVT);}if(events & ENABLE_NOTICE_EVT ) //使能通知
 {
//uint8 ValueBuf[2];
//gattPrepareWriteReq_t req;
//
//req.handle = 0x0039;
//req.len = 2;
//ValueBuf[0] = 0x01;
//ValueBuf[1] = 0x00;
//req.offset = 0;
//req.pValue = osal_msg_allocate(2);
//osal_memcpy(req.pValue,ValueBuf,2);
//GATT_WriteLongCharValue( simpleBLEConnHandle, &req, simpleBLETaskId );HalUARTWrite(0,"\nenable notic\n",15 );attWriteReq_t writeReq;writeReq.handle = 0x0039;//1C;writeReq.len = 2;writeReq.value[0] = 0x01;writeReq.value[1] = 0x00;writeReq.sig = 0;writeReq.cmd = 0;GATT_WriteCharDesc( simpleBLEConnHandle, &writeReq, simpleBLETaskId );return(events ^ ENABLE_NOTICE_EVT );
 }
 
/////////////////////////////////用户编写//////////////////////////////////////if(events & VERSION_WRITE_EVT)//版本信息{if ( ( simpleBLEState == BLE_STATE_CONNECTED  ) )//&& ( simpleBLECharHdl != 0 ) ) //写char6{
//给从机写数据用
//GATT_WriteCharValue( simpleBLEConnHandle, &writeReq, simpleBLETaskId );
//gattPrepareWriteReq_t req;
//
//req.handle = simpleBLECharHdl;
//req.len = SIMPLEPROFILE_CHAR6_LEN;
//req.offset = 0;
//req.pValue = osal_msg_allocate(SIMPLEPROFILE_CHAR6_LEN);
//osal_memcpy(req.pValue,ver_data,SIMPLEPROFILE_CHAR6_LEN);
//GATT_WriteLongCharValue( simpleBLEConnHandle, &req, simpleBLETaskId );attWriteReq_t writeReq;writeReq.handle = simpleBLECharHdl;writeReq.len = SIMPLEPROFILE_CHAR6_LEN;
//writeReq.value[0] = 0x01;
//writeReq.value[1] = 0x00;writeReq.sig = 0;writeReq.cmd = 0;osal_memcpy(writeReq.value,ver_data,SIMPLEPROFILE_CHAR6_LEN);GATT_WriteCharValue( simpleBLEConnHandle, &writeReq, simpleBLETaskId );HalUARTWrite(0,"\nversion0000 \n",20 );}else{HalUARTWrite(0,"\nversion222222 \n",20 );}return(events ^ VERSION_WRITE_EVT );
 }if(events & ALARM_WRITE_EVT)//报警信息
 {if ( ( simpleBLEState == BLE_STATE_CONNECTED  ) && ( simpleBLECharHdl != 0 ) ) //写char6{gattPrepareWriteReq_t req;req.handle = simpleBLECharHdl;req.len = SIMPLEPROFILE_CHAR6_LEN;req.offset = 0;req.pValue = osal_msg_allocate(DATALENGTH);osal_memcpy(req.pValue,alarm_data,DATALENGTH);GATT_WriteLongCharValue( simpleBLEConnHandle, &req, simpleBLETaskId );}else{///}return(events ^ ALARM_WRITE_EVT );
 }if(events & TRIG_WRITE_EVT)//触发信息
 {if ( ( simpleBLEState == BLE_STATE_CONNECTED  ) && ( simpleBLECharHdl != 0 ) ) //写char6{gattPrepareWriteReq_t req;req.handle = simpleBLECharHdl;req.len = SIMPLEPROFILE_CHAR6_LEN;req.offset = 0;req.pValue = osal_msg_allocate(DATALENGTH);osal_memcpy(req.pValue,trig_data,DATALENGTH);GATT_WriteLongCharValue( simpleBLEConnHandle, &req, simpleBLETaskId );}else{///}return(events ^ TRIG_WRITE_EVT );
 }if(events & CONTR_WRITE_EVT)//控制信息
 {if ( ( simpleBLEState == BLE_STATE_CONNECTED  ) && ( simpleBLECharHdl != 0 ) ) //写char6{gattPrepareWriteReq_t req;req.handle = simpleBLECharHdl;req.len = SIMPLEPROFILE_CHAR6_LEN;req.offset = 0;req.pValue = osal_msg_allocate(DATALENGTH);osal_memcpy(req.pValue,ctr_data,DATALENGTH);GATT_WriteLongCharValue( simpleBLEConnHandle, &req, simpleBLETaskId );}else{///}return(events ^ CONTR_WRITE_EVT );
 }if(events & THDATA_WRITE_EVT)//温湿信息
 {if ( ( simpleBLEState == BLE_STATE_CONNECTED  ) && ( simpleBLECharHdl != 0 ) ) //写char6{gattPrepareWriteReq_t req;req.handle = simpleBLECharHdl;req.len = SIMPLEPROFILE_CHAR6_LEN;req.offset = 0;req.pValue = osal_msg_allocate(DATALENGTH);osal_memcpy(req.pValue,th_data,DATALENGTH);GATT_WriteLongCharValue( simpleBLEConnHandle, &req, simpleBLETaskId );}else{///}
	return(events ^ THDATA_WRITE_EVT );
 }
 
if(events & LINK_TEST_EVT)//连接测试
{
	if ( ( simpleBLEState == BLE_STATE_CONNECTED  ) && ( simpleBLECharHdl != 0 ) ) //写char6{HalUARTWrite(0,"ANJUBAO_LINK\n",13 );gattPrepareWriteReq_t req;req.handle = simpleBLECharHdl;req.len = SIMPLEPROFILE_CHAR6_LEN;req.offset = 0;req.pValue = osal_msg_allocate(DATALENGTH);osal_memcpy(req.pValue,"ANJUBAO_LINK",DATALENGTH);GATT_WriteLongCharValue( simpleBLEConnHandle, &req, simpleBLETaskId );HalUARTWrite(0,"test succee\n",15 );}else{HalUARTWrite(0,"test failed\n",15 );}
	HalUARTWrite(0,"11test failed\n",15 );
	return(events ^ LINK_TEST_EVT );
}if(events & TRY_CONNECT_EVT)//循环连接
 {if((conn_counter<simpleBLEScanRes)&&
		((simpleBLEState != BLE_STATE_CONNECTING)||(simpleBLEState != BLE_STATE_CONNECTED))){HalUARTWrite(0,"tryconn\n",7 );uint8 addrType;uint8 *peerAddr;peerAddr = simpleBLEDevList[conn_counter].addr;addrType = simpleBLEDevList[conn_counter].addrType;simpleBLEState = BLE_STATE_CONNECTING;GAPCentralRole_EstablishLink( DEFAULT_LINK_HIGH_DUTY_CYCLE,DEFAULT_LINK_WHITE_LIST,addrType, peerAddr );for(uint8 i=0;i<simpleBLEScanRes;i++){HalUARTWrite(0,bdAddr2Str(simpleBLEDevList[conn_counter].addr),20 );HalUARTWrite(0,"\n",1 );}conn_counter++;osal_start_timerEx(simpleBLETaskId,LINK_TEST_EVT,1000);}else{//复位后重新开始搜索conn_counter = 0;//osal_start_timerEx( simpleBLETaskId, SYSTEM_RESET_EVT,200);}return(events ^ TRY_CONNECT_EVT);
 }
 
////////////////////////////////用户编写/////////////////////////////////////////if( events & SYSTEM_RESET_EVT){HAL_SYSTEM_RESET();return (events ^ SYSTEM_RESET_EVT);}// Discard unknown eventsreturn 0;
}

/*********************************************************************
 * @fnsimpleBLECentral_ProcessOSALMsg
 *
 * @briefProcess an incoming task message.
 *
 * @parampMsg - message to process
 *
 * @return  none
 */
static void simpleBLECentral_ProcessOSALMsg( osal_event_hdr_t *pMsg )
{switch ( pMsg->event ){case KEY_CHANGE:HalUARTWrite(0,"key\n",7 );simpleBLECentral_HandleKeys( ((keyChange_t *)pMsg)->state, ((keyChange_t *)pMsg)->keys );break;case GATT_MSG_EVENT:HalUARTWrite(0,"sysmsg\n",7 );simpleBLECentralProcessGATTMsg( (gattMsgEvent_t *) pMsg );break;case SERIAL_MSG:	//串口接收事件//HalUARTWrite(HAL_UART_PORT_0,"\nuart\n",7 );simpleBLEPeripheral_HandleSerial( (mtOSALSerialData_t *)pMsg );//HalUARTWrite(0,"\npreuart\n",10 );break;}
}

uint8 gStatus;
static void simpleBLEPeripheral_HandleSerial(mtOSALSerialData_t *cmdMsg)
{uint8 len,*str=NULL;  //len有用数据长度//uint8 Remberflag=0;str=cmdMsg->msg;//指向数据开头len=*str;//msg里的第1个字节代表后面的数据长度uint8 *buf = NULL;buf = str+1;HalUARTWrite(0,buf,len );if(*(buf+1)=='0'){if(*(buf+2)=='0'){//strcpy(ver_data,buf);osal_memcpy(ver_data,buf,DATALENGTH);osal_start_timerEx( simpleBLETaskId, VERSION_WRITE_EVT,100);//版本信息HalUARTWrite(0,"version",7) ;}if(*(buf+2)=='1'){//strcpy(alarm_data,buf);osal_memcpy(alarm_data,buf,DATALENGTH);osal_start_timerEx( simpleBLETaskId, ALARM_WRITE_EVT,100);//报警信息//HalUARTWrite(0,"alarm",5) ;}if(*(buf+2)=='2'){//strcpy(trig_data,buf);osal_memcpy(trig_data,buf,DATALENGTH);osal_start_timerEx( simpleBLETaskId, TRIG_WRITE_EVT,500);//触发信息//HalUARTWrite(0,"trig",4) ;}}if(*(buf+1)=='8'){if(*(buf+2)=='0'){//strcpy(ctr_data,buf);osal_memcpy(ctr_data,buf,DATALENGTH);osal_start_timerEx( simpleBLETaskId, CONTR_WRITE_EVT,500);//控制信息//HalUARTWrite(0,"contr",5) ;}if(*(buf+2)=='1'){//strcpy(th_data,buf);osal_memcpy(th_data,buf,DATALENGTH);osal_start_timerEx( simpleBLETaskId, THDATA_WRITE_EVT,500);//温度信息//HalUARTWrite(0,"th_data",5) ;}}
}

/*********************************************************************
 * @fnsimpleBLECentral_HandleKeys
 *
 * @briefHandles all key events for this device.
 *
 * @paramshift - true if in shift/alt.
 * @paramkeys - bit field for key events. Valid entries:
 *HAL_KEY_SW_2
 *HAL_KEY_SW_1
 *
 * @return  none
 */
static void simpleBLECentral_HandleKeys( uint8 shift, uint8 keys )
{(void)shift;  // Intentionally unreferenced parameterif ( keys & HAL_KEY_SW_1 )//P1_0暂时用于蓝牙连接按键触发{HalUARTWrite(0,"keyscaning\n",12 );//osal_start_timerEx(simpleBLETaskId,START_SCAN_EVT,500);}if ( keys & HAL_KEY_SW_2 ){HalUARTWrite( 0 ,"KEY K2\n", 7 );}
}

/*********************************************************************
 * @fnsimpleBLECentralProcessGATTMsg
 *
 * @briefProcess GATT messages
 *
 * @return  none
 *主机接收处理函数
 */
static void simpleBLECentralProcessGATTMsg( gattMsgEvent_t *pMsg )
{
	if ( simpleBLEState != BLE_STATE_CONNECTED ){// In case a GATT message came after a connection has dropped,// ignore the messagereturn;}if ( ( pMsg->method == ATT_READ_RSP ) ||( ( pMsg->method == ATT_ERROR_RSP ) &&( pMsg->msg.errorRsp.reqOpcode == ATT_READ_REQ ) ) ){if ( pMsg->method == ATT_ERROR_RSP ){uint8 status = pMsg->msg.errorRsp.errCode;LCD_WRITE_STRING_VALUE( "Read Error", status, 10, HAL_LCD_LINE_1 );}else{// After a successful read, display the read valueuint8 valueRead = pMsg->msg.readRsp.value[0];LCD_WRITE_STRING_VALUE( "Read rsp:", valueRead, 10, HAL_LCD_LINE_1 );}simpleBLEProcedureInProgress = FALSE;}else if ( ( pMsg->method == ATT_WRITE_RSP ) ||( ( pMsg->method == ATT_ERROR_RSP ) &&( pMsg->msg.errorRsp.reqOpcode == ATT_WRITE_REQ ) ) ){if ( pMsg->method == ATT_ERROR_RSP == ATT_ERROR_RSP ){uint8 status = pMsg->msg.errorRsp.errCode;LCD_WRITE_STRING_VALUE( "Write Error", status, 10, HAL_LCD_LINE_1 );}else{// After a succesful write, display the value that was written and increment valueLCD_WRITE_STRING_VALUE( "Write sent:", simpleBLECharVal++, 10, HAL_LCD_LINE_1 );}simpleBLEProcedureInProgress = FALSE;}else if ( simpleBLEDiscState != BLE_DISC_STATE_IDLE ){simpleBLEGATTDiscoveryEvent( pMsg );}else if ( ( pMsg->method == ATT_HANDLE_VALUE_NOTI ) )//通知{if( pMsg->msg.handleValueNoti.handle == 0x0038)//CHAR7的通知{HalUARTWrite(0,"NOTICE\n",7 );
		uint8 i=0;
		///////////////////////////////////////////////////////////////////////
		if(pMsg->msg.handleValueNoti.value[0] == '*' &&pMsg->msg.handleValueNoti.value[1] == '0' &&pMsg->msg.handleValueNoti.value[2] == '0')
		{
			i++;
			HalUARTWrite( 0 ,"A", 1 );
			//版本信息
		}
		
		if(pMsg->msg.handleValueNoti.value[0] == '*' &&pMsg->msg.handleValueNoti.value[1] == '0' &&pMsg->msg.handleValueNoti.value[2] == '1')
		{
			i++;
			HalUARTWrite( 0 ,"C", 1 );
			//报警信息
		}
		
		if(pMsg->msg.handleValueNoti.value[0] == '*' &&pMsg->msg.handleValueNoti.value[1] == '0' &&pMsg->msg.handleValueNoti.value[2] == '2')
		{
			i++;
			HalUARTWrite( 0 ,"E", 1 );
			//触发信息
		}
		
		
		if(pMsg->msg.handleValueNoti.value[0] == '*' &&pMsg->msg.handleValueNoti.value[1] == '8' &&pMsg->msg.handleValueNoti.value[2] == '0')
		{
			i++;
			if(pMsg->msg.handleValueNoti.value[3] == 0 &&pMsg->msg.handleValueNoti.value[4] == 0 &&pMsg->msg.handleValueNoti.value[5] == '#')
				
			{
				HalUARTWrite( 0 ,"B", 1 );
			//控制信息
			}
			else
			{
				uint8 ctr_status[DATALENGTH];
				osal_memcpy(ctr_status,pMsg->msg.handleValueNoti.value,DATALENGTH);
				if(ctr_status[3] & 0x08)
				{
					//发命令开门灯1
					HalUARTWrite( 0 ,"m", 1 );
					ctr_data[3] |= 0x08;
					
				}
				else
				{
					//发命令关门灯1
					HalUARTWrite( 0 ,"M", 1 );
					ctr_data[3] &= ~0x08;
				}
				
				if(ctr_status[4] & 0x08)
				{
					//发命令开门灯2
					HalUARTWrite( 0 ,"n", 1 );
					ctr_data[4] |= 0x08;
				}
				else
				{
					//发命令关门灯2
					HalUARTWrite( 0 ,"N", 1 );
					ctr_data[4] &= ~0x08;
				}
				if(ctr_status[5] & 0x08)
				{
					//发命令开报警器
					HalUARTWrite( 0 ,"z", 1 );
					ctr_data[5] |= 0x08;
				}
				else
				{
					//发命令关报警器
					HalUARTWrite( 0 ,"Z", 1 );
					ctr_data[5] &= ~0x08;
				}
			}
			
		}
		
		if(pMsg->msg.handleValueNoti.value[0] == '*' &&pMsg->msg.handleValueNoti.value[1] == '8' &&pMsg->msg.handleValueNoti.value[2] == '1')
		{
			i++;
			HalUARTWrite( 0 ,"D", 1 );
			//温度信息
		}
		
//		if(i==0)
//		{
//			if(osal_memcmp("ANJUBAO_LINK",pMsg->msg.handleValueNoti.value,12)==TRUE)
//			{
//				//回应的数据正确表明,对方是腕带,可以进行其他操作
//			}
//			else
//			{
//				//conn_counter++;
//				
//				//终止连接
//				simpleBLEState = BLE_STATE_DISCONNECTING;
//				
//				GAPCentralRole_TerminateLink( simpleBLEConnHandle );
//				
//				
//				//GAPCentralRole_TerminateLink( simpleBLEConnHandle);//disconnecthandle );simpleBLEConnHandle = GAP_CONNHANDLE_INIT;
//				
//				//连接下一个
//				osal_start_timerEx( simpleBLETaskId, TRY_CONNECT_EVT,500); 
//				HalUARTWrite(0,"next\n",7 );	
//			}
//			
//		}
		
		
		
		
		/////////////////////////////////////////////////////////////////////////SLA11401_Uart_Send(&pMsg->msg.handleValueNoti.value[1],5);//osal_start_timerEx( simpleBLETaskId, SYSTEM_RESET_EVT,200);////?????// HalUARTWrite(0,&pMsg->msg.handleValueNoti.value[1],4);/*if(pMsg->msg.handleValueNoti.value[0]>=15){HalUARTWrite(0,&pMsg->msg.handleValueNoti.value[1],14 );HalUARTWrite(0,"...\n",4 );}else{HalUARTWrite(0,&pMsg->msg.handleValueNoti.value[1],pMsg->msg.handleValueNoti.value[0] );}*/}}
}

/*********************************************************************
 * @fnsimpleBLECentralRssiCB
 *
 * @briefRSSI callback.
 *
 * @paramconnHandle - connection handle
 * @paramrssi - RSSI
 *
 * @return  none
 */
static void simpleBLECentralRssiCB( uint16 connHandle, int8 rssi )
{LCD_WRITE_STRING_VALUE( "RSSI -dB:", (uint8) (-rssi), 10, HAL_LCD_LINE_1 );
}

/*********************************************************************
 * @fnsimpleBLECentralEventCB
 *
 * @briefCentral event callback function.
 *
 * @parampEvent - pointer to event structure
 *
 * @return  none
 */
static void simpleBLECentralEventCB( gapCentralRoleEvent_t *pEvent )
{switch ( pEvent->gap.opcode ){case GAP_DEVICE_INIT_DONE_EVENT:{
#ifdef DEBUGLCD_WRITE_STRING( "BLE Central", HAL_LCD_LINE_1 );LCD_WRITE_STRING( bdAddr2Str( pEvent->initDone.devAddr ),  HAL_LCD_LINE_2 );
#endif}break;case GAP_DEVICE_INFO_EVENT:
		{
			// if filtering device discovery results based on service UUID
			if ( DEFAULT_DEV_DISC_BY_SVC_UUID == TRUE )
			{
				//HalUARTWrite(0,"GAP_DEVICE_INFO_EVENT",22);
				if ( simpleBLEFindSvcUuid( SIMPLEPROFILE_SERV_UUID,pEvent->deviceInfo.pEvtData,pEvent->deviceInfo.dataLen ) )
				{
					simpleBLEAddDeviceInfo( pEvent->deviceInfo.addr, pEvent->deviceInfo.addrType );
				}
				
				HalUARTWrite(0,pEvent->deviceInfo.pEvtData,20 );
			}
		}break;case GAP_DEVICE_DISCOVERY_EVENT:{// discovery completesimpleBLEScanning = FALSE;// if not filtering device discovery results based on service UUIDif ( DEFAULT_DEV_DISC_BY_SVC_UUID == FALSE ){// Copy resultssimpleBLEScanRes = pEvent->discCmpl.numDevs;osal_memcpy( simpleBLEDevList, pEvent->discCmpl.pDevList,(sizeof( gapDevRec_t ) * pEvent->discCmpl.numDevs) );}if(simpleBLEScanRes==0){HalUARTWrite(0,"\nno_Devices_Found\n",30);}if ( simpleBLEScanRes > 0 ){
#ifdef DEBUG//LCD_WRITE_STRING( "<- To Select", HAL_LCD_LINE_2 );//连接第一个//HalLedSet(HAL_LED_3, HAL_LED_MODE_ON );//开LED3
#endifif ( simpleBLEState == BLE_STATE_IDLE ){simpleBLEScanIdx++;if ( simpleBLEScanIdx >= simpleBLEScanRes ){simpleBLEScanIdx = 0;}// connect to current device in scan result//////////////////////////用户编写/////////////////////////////////HalLedSet(HAL_LED_2, HAL_LED_MODE_ON );//开led2,表示连接成功HalUARTWrite(0,"\n111111111111",12 );//尝试连接 
			osal_start_timerEx( simpleBLETaskId, TRY_CONNECT_EVT,200);//////////////////////////用户编写//////////////////////////////}//HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF );}// initialize scan index to last devicesimpleBLEScanIdx = simpleBLEScanRes;}break;case GAP_LINK_ESTABLISHED_EVENT:{if ( pEvent->gap.hdr.status == SUCCESS ){simpleBLEState = BLE_STATE_CONNECTED;simpleBLEConnHandle = pEvent->linkCmpl.connectionHandle;simpleBLEProcedureInProgress = TRUE;////此处加入亮灯HalLedSet(HAL_LED_1, HAL_LED_MODE_ON );//led1与led2同时亮时表示连接成功// If service discovery not performed initiate service discoveryif ( simpleBLECharHdl == 0 ){osal_start_timerEx( simpleBLETaskId, START_DISCOVERY_EVT, DEFAULT_SVC_DISCOVERY_DELAY );
			HalUARTWrite(0,"simpleBLECharHd6",20);}osal_start_timerEx( simpleBLETaskId, ENABLE_NOTICE_EVT,200);}else{simpleBLEState = BLE_STATE_IDLE;simpleBLEConnHandle = GAP_CONNHANDLE_INIT;simpleBLERssi = FALSE;simpleBLEDiscState = BLE_DISC_STATE_IDLE;}}break;case GAP_LINK_TERMINATED_EVENT:{HalLedSet(HAL_LED_1, HAL_LED_MODE_OFF );	//led1灭表示连接失败,断开连接
		HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF );
		
		simpleBLEState = BLE_STATE_IDLE;simpleBLEConnHandle = GAP_CONNHANDLE_INIT;simpleBLERssi = FALSE;simpleBLEDiscState = BLE_DISC_STATE_IDLE;simpleBLECharHdl = 0;HalUARTWrite(0,"\nDisconnected\n",20);simpleBLEProcedureInProgress = FALSE;}break;case GAP_LINK_PARAM_UPDATE_EVENT:{
#ifdef  DEBUGLCD_WRITE_STRING( "Param Update", HAL_LCD_LINE_1 );
#endif}break;default:break;}
}

/*********************************************************************
 * @fnpairStateCB
 *
 * @briefPairing state callback.
 *
 * @return  none
 */
static void simpleBLECentralPairStateCB( uint16 connHandle, uint8 state, uint8 status )
{if ( state == GAPBOND_PAIRING_STATE_STARTED ){LCD_WRITE_STRING( "Pairing started", HAL_LCD_LINE_1 );}else if ( state == GAPBOND_PAIRING_STATE_COMPLETE ){if ( status == SUCCESS ){LCD_WRITE_STRING( "Pairing success", HAL_LCD_LINE_1 );}else{LCD_WRITE_STRING_VALUE( "Pairing fail", status, 10, HAL_LCD_LINE_1 );}}else if ( state == GAPBOND_PAIRING_STATE_BONDED ){if ( status == SUCCESS ){LCD_WRITE_STRING( "Bonding success", HAL_LCD_LINE_1 );}}
}

/*********************************************************************
 * @fnsimpleBLECentralPasscodeCB
 *
 * @briefPasscode callback.
 *
 * @return  none
 */
static void simpleBLECentralPasscodeCB( uint8 *deviceAddr, uint16 connectionHandle,uint8 uiInputs, uint8 uiOutputs )
{
#if (HAL_LCD == TRUE)uint32  passcode;uint8str[7];// Create random passcodeLL_Rand( ((uint8 *) &passcode), sizeof( uint32 ));passcode %= 1000000;// Display passcode to userif ( uiOutputs != 0 ){LCD_WRITE_STRING( "Passcode:",  HAL_LCD_LINE_1 );LCD_WRITE_STRING( (char *) _ltoa(passcode, str, 10),  HAL_LCD_LINE_2 );}// Send passcode responseGAPBondMgr_PasscodeRsp( connectionHandle, SUCCESS, passcode );
#endif
}

/*********************************************************************
 * @fnsimpleBLECentralStartDiscovery
 *
 * @briefStart service discovery.
 *
 * @return  none
 */
static void simpleBLECentralStartDiscovery( void )
{uint8 uuid[ATT_BT_UUID_SIZE] = { LO_UINT16(SIMPLEPROFILE_SERV_UUID),HI_UINT16(SIMPLEPROFILE_SERV_UUID) };// Initialize cached handlessimpleBLESvcStartHdl = simpleBLESvcEndHdl = simpleBLECharHdl = 0;simpleBLEDiscState = BLE_DISC_STATE_SVC;// Discovery simple BLE serviceGATT_DiscPrimaryServiceByUUID( simpleBLEConnHandle,uuid,ATT_BT_UUID_SIZE,simpleBLETaskId );
}

/*********************************************************************
 * @fnsimpleBLEGATTDiscoveryEvent
 *
 * @briefProcess GATT discovery event
 *
 * @return  none
 */
static void simpleBLEGATTDiscoveryEvent( gattMsgEvent_t *pMsg )
{attReadByTypeReq_t req;if ( simpleBLEDiscState == BLE_DISC_STATE_SVC ){// Service found, store handlesif ( pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP &&pMsg->msg.findByTypeValueRsp.numInfo > 0 ){simpleBLESvcStartHdl = pMsg->msg.findByTypeValueRsp.handlesInfo[0].handle;simpleBLESvcEndHdl = pMsg->msg.findByTypeValueRsp.handlesInfo[0].grpEndHandle;}// If procedure completeif ( ( pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP  &&pMsg->hdr.status == bleProcedureComplete ) ||( pMsg->method == ATT_ERROR_RSP ) ){if ( simpleBLESvcStartHdl != 0 ){// Discover characteristicsimpleBLEDiscState = BLE_DISC_STATE_CHAR;req.startHandle = simpleBLESvcStartHdl;req.endHandle = simpleBLESvcEndHdl;req.type.len = ATT_BT_UUID_SIZE;req.type.uuid[0] = LO_UINT16(SIMPLEPROFILE_CHAR6_UUID);req.type.uuid[1] = HI_UINT16(SIMPLEPROFILE_CHAR6_UUID);GATT_ReadUsingCharUUID( simpleBLEConnHandle, &req, simpleBLETaskId );}}}else if ( simpleBLEDiscState == BLE_DISC_STATE_CHAR ){// Characteristic found, store handleif ( pMsg->method == ATT_READ_BY_TYPE_RSP &&pMsg->msg.readByTypeRsp.numPairs > 0 ){simpleBLECharHdl = BUILD_UINT16( pMsg->msg.readByTypeRsp.dataList[0],pMsg->msg.readByTypeRsp.dataList[1] );HalUARTWrite(0,"find char6",20);simpleBLEProcedureInProgress = FALSE;}simpleBLEDiscState = BLE_DISC_STATE_IDLE;}
}
Yan:

yongcai,

建议你用CC2540 dongle烧入sniffer 空中抓包看看。

这种情况下最好是空中先看看你的交互正常不正常。

yongcai zhang:

回复 Yan:

本人学蓝牙不久,还望大家多多指点

我向往特征值6里面写数据应该设置哪些呢?

if ( simpleBLESvcStartHdl != 0 ) { // Discover characteristic simpleBLEDiscState = BLE_DISC_STATE_CHAR; req.startHandle = simpleBLESvcStartHdl; req.endHandle = simpleBLESvcEndHdl; req.type.len = ATT_BT_UUID_SIZE; req.type.uuid[0] = LO_UINT16(SIMPLEPROFILE_CHAR1_UUID); req.type.uuid[1] = HI_UINT16(SIMPLEPROFILE_CHAR1_UUID);

SIMPLEPROFILE_CHAR1_UUID这个值我改成SIMPLEPROFILE_CHAR6_UUID是不是就表示我往特征值6里些东西呀

Yan:

回复 yongcai zhang:

yongcai,

你这个是查找你的char 6 的特征值,不是往里面写数据。

默认的simpleBLEPeripheral 并没有定义到char 6。首先你得确定你自己的工程里面正确添加了这个特征值。

正确的往里面写入值的方法可以参考SimpleBLECentral.c 中的simpleBLECentral_HandleKeys() 函数,里面有一段如下:

attWriteReq_t req; req.pValue = GATT_bm_alloc( simpleBLEConnHandle, ATT_WRITE_REQ, 1, NULL ); if ( req.pValue != NULL ) { req.handle = simpleBLECharHdl; req.len = 1; req.pValue[0] = simpleBLECharVal; req.sig = 0; req.cmd = 0; status = GATT_WriteCharValue( simpleBLEConnHandle, &req, simpleBLETaskId ); if ( status != SUCCESS ) { GATT_bm_free( (gattMsg_t *)&req, ATT_WRITE_REQ ); }

yongcai zhang:

回复 Yan:

req.pValue = GATT_bm_alloc( simpleBLEConnHandle, ATT_WRITE_REQ, 1, NULL );是什么含义呢,那我怎么确定我是往特征6里面写数据呢???

我已经添加了特征值6得uuid

if ( simpleBLESvcStartHdl != 0 ){// Discover characteristicsimpleBLEDiscState = BLE_DISC_STATE_CHAR;req.startHandle = simpleBLESvcStartHdl;req.endHandle = simpleBLESvcEndHdl;req.type.len = ATT_BT_UUID_SIZE;req.type.uuid[0] = LO_UINT16(SIMPLEPROFILE_CHAR6_UUID);//更改为char6req.type.uuid[1] = HI_UINT16(SIMPLEPROFILE_CHAR6_UUID);//更改为char6

然后还有设置哪些参数才可以执行写特征值6呢???

yongcai zhang:

回复 Yan:

GATT_bm_alloc( simpleBLEConnHandle, ATT_WRITE_REQ, 1, NULL );我在central.c里面没有找到这个函数

Yan:

回复 yongcai zhang:

yongcai,

这个是最新的SDK,1.4.1,你可以在TI的官网上下载到。在simpleBLECentral.c中,不是在central.c 中。定义在gatt.h中。

这个是分配给ATT_WRITE_REQ命令分配空间。

这里的参数1, 就代表一个字节。建议你看一下GATT_bm_alloc 的定义。

yongcai zhang:

回复 Yan:

我的还是1.2.1 大部分工作都已经做完了 就是差主机给从机发数据了    

麻烦问下这个版本的怎么弄呢

顺便问下  两个不同的蓝牙芯片如何通信呢。一个是ti的 一个nxp的 

Yan:

回复 yongcai zhang:

yongcai,

建议更新到最新的SDK。

老的SDK一样的弄法。你可以在simpleBLECentral.c 中找到simpleBLECentral_HandleKeys() 函数,里面有如下代码:这是对char 3 的写操作,你可以完全拷贝。

uint8 status; // Do a read or write as long as no other read or write is in progress if ( simpleBLEDoWrite ) { // Do a write attWriteReq_t req; req.handle = simpleBLECharHdl; req.len = 1; req.value[0] = simpleBLECharVal; req.sig = 0; req.cmd = 0; status = GATT_WriteCharValue( simpleBLEConnHandle, &req, simpleBLETaskId );

另外,不同厂家芯片的通信,大同小异。只要都符合BLE标准,然后Profile写的两边都认识,就没有问题。

yongcai zhang:

回复 Yan:

你好,那我怎么区分是在char3,还是char2呢 主要是我想发送多个字符

求指导

yongcai zhang:

回复 Yan:

那我怎么区分是char3,还是char1呢

主要是这几个特征值都是单字节的,我想发送多个字符

所以得请教下

赞(0)
未经允许不得转载:TI中文支持网 » 蓝牙通信异常,可以连接不能通信
分享到: 更多 (0)