11 Replies Latest reply on Nov 3, 2017 12:25 AM by thejus_2543151

    How to detect authentified Central in range?

    javiersenas_1605611

      Hello colleagues,

         

      I set the GAP Role of my BLE device as Peripheral and Bonding Yes.  When I connect for first time my PRoC to the Smartphone (Central) through CySmart App I can see all these events:

         

      CYBLE_EVT_STACK_ON
      CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP
      CYBLE_EVT_GATT_CONNECT_IND

         

      CYBLE_EVT_GAP_DEVICE_CONNECTED

         

      CYBLE_EVT_GAP_AUTH_REQ

         

      CYBLE_EVT_GAP_ENCRYPT_CHANGE

         

      KEYINFO_EXCHANGE_COMPLETED. Here I can read the Long term Key: 10006aceca77f24d8755b800a0507258

         

      AUTHENTICATION_COMPLETED

         

      When I connect it for second time, the Authentification messages dissapear. What I would like is to know how I can know if one already-authentified Central is in range of my PRoC. I was thinking of reading SCAN_REQ Message and then compare its Long Term Key but I don't find such Event. How can I do this?

         

      Thanks in advance for the great support!

         

      Peter

        • 1. Re: How to detect authentified Central in range?
          anpm

          Hi Peter,

             

          Need some clarification.

             

          When I connect it for second time, the Authentification messages disappear. - which message is disappearing?

             

          You have kept bonding - yes, but did you called CyBle_StoreBondingData() API for store bonding? I suggest you to go through our Bonding example project for better understanding. https://github.com/cypresssemiconductorco/PSoC-4-BLE/tree/master/100_Projects_in_100_Days/Day015_Bonding

             

          What I would like is to know how I can know if one already-authentified Central is in range of my PRoC. - Once after connecting and bonding, you can keep whitelist only that particular device. Please refer the following project  :https://github.com/cypresssemiconductorco/PSoC-4-BLE/tree/master/100_Projects_in_100_Days/Day014_Whitelist

             

          Thanks,
          Anjana

          • 2. Re: How to detect authentified Central in range?
            javiersenas_1605611

            Hello Anjana,

               

            Thanks for your kind comments and examples. They help me but however don't solve my problem totally... 

               

            - With the command CyBle_GapAddDeviceToWhiteList, I can avoid scan requests from devices out of this list. However, what I need is to know when a scan request message (SCAN_REQ) of such whitelist arrives (this would mean the user of the whitelist is in range). Is there any BLE Event to know when a user of the whitelist is in range (by checking for example SCAN_REQ message)?

               

            Thanks in advance,

               

            Peter

            • 3. Re: How to detect authentified Central in range?
              anpm

              Hi Peter,

                 

              Upon receiving a scan request from a Central, peripheral will trigger an event CYBLE_EVT_GAP_SCAN_REQ_RECVD (go through BLE component datasheet for details). But it will not tell you which central send the scan request. But if you have only one Central and it is added to whitelist , and keep the filter policy as scan request whitelist. This may help you.

                 

              There is no other way to do the same.

                 

              Thanks & Regards,
              Anjana

              • 4. Re: How to detect authentified Central in range?
                javiersenas_1605611

                Hi Anjana, thanks for the answer. This was what I was looking for!! And it is working perfectly.

                   

                I can not select the filter policy as scan request whitelist because somebody else (another central) might want to authentify so I need to check is the Central is in the Whitelist already. 

                   

                How can I read the BD Address in the scan_req message? Knowing this I can compare it with all the devices in the whitelist using GapGetDevicesFromWhiteList. There must be a way to know the DB Address in the scan_req message as you need this information in the stack when selecting filter policy scan request whitelist.

                   

                Thanks in advance,

                   

                Peter

                • 5. Re: How to detect authentified Central in range?
                  javiersenas_1605611

                  Hi Anjana,

                     

                  I found out I can change the filter policy dynamically in code so it should solve my issue. The idea is:

                     

                  - Scan/Connect Request Any in *.cysch

                     

                  - When CYBLE_EVT_GAP_DEVICE_CONNECTED event happens, I get my Peer Bd Address calling CyBle_GapGetPeerBdAddr and add it to the Whitelist with the command CyBle_GapAddDeviceToWhiteList.

                     

                  - I change the FilterPolicy in CYBLE_EVT_GAP_DEVICE_DISCONNECTED event to CYBLE_GAPP_SCAN_CONN_WHITELIST_ONLY. When I disconnect I can see return value is ERROR_OK, so it should be added correctly to the WhiteList.

                     

                  - I can see my Peer BD Address when calling CyBle_GapGetDevicesFromWhiteList in CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP. So everything looks fine.

                     

                  Looking at my Smartphone Bluetooth Address, it doesn't match the returned peer Bluetooth Address (surrounded in red in the log.png). Am I reading out incorrectly the Peer Bluetooth Address? Or in a incorrect place? Is there any any other function to read out the Bluetooth Address of my peer device? Or is it maybe because I am not reading out the static/random public address?

                     

                  Attached you can see the log and the source code. 

                     

                  Peter

                  • 6. Re: How to detect authentified Central in range?
                    yssu

                    Peter,

                       

                    Most mobile phones use, what is called, Resolvable Private Address or RPA. RPA is derived from an IRK; new address is generated over time. This is the reason you are seeing different addresses.

                    • 7. Re: How to detect authentified Central in range?
                      javiersenas_1605611
                              How can I read the real Bluetooth Address?   
                      • 8. Re: How to detect authentified Central in range?
                        Gyan Chand

                        Hi Peter,

                           

                            You can access the real address( ID address ) by calling CyBle_GapGetDevSecurityKeyInfo( uint8 *keyFlags,
                        CYBLE_GAP_SMP_KEY_DIST_T *keys) API. The parameter 'keys' is a pointer to a structure of type CYBLE_GAP_SMP_KEY_DIST_T which contains the real address.

                           

                         

                           

                        Regards,

                           

                        Gyan

                        • 9. Re: How to detect authentified Central in range?
                          Gyan Chand

                          Hi Peter,

                             

                          For peer device use below  API instead of mentioned in the previous post.

                             

                          CyBle_GapGetPeerDevSecurityKeyInfo (uint8 bdHandle, uint8 *keysFlag,
                          CYBLE_GAP_SMP_KEY_DIST_T *keyInfo)

                             

                          Thanks.

                          • 10. Re: How to detect authentified Central in range?
                            javiersenas_1605611

                            Hi Gyan,

                               

                            Thanks for your comment. However, it is not working, I only see 00000000 as return. I have even used your Authentication project from Github https://github.com/cypresssemiconductorco/PSoC-4-BLE/tree/master/100_Projects_in_100_Days/Day016_Authentication and I get the same result. See attached the UART Screenshot. Is there any possibility to get a correct Bluetooth Address? Should I maybe configure my Android Smartphone in another way? I assume as your FilterPolicy is working fine, you should have a way to read correctly the Bluetooth Address.

                               

                            Thanks, Peter

                            • 11. Re: How to detect authentified Central in range?
                              thejus_2543151

                              Hi,

                                   You can get your bluetooth public address here.

                               

                                   Update your stack handler code by adding this event.

                              -------------------------------------------------------------------------------------------------

                                  

                                          case CYBLE_EVT_GAP_KEYINFO_EXCHNGE_CMPLT:

                                                   

                                                    /*Key Information exchange completed */

                                                    CYBLE_GAP_SMP_KEY_DIST_T KEY;

                                                    int j;

                                         

                                                    KEY = *(CYBLE_GAP_SMP_KEY_DIST_T *)eventParam;

                                         

                                                    for( j = 1; j < CYBLE_GAP_SMP_IDADDR_DATA_SIZE; j++ )

                                                    {

                                                             printf("%2.2x",KEY.idAddrInfo[CYBLE_GAP_SMP_IDADDR_DATA_SIZE-j]);

                                                    }

                                          break;