This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Connecting device automatically when switched on

Other Parts Discussed in Thread: CC2650, CC2650EM-5XD-RD

I have two sensor tag's and i want that when they are powered on they should get connected to each other automatically with out any key press.
To get notification i had made that when they will get connected  a LED will glow as a notification.

I had made #define DEFAULT_DEV_DISC_BY_SVC_UUID          TRUE So that it doesn't search for any other Device and Filter it by UUID.
ANy help will be Great.

My Code For connection is:

static void simpleTopology_processRoleEvent(gapMultiRoleEvent_t *pEvent)
{
switch (pEvent->gap.opcode)
{
case GAP_DEVICE_INIT_DONE_EVENT:
{
maxPduSize = pEvent->initDone.dataPktLen;
LCD_WRITE_STRING("Connected to 0", LCD_PAGE0);
LCD_WRITE_STRING(Util_convertBdAddr2Str(pEvent->initDone.devAddr),
LCD_PAGE1);
LCD_WRITE_STRING("Initialized", LCD_PAGE2);

DevInfo_SetParameter(DEVINFO_SYSTEM_ID, DEVINFO_SYSTEM_ID_LEN, pEvent->initDone.devAddr);
}
break;

case GAP_MAKE_DISCOVERABLE_DONE_EVENT:
{
if (gapRoleNumLinks(GAPROLE_ACTIVE_LINKS) > 0)
{
LCD_WRITE_STRING("Advertising", LCD_PAGE2);
}
else
{
LCD_WRITE_STRING("Advertising", LCD_PAGE2);
}
}
break;

case GAP_END_DISCOVERABLE_DONE_EVENT:
{
if (gapRoleNumLinks(GAPROLE_AVAILABLE_LINKS) > 0)
{
LCD_WRITE_STRING("Ready to Advertise", LCD_PAGE2);
}
else
{
LCD_WRITE_STRING("Can't Adv : No links", LCD_PAGE2);
}
}
break;

case GAP_DEVICE_INFO_EVENT:
{
// if filtering device discovery results based on service UUID
if (DEFAULT_DEV_DISC_BY_SVC_UUID == TRUE)
{
if (simpleTopology_findSvcUuid(SIMPLEPROFILE_SERV_UUID,
pEvent->deviceInfo.pEvtData,
pEvent->deviceInfo.dataLen))
{
simpleTopology_addDeviceInfo(pEvent->deviceInfo.addr,
pEvent->deviceInfo.addrType);
}
}
}
break;

case GAP_DEVICE_DISCOVERY_EVENT:
{
// discovery complete
scanningStarted = FALSE;

// if not filtering device discovery results based on service UUID
if (DEFAULT_DEV_DISC_BY_SVC_UUID == FALSE)
{
// Copy results
scanRes = pEvent->discCmpl.numDevs;
memcpy(devList, pEvent->discCmpl.pDevList,
(sizeof(gapDevRec_t) * scanRes));
}
LCD_WRITE_STRING_VALUE("Devices Found", scanRes, 10, LCD_PAGE3);

if (scanRes > 0)
{
LCD_WRITE_STRING("<- To Select", LCD_PAGE4);
}

// initialize scan index to last device
scanIdx = scanRes;
}
break;

case GAP_LINK_ESTABLISHED_EVENT:
{
if (pEvent->gap.hdr.status == SUCCESS)
{
LCD_WRITE_STRING("Connected!", LCD_PAGE3);
PIN_setOutputValue(hGpioPin, Board_LED1, Board_LED_ON);
LCD_WRITE_STRING_VALUE("Connected to ", gapRoleNumLinks(GAPROLE_ACTIVE_LINKS) ,10, LCD_PAGE0);

//update state
connecting_state = 0;
//store connection handle
connHandle = pEvent->linkCmpl.connectionHandle;

//if we're not advertising, attempt to turn advertising back on
uint8_t adv;
GAPRole_GetParameter(GAPROLE_ADVERT_ENABLED, &adv, NULL);
if (adv == 1) //connected and advertising
{
if (gapRoleNumLinks(GAPROLE_AVAILABLE_LINKS) > 0)
{
LCD_WRITE_STRING("Advertising", LCD_PAGE2);
}
else //no available links
{
LCD_WRITE_STRING("Can't adv: no links", LCD_PAGE2);
}
}
else //not currently advertising
{
LCD_WRITE_STRING("Ready to Advertise", LCD_PAGE2);
//attempt to turn advertising back o
uint8_t advertEnabled = TRUE;
uint8_t stat = GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &advertEnabled, NULL);
if (stat == bleNoResources) //no more links
{
LCD_WRITE_STRING("Can't adv: no links", LCD_PAGE2);
}
else if (stat == SUCCESS) //turning advertising back on
{
LCD_WRITE_STRING("Advertising", LCD_PAGE2);
}
else
{
while(1);
}
}

// Print last connected device
LCD_WRITE_STRING("", LCD_PAGE4);
LCD_WRITE_STRING(Util_convertBdAddr2Str(pEvent->linkCmpl.devAddr), LCD_PAGE5 );

// initiate service discovery
Util_startClock(&startDiscClock);
}
else
{
connHandle = GAP_CONNHANDLE_INIT;
discState = BLE_DISC_STATE_IDLE;

LCD_WRITE_STRING("Connect Failed", LCD_PAGE4);
LCD_WRITE_STRING_VALUE("Reason:", pEvent->gap.hdr.status, 10,
LCD_PAGE3);
}
}
break;

case GAP_LINK_TERMINATED_EVENT:
{
connHandle = GAP_CONNHANDLE_INIT;
discState = BLE_DISC_STATE_IDLE;

uint8_t i;
for (i=0; i < MAX_NUM_BLE_CONNS; i++)
{
if (multiConnInfo[i].gapRole_ConnectionHandle == GAPROLE_CONN_JUST_TERMINATED)
{
//clear screen, reset discovery info, and return to main menu
multiConnInfo[i].gapRole_ConnectionHandle = INVALID_CONNHANDLE;
charHdl[i] = 0;
LCD_WRITE_STRING_VALUE("Connected to ", gapRoleNumLinks(GAPROLE_ACTIVE_LINKS) ,10, LCD_PAGE0);
LCD_WRITE_STRING("Disconnected!", LCD_PAGE5);
PIN_setOutputValue(hGpioPin, Board_LED1, Board_LED_OFF);
LCD_WRITE_STRING("Main Menu", LCD_PAGE3);
LCDmenu = MAIN_MENU;
}
if ((gapRoleNumLinks(GAPROLE_ACTIVE_LINKS) == (MAX_NUM_BLE_CONNS-1))) //now we can advertise again
{
LCD_WRITE_STRING("Ready to Advertise", LCD_PAGE2);
}
}
}
break;

case GAP_LINK_PARAM_UPDATE_EVENT:
{
LCD_WRITE_STRING_VALUE("Param Update:", pEvent->linkUpdate.status,
10, LCD_PAGE2);
}
break;

default:
break;
}
}



Thanks

  • Hello Pradeep,

    It is easy to set up your central to automatically connect to a peripheral with a known address. Simply enable the white list (set DEFAULT_DISCOVERY_WHITE_LIST and DEFAULT_LINK_WHITE_LIST to true)  and add the peers device address to it (use HCI_LE_AddWhiteListCmd in the init function). Then use the folowing code in your GAP_DEVICE_INFO_EVENT case:

       case GAP_DEVICE_INFO_EVENT:
         {
           // if filtering device discovery results based on service UUID
           if (DEFAULT_DEV_DISC_BY_SVC_UUID == TRUE)
           {
             if (SimpleBLECentral_findSvcUuid(SIMPLEPROFILE_SERV_UUID,
                                              pEvent->deviceInfo.pEvtData,
                                              pEvent->deviceInfo.dataLen))
             {
               SimpleBLECentral_addDeviceInfo(pEvent->deviceInfo.addr,
                                              pEvent->deviceInfo.addrType);
             }
           }
           // Cancel discovery and connect right away.
           if(!(GAPCentralRole_CancelDiscovery())) {
               // Initiate connection to peer from white list
               GAPCentralRole_EstablishLink(DEFAULT_LINK_HIGH_DUTY_CYCLE,
                                            DEFAULT_LINK_WHITE_LIST,
                                            pEvent->deviceInfo.addrType, pEvent->deviceInfo.addr);
    
           }
         }
         break;

  • Hey Eirik,
    I tried this Method but the problem is its still not searching without a button press.I have to press a button to start the search which does not solve the issue.

    And is this correct way to implement the white List?
    uint8_t bdAddr[][6]={0xB0,0xB4,0x48,0xBF,0x4B,0x82};
    HCI_LE_AddWhiteListCmd(HCI_PUBLIC_DEVICE_ADDRESS, &bdAddr[0][0]);

    As after implementation of this code the device has stop searching the above Bluetooth address Device also.
  • Hello Pradeep,
    Start the discovery process (GAPCentralRole_StartDiscovery) in case GAP_DEVICE_INIT_DONE_EVENT in SimpleBLECentral_processRoleEvent.

    Add the address to the whitelist like this:
    // Add peer device to white list
    uint8 bdPeerAddress[B_ADDR_LEN] = { 0xB0, 0xA0, 0xD0, 0xA0, 0x50, 0x50 };
    HCI_LE_ClearWhiteListCmd();
    HCI_LE_AddWhiteListCmd(ADDRTYPE_PUBLIC,bdPeerAddress);
  • Hey Eirik,

    I tired using this method but no device is getting discovered after doing this.
  • Hello Pradeep,
    Have you set the wanted address on your peer device so that it matches the entry in the white list?
    Do you have a Bluetooth sniffer available to verify if your peer device is advertising with the correct address.
  • Hey EIrik,

    Thank You Very much for the Help.
    now i am able to connect devices without pressing a button but How i can just connect to some Particular devices?
    when i try the Method of white list no device can be found.
  • Hey, great.
    Remember to set the define DEFAULT_LINK_WHITE_LIST to true and makes sure the advertiser address match the one you enter into the white list. A common mistake can be to enter the address in the wrong byte order. If this is the case you can test with a pattern that equal both ways, for example 0xAA 0X11....0x11 0xAA. or for example
    uint8 bdAddress[B_ADDR_LEN] = { 0x88, 0x88, 0x88, 0x88, 0x88, 0x88 };
    HCI_EXT_SetBDADDRCmd(bdAddress);
  • Hi Eirik,

    I have a problem. I did so as stated earlier in this article.

    #define DEFAULT_DISCOVERY_ACTIVE_SCAN         TRUE

    // TRUE to use white list during discovery

    #define DEFAULT_DISCOVERY_WHITE_LIST          TRUE

    // TRUE to use high scan duty cycle when creating link

    #define DEFAULT_LINK_HIGH_DUTY_CYCLE          FALSE

    // TRUE to use white list when creating link

    #define DEFAULT_LINK_WHITE_LIST               TRUE

     

    in SimpleBLECentral_processRoleEventI() added code:

    case GAP_DEVICE_INFO_EVENT:
    {
    // if filtering device discovery results based on service UUID
    if (DEFAULT_DEV_DISC_BY_SVC_UUID == TRUE)
    {
    if (SimpleBLECentral_findSvcUuid(SIMPLEPROFILE_SERV_UUID,
    pEvent->deviceInfo.pEvtData,
    pEvent->deviceInfo.dataLen))
    {
    SimpleBLECentral_addDeviceInfo(pEvent->deviceInfo.addr,
    pEvent->deviceInfo.addrType);
    }
    }
    if (!(GAPCentralRole_CancelDiscovery())) {

    GAPCentralRole_EstablishLink(DEFAULT_LINK_HIGH_DUTY_CYCLE,
    DEFAULT_LINK_WHITE_LIST,
    pEvent->deviceInfo.addrType, pEvent->deviceInfo.addr);
    }
    }
    break;

    and 

    case GAP_DEVICE_INIT_DONE_EVENT:
    {
    maxPduSize = pEvent->initDone.dataPktLen;

    Display_print0(dispHandle, 1, 0, Util_convertBdAddr2Str(pEvent->initDone.devAddr));
    Display_print0(dispHandle, 2, 0, "Initialized");
    /////////////////////////////////////////////////////////////////////////////////////////////////////////
    GAPCentralRole_StartDiscovery(DEFAULT_DISCOVERY_MODE,
    DEFAULT_DISCOVERY_ACTIVE_SCAN,
    DEFAULT_DISCOVERY_WHITE_LIST);

    /////////////////////////////////////////////////////////////////////////////////////////////////////////
    }
    break;

    After that I added code:

    in SimpleBLECentral_init()

    uint8 bdPeerAddress[B_ADDR_LEN] = { 0xB0, 0xB4, 0x48, 0xBD, 0x22, 0x04 };
    HCI_LE_ClearWhiteListCmd();
    HCI_LE_AddWhiteListCmd(ADDRTYPE_PUBLIC,bdPeerAddress);

     

    But the event 0x0D (GAP_DEVICE_INFO_EVENT) does not occur.

    Please tell me what I did wrong?

    I use CC2650EM-5XD-RD as simple_central_cc2650em and cc2650 launhPad as Simple_peripheral (address hex:0x0422BD000048B4B0) B0:B4:48:BD:22:04

    Thanks in advance,

    Arkadii.simple_central.rar

  • Hi Arkadii,

    It sounds like your peripheral device doesn't get discovered. I'm not sure why, maybe it would be easier to debug if you set your central to discover all devices (#define DEFAULT_DISCOVERY_WHITE_LIST FALSE) and then only connect to the device in the white list? (#define DEFAULT_LINK_WHITE_LIST TRUE)

    Also, I don't know where you got this from: "Simple_peripheral (address hex:0x0422BD000048B4B0) ", so maybe you should try giving the address bytes in reverse order just to be sure?
    uint8 bdPeerAddress[B_ADDR_LEN] = { 0x04, 0x22, 0xBD, 0x48, 0xB4, 0xB0 };
  • Hi Marie,
    Thank you for your help.
    Everything works perfectly.
    The error was in the address.
    With respect,
    Arkadii