This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

S130 connections

hi I use S130 from nRF5_SDK_11.0.0_89a8197 as an ble central base on ble_app_uart_c example. when the ble_peripheral change the adv's content(means have data to send),the central connected it, then peripheral send a packet data, after the central got the data,disconnect the peripheral immediately.

I run this for a long time, then the connection never happen again,this is my code:

#ifdef S210
#define NRF_CLOCK_LFCLKSRC      NRF_CLOCK_LFCLKSRC_XTAL_20_PPM
#else
//8second Calibration timer interval in 1/4 second(units) 32*(1/4)=8second
//1: Only calibrate if the temperature has changed (nRF51 only).
#define NRF_CLOCK_LFCLKSRC      {.source        = NRF_CLOCK_LF_SRC_RC,            \
                                 .rc_ctiv       = 32,                                \
                                 .rc_temp_ctiv  = 0,                                \
                                 .xtal_accuracy = 0}
#endif

//ble stack parametres setting
#define CENTRAL_LINK_COUNT      					1 
#define PERIPHERAL_LINK_COUNT   				0


uint32_t ble_nus_c_init(ble_nus_c_t * p_ble_nus_c, ble_nus_c_init_t * p_ble_nus_c_init)
{
    uint32_t      err_code;
    ble_uuid_t    uart_uuid;
    ble_uuid128_t nus_base_uuid = NUS_BASE_UUID;
        
    VERIFY_PARAM_NOT_NULL(p_ble_nus_c);
    VERIFY_PARAM_NOT_NULL(p_ble_nus_c_init);
    
    err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &p_ble_nus_c->uuid_type);
    VERIFY_SUCCESS(err_code);
    
    uart_uuid.type = p_ble_nus_c->uuid_type;
	//uart_uuid.type = BLE_UUID_TYPE_BLE;
    uart_uuid.uuid = BLE_UUID_NUS_SERVICE;
    
    p_ble_nus_c->conn_handle           = BLE_CONN_HANDLE_INVALID;
    p_ble_nus_c->evt_handler           = p_ble_nus_c_init->evt_handler;
    p_ble_nus_c->handles.nus_rx_handle = BLE_GATT_HANDLE_INVALID;
	p_ble_nus_c->handles.nus_rx_cccd_handle = BLE_GATT_HANDLE_INVALID;
    
    return ble_db_discovery_evt_register(&uart_uuid);
}


/*==================================================================
* Function	    : on_ble_evt
* Description	: handling the Application's BLE Stack events
* Input Para	: 
* Output Para	: 
* Return Value  : 
==================================================================*/
static void on_ble_evt(ble_evt_t *p_ble_evt)
{
	 uint32_t              err_code;
	 static unsigned char old_adv_flag=0x30;
    const ble_gap_evt_t * p_gap_evt = &p_ble_evt->evt.gap_evt;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_ADV_REPORT:
		{
            const ble_gap_evt_adv_report_t * p_adv_report = &p_gap_evt->params.adv_report;
			//gprs connected and login the server
			if( (gtGprsAT.geGrps_State==GPRS_STATE_CONNT) && (gtProtocol.geLoginState==Login_Succeed) ) 
			{
				//compare the mac is the storage one c5f9dd489120
//				if( (p_adv_report->peer_addr.addr[0]==0x5F) && (p_adv_report->peer_addr.addr[1]==0x60) && (p_adv_report->peer_addr.addr[2]==0xE0) \
//					&& (p_adv_report->peer_addr.addr[3]==0x48) && (p_adv_report->peer_addr.addr[4]==0x91) && (p_adv_report->peer_addr.addr[5]==0x20) )
				if( memcmp(p_adv_report->peer_addr.addr,mac,sizeof(mac) ) == 0 )
				{
					
					if( (old_adv_flag==0x30) && (p_adv_report->data[17]==0x31) ) //get the adv's dat flag==1
					{
						err_code = sd_ble_gap_connect(&p_adv_report->peer_addr,&m_scan_params,&m_connection_param);
						
						if (err_code == NRF_SUCCESS)
						{
							// scan is automatically stopped by the connect
							err_code = bsp_indication_set(BSP_INDICATE_IDLE);
							APP_ERROR_CHECK(err_code);
							#if (USED_DEBUGE_MODE==1)
							APPL_LOG("Connecting to target %02x%02x%02x%02x%02x%02x\r\n",
									 p_adv_report->peer_addr.addr[0],
									 p_adv_report->peer_addr.addr[1],
									 p_adv_report->peer_addr.addr[2],
									 p_adv_report->peer_addr.addr[3],
									 p_adv_report->peer_addr.addr[4],
									 p_adv_report->peer_addr.addr[5]
									 );
							#endif
						}
						else
						{
							APPL_LOG("[APPL]: connect1 error ---sunil.\r\n");
							//ble_stack_init(); //change m_ble_nus_c's value
							//nus_c_init();
							// Start scanning for peripherals and initiate connection
							// with devices that advertise NUS UUID.
							//scan_start();
						}
					}
					if(p_adv_report->data[17]!=0)
						old_adv_flag = p_adv_report->data[17];
				}
			}
            break;	
    	}
			
        case BLE_GAP_EVT_CONNECTED:
			#if (USED_DEBUGE_MODE==1)
            APPL_LOG("Connected to target\r\n");
			#endif
            err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
			if(err_code != NRF_SUCCESS)
			{
				APPL_LOG("[APPL]: connect2 error ---sunil.\r\n");
				//ble_stack_init(); //change m_ble_nus_c's value
				//nus_c_init();
				// Start scanning for peripherals and initiate connection
				// with devices that advertise NUS UUID.
				//scan_start();
			}
            //APP_ERROR_CHECK(err_code);

            // start discovery of services. The NUS Client waits for a discovery result
            err_code = ble_db_discovery_start(&m_ble_db_discovery, p_ble_evt->evt.gap_evt.conn_handle);
			if(err_code != NRF_SUCCESS)
			{
				APPL_LOG("[APPL]: connect3 error ---sunil.\r\n");
				//ble_stack_init(); //change m_ble_nus_c's value
				//nus_c_init();
				// Start scanning for peripherals and initiate connection
				// with devices that advertise NUS UUID.
				//scan_start();
			}
            //APP_ERROR_CHECK(err_code);
            break;
			
		case BLE_GAP_EVT_DISCONNECTED:
			//scan_start();
			break;
			
        case BLE_GAP_EVT_TIMEOUT:
            if (p_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_SCAN)
            {
            	#if (USED_DEBUGE_MODE==1)
                APPL_LOG("[APPL]: Scan timed out.\r\n");
				#endif
                scan_start();
            }
            else if (p_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_CONN)
            {
            	#if ( USED_DEBUGE_MODE==1 )
                APPL_LOG("[APPL]: Connection Request timed out.\r\n");
				#endif
            }
            break;

        case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            // Pairing not supported
            err_code = sd_ble_gap_sec_params_reply(p_ble_evt->evt.gap_evt.conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
            // Accepting parameters requested by peer.
            err_code = sd_ble_gap_conn_param_update(p_gap_evt->conn_handle,
                                                    &p_gap_evt->params.conn_param_update_request.conn_params);
            APP_ERROR_CHECK(err_code);
            break;
    
        default:
            break;
    }
}

/**@brief Callback handling NUS Client events.
 *
 * @details This function is called to notify the application of NUS client events.
 *
 * @param[in]   p_ble_nus_c   NUS Client Handle. This identifies the NUS client
 * @param[in]   p_ble_nus_evt Pointer to the NUS Client event.
 */

/**@snippet [Handling events from the ble_nus_c module] */ 
void ble_nus_c_evt_handler(ble_nus_c_t * p_ble_nus_c, const ble_nus_c_evt_t * p_ble_nus_evt)
{
    uint32_t err_code;
	static unsigned char datbuf[20];
	uint8_t i = 0,j=0;
    switch (p_ble_nus_evt->evt_type)
    {
        case BLE_NUS_C_EVT_DISCOVERY_COMPLETE:
            err_code = ble_nus_c_handles_assign(p_ble_nus_c, p_ble_nus_evt->conn_handle, &p_ble_nus_evt->handles);
            APP_ERROR_CHECK(err_code);

            err_code = ble_nus_c_rx_notif_enable(p_ble_nus_c);
            APP_ERROR_CHECK(err_code);
			#if (USED_DEBUGE_MODE==1)
            APPL_LOG("The device has the Nordic UART Service\r\n");
			#endif
            break;
        
        case BLE_NUS_C_EVT_NUS_RX_EVT:
			//gprs connected and login the server
			if( (gtGprsAT.geGrps_State==GPRS_STATE_CONNT) && (gtProtocol.geLoginState==Login_Succeed) ) 
			{
				for (; i < p_ble_nus_evt->data_len; i++)
				{
					datbuf[i]=p_ble_nus_evt->p_data[i];
				}

				if( i==p_ble_nus_evt->data_len )
				{
					if( strstr("AT",(const char *)datbuf) == NULL) //not get the "AT"
					{
						//while( (app_uart_put( datbuf[p_ble_nus_evt->data_len-i]) != NRF_SUCCESS) && (i-->0));
						app_uart_put(0XEE);
						app_uart_put(0XEE);
						for( ; j<p_ble_nus_evt->data_len;j++)
						{
							while( app_uart_put( p_ble_nus_evt->p_data[j] ) != NRF_SUCCESS) ;
						}
						app_uart_put(0XFF);
						app_uart_put(0XFF);
						
						gtProtocol.eBTDatState = GOT_DAT;
						memcpy(gtProtocol.btdat,p_ble_nus_evt->p_data,p_ble_nus_evt->data_len);
						
						//disconnected when received valid dat
						err_code = sd_ble_gap_disconnect(m_ble_nus_c.conn_handle,BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
						//err_code = sd_ble_gap_disconnect(BLE_CONN_HANDLE_ALL,BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
						if (err_code != NRF_ERROR_INVALID_STATE)
							APP_ERROR_CHECK(err_code);
					}
				}
			}
            break;
        
        case BLE_NUS_C_EVT_DISCONNECTED:
			#if (USED_DEBUGE_MODE==1)
            APPL_LOG("Disconnected\r\n");
			#endif
            scan_start();
            break;
    }
}
Parents Reply Children
No Data
Related