new version for central test two peripherals

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_Nano_Central_Type3 by Francesco Pavoni

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Copyright (c) 2016 RedBear
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
00005  * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
00006  * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
00007  *
00008  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00009  *
00010  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00011  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00012  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
00013  * IN THE SOFTWARE.
00014  */
00015 /**
00016  * @brief This example is used to demonstrate central functions.
00017  *        Device with this application will try to connect to example "BLE_HRM" or "BLE_simplePeripheral"
00018  *        If connecting to "BLE_HRM", will get heart rate
00019  *        If connecting to "BLE_simplePeripheral, will discovery all services and characteristics
00020  */
00021 #include "mbed.h"
00022 #include "ble/BLE.h"
00023 #include "ble/DiscoveredCharacteristic.h"
00024 #include "ble/DiscoveredService.h"
00025 #include "ble/GapScanningParams.h"
00026 #include "ble_radio_notification.h"
00027 #include "ble_gap.h"
00028 
00029 BLE ble;
00030 Serial pc(USBTX, USBRX);
00031 
00032 //const uint8_t MPU6050_service_uuid[] = {0x45,0x35,0x56,0x80,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9};
00033 //const uint8_t MPU6050_Accel_Characteristic_uuid[] = {0x45,0x35,0x56,0x81,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9};
00034 
00035 static uint8_t service1_uuid[]    = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00036 static uint8_t service1_chars1[]  = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00037 static uint8_t service1_chars2[]  = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00038 static uint8_t service1_chars3[]  = {0x71, 0x3D, 0, 4, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00039 
00040 
00041 UUID service_uuid(0x180D); // (1) this change in sensor PWM 
00042 UUID chars_uuid1(0x2A37);
00043 UUID chars_uuid2(service1_chars2);
00044 UUID chars_uuid3(service1_chars3);
00045 
00046 static uint8_t device_is_hrm = 0;
00047 static uint8_t device_is_simple_peripheral = 0;
00048 
00049 // When found the match characteristic, set 1.
00050 static uint8_t characteristic_is_fond = 0;
00051 // When found the match descriptor, set 1.
00052 static uint8_t descriptor_is_found = 0;
00053 // To save the hrm characteristic and descriptor
00054 static DiscoveredCharacteristic            chars_hrm;
00055 static DiscoveredCharacteristicDescriptor  desc_of_chars_hrm(NULL,GattAttribute::INVALID_HANDLE,GattAttribute::INVALID_HANDLE,UUID::ShortUUIDBytes_t(0));
00056 
00057 static void scanCallback(const Gap::AdvertisementCallbackParams_t *params);
00058 static void discoveredServiceCallBack(const DiscoveredService *service);
00059 static void discoveredCharacteristicCallBack(const DiscoveredCharacteristic *chars);
00060 static void discoveryTerminationCallBack(Gap::Handle_t connectionHandle);
00061 static void discoveredCharsDescriptorCallBack(const CharacteristicDescriptorDiscovery::DiscoveryCallbackParams_t *params);
00062 static void discoveredDescTerminationCallBack(const CharacteristicDescriptorDiscovery::TerminationCallbackParams_t *params) ;
00063 
00064 //DiscoveredCharacteristic accelChar;
00065 //UUID serviceUUID(MPU6050_service_uuid);
00066 //UUID accelUUID(MPU6050_Accel_Characteristic_uuid);
00067 
00068 #define NUMBER_OF_PERIPHERALS 2
00069 
00070 typedef struct {
00071     Gap::Handle_t   handle;
00072     Gap::Address_t  address;
00073     bool    connected;
00074     uint8_t*    deviceName;
00075 } peripheral_t;
00076 
00077 static peripheral_t gs_peripheral[NUMBER_OF_PERIPHERALS];
00078 
00079 uint32_t ble_advdata_parser(uint8_t type, uint8_t advdata_len, uint8_t *p_advdata, uint8_t *len, uint8_t *p_field_data)
00080 {
00081        pc.printf("ble_advdata_parse  \r\n");
00082 
00083     uint8_t index=0;
00084     uint8_t field_length, field_type;
00085     
00086     while(index<advdata_len)
00087     {
00088         field_length = p_advdata[index];
00089         field_type   = p_advdata[index+1];
00090         if(field_type == type)
00091         {
00092             memcpy(p_field_data, &p_advdata[index+2], (field_length-1));
00093             *len = field_length - 1;
00094             return NRF_SUCCESS;
00095         }
00096         pc.printf("FOUND  \r\n");
00097         index += field_length + 1;
00098     }
00099      pc.printf("NRF_ERROR_NOT_FOUND  \r\n");
00100     return NRF_ERROR_NOT_FOUND;
00101 }
00102 
00103 
00104 void startDiscovery(uint16_t handle) {
00105   /**
00106    * Launch service discovery. Once launched, application callbacks will beinvoked for matching services or characteristics.
00107    * isServiceDiscoveryActive() can be used to determine status, and a termination callback (if one was set up)will be invoked at the end.
00108    * Service discovery can be terminated prematurely,if needed, using terminateServiceDiscovery().
00109    *
00110    * @param[in]  connectionHandle   Handle for the connection with the peer.
00111    * @param[in]  sc  This is the application callback for a matching service. Taken as NULL by default.
00112    *                 Note: service discovery may still be active when this callback is issued;
00113    *                 calling asynchronous BLE-stack APIs from within this application callback might cause the stack to abort service discovery.
00114    *                 If this becomes an issue, it may be better to make a local copy of the discoveredService and wait for service discovery to terminate before operating on the service.
00115    * @param[in]  cc  This is the application callback for a matching characteristic.Taken as NULL by default.
00116    *                 Note: service discovery may still be active when this callback is issued;
00117    *                 calling asynchronous BLE-stack APIs from within this application callback might cause the stack to abort service discovery.
00118    *                 If this becomes an issue, it may be better to make a local copy of the discoveredCharacteristic and wait for service discovery to terminate before operating on the characteristic.
00119    * @param[in]  matchingServiceUUID  UUID-based filter for specifying a service in which the application is interested.
00120    *                                  By default it is set as the wildcard UUID_UNKNOWN, in which case it matches all services.
00121    * @param[in]  matchingCharacteristicUUIDIn  UUID-based filter for specifying characteristic in which the application is interested.
00122    *                                           By default it is set as the wildcard UUID_UKNOWN to match against any characteristic.
00123    *
00124    * @note     Using wildcard values for both service-UUID and characteristic-UUID will result in complete service discovery:
00125    *           callbacks being called for every service and characteristic.
00126    *
00127    * @note     Providing NULL for the characteristic callback will result in characteristic discovery being skipped for each matching service.
00128    *           This allows for an inexpensive method to discover only services.
00129    *
00130    * @return   BLE_ERROR_NONE if service discovery is launched successfully; else an appropriate error.
00131    */
00132    pc.printf("start discovery \r\n");
00133   if(device_is_hrm) {
00134      pc.printf("device_is_hrm \r\n");
00135     ble.gattClient().launchServiceDiscovery(handle, discoveredServiceCallBack, discoveredCharacteristicCallBack, service_uuid, chars_uuid1);}
00136   if(device_is_simple_peripheral){
00137     pc.printf("device_is_simple_peripheral \r\n");
00138     ble.gattClient().launchServiceDiscovery(handle, discoveredServiceCallBack, discoveredCharacteristicCallBack);}
00139 }
00140 
00141 /**
00142  * @brief  Callback handle for scanning device
00143  *
00144  * @param[in]  *params   params->peerAddr            The peer's BLE address
00145  *                       params->rssi                The advertisement packet RSSI value
00146  *                       params->isScanResponse      Whether this packet is the response to a scan request
00147  *                       params->type                The type of advertisement
00148  *                                                   (enum from 0 ADV_CONNECTABLE_UNDIRECTED,ADV_CONNECTABLE_DIRECTED,ADV_SCANNABLE_UNDIRECTED,ADV_NON_CONNECTABLE_UNDIRECTED)
00149  *                       params->advertisingDataLen  Length of the advertisement data
00150  *                       params->advertisingData     Pointer to the advertisement packet's data
00151  */
00152 
00153  void scanCallback(const Gap::AdvertisementCallbackParams_t *params) {
00154     pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
00155            params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
00156            params->rssi, params->isScanResponse, params->type);
00157     
00158     uint8_t len;
00159     uint8_t adv_name[31];
00160     if( NRF_SUCCESS == ble_advdata_parser(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME,
00161                              params->advertisingDataLen,
00162                             (uint8_t *)params->advertisingData, &len, adv_name)){
00163             pc.printf("NRF_Success \r\n");
00164 
00165 // Cycle fo rconnect two peripheral 
00166         for(uint8_t i=0; i<2; i++){
00167             pc.printf("Cycle go connect \r\n");
00168             if(gs_peripheral[i].connected == false){
00169                 pc.printf("If connect == false -> Connect \r\n");
00170                 memcpy(gs_peripheral[i].address, params->peerAddr, sizeof(params->peerAddr)); 
00171                 gs_peripheral[i].deviceName = adv_name;
00172                 ble.connect(params->peerAddr, BLEProtocol::AddressType::RANDOM_STATIC, NULL, NULL);
00173                 // (5) forsatura da eliminare
00174                 device_is_simple_peripheral = 1;
00175 
00176                 
00177                 // new edit add FP
00178                 // (2) Make set name peripheral with button
00179                 if( memcmp("TXRX", adv_name, 4) == 0x00 ) {
00180                           pc.printf("peripheral type simpleControl \r\n");
00181                           device_is_simple_peripheral = 1;
00182                           // On Led 4
00183                     }
00184                     // (3) make set name peripheral with sensor 
00185                     if(memcmp("Nordic_HRM", adv_name, 10) == 0x00) {
00186                               pc.printf("peripheral type Nordic_HRM \r\n");
00187                               device_is_hrm = 1;
00188                               // On led 5
00189                         }
00190                         // end
00191                     
00192                 break;
00193             }
00194         }
00195         pc.printf("Stop Scan \r\n");
00196         ble.stopScan();
00197     }
00198 }
00199 
00200 /** @brief  Connection callback handle
00201  *
00202  *  @param[in] *params   params->handle : The ID for this connection
00203  *                       params->role : PERIPHERAL  = 0x1, // Peripheral Role
00204  *                                      CENTRAL     = 0x2, // Central Role.
00205  *                       params->peerAddrType : The peer's BLE address type
00206  *                       params->peerAddr : The peer's BLE address
00207  *                       params->ownAddrType : This device's BLE address type
00208  *                       params->ownAddr : This devices's BLE address
00209  *                       params->connectionParams->minConnectionInterval
00210  *                       params->connectionParams->maxConnectionInterval
00211  *                       params->connectionParams->slaveLatency
00212  *                       params->connectionParams->connectionSupervisionTimeout
00213  */
00214 // Comment 1
00215 void connectionCallBack( const Gap::ConnectionCallbackParams_t *params ) {
00216   /*
00217   uint8_t index;
00218 
00219   Serial.print("The conn handle : ");
00220   Serial.println(params->handle, HEX);
00221   Serial.print("  The peerAddr : ");
00222   for(index=0; index<6; index++) {
00223     Serial.print(params->peerAddr[index], HEX);
00224     Serial.print(" ");
00225   }
00226   Serial.println(" ");*/
00227   
00228   // start to discovery
00229   // Attention Comment
00230   // this state is set in startDiscovery
00231   /*
00232   pc.printf("GAP_EVT_CONNECTED\r\n");
00233     if (params->role == Gap::CENTRAL) {
00234 
00235         for(uint8_t i=0; i<2; i++){
00236             if(gs_peripheral[i].connected == false){
00237                 gs_peripheral[i].handle = params->handle;
00238                 gs_peripheral[i].connected = true;
00239                 break;
00240             }
00241         }
00242         ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, serviceUUID, accelUUID);
00243         //ble.gattClient().launchServiceDiscovery(params->handle, discoveredServiceCallBack, discoveredCharacteristicCallBack, service_uuid(0x180D));
00244         
00245     }*/
00246   
00247   
00248   //Comment 3
00249   // check is set Ok
00250      pc.printf("connectionCallBack \r\n");
00251   startDiscovery(params->handle);
00252 }
00253 
00254 /** @brief  Disconnect callback handle
00255  *
00256  *  @param[in] *params   params->handle : connect handle
00257  *                       params->reason : CONNECTION_TIMEOUT                          = 0x08,
00258  *                                        REMOTE_USER_TERMINATED_CONNECTION           = 0x13,
00259  *                                        REMOTE_DEV_TERMINATION_DUE_TO_LOW_RESOURCES = 0x14,  // Remote device terminated connection due to low resources.
00260  *                                        REMOTE_DEV_TERMINATION_DUE_TO_POWER_OFF     = 0x15,  // Remote device terminated connection due to power off.
00261  *                                        LOCAL_HOST_TERMINATED_CONNECTION            = 0x16,
00262  *                                        CONN_INTERVAL_UNACCEPTABLE                  = 0x3B,
00263  */
00264  // Comment 2
00265  
00266 void disconnectionCallBack(const Gap::DisconnectionCallbackParams_t *params) {
00267  // Serial.println("Disconnected, start to scanning");
00268       pc.printf("Disconnected, start to scanning \r\n");
00269 
00270  // New edit FP
00271   device_is_simple_peripheral = 0;
00272   device_is_hrm = 0;
00273   characteristic_is_fond = 0;
00274   descriptor_is_found = 0;
00275   ble.startScan(scanCallback); // scanCallback
00276   // end
00277   
00278   pc.printf("disconnected\r\n");
00279     
00280     for(uint8_t i=0; i<2; i++){
00281         if(gs_peripheral[i].handle == params->handle){
00282             gs_peripheral[i].connected = false;
00283             gs_peripheral[i].handle = 0xFFFF;
00284         }
00285     }
00286     wait(8.0);
00287     ble.gap().startScan(scanCallback);
00288 }
00289 
00290 /** @brief Discovered service callback handle
00291  *
00292  *  @param[in] *service  service->getUUID()  The UUID of service
00293  *                       service->getStartHandle()
00294  *                       service->getEndHandle()
00295  */
00296 
00297 static void discoveredServiceCallBack(const DiscoveredService *service) {
00298     pc.printf("service found...\r\n");    
00299 }
00300 
00301 /** @brief Discovered characteristics callback handle
00302  *
00303  *  @param[in] *chars  chars->getUUID()        The UUID of service
00304  *                     chars->getProperties()  broadcast() : Check if broadcasting is permitted. True is permited.
00305  *                                             read() : Check reading is permitted.
00306  *                                             writeWoResp() : Check if writing with Write Command is permitted
00307  *                                             write() : Check if writing with Write Request is permitted.
00308  *                                             notify() : Check notifications are permitted.
00309  *                                             indicate() : Check if indications are permitted.
00310  *                                             authSignedWrite() : Check if writing with Signed Write Command is permitted.
00311  *                     chars->getDeclHandle()  characteristic's declaration attribute handle
00312  *                     chars->getValueHandle() characteristic's value attribute handle
00313  *                     chars->getLastHandle()  characteristic's last attribute handle
00314  */
00315 static void discoveredCharacteristicCallBack(const DiscoveredCharacteristic *characteristicP) {
00316     pc.printf("characteristicDiscoveryCallback\r\n");
00317     
00318     //accelChar = *characteristicP;
00319     // (4) change serial.print -> println how make ?
00320     // New edit FP
00321     if(characteristicP->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
00322         pc.printf("characteristicDiscoveryCallback -1- \r\n");
00323     //Serial.println(chars->getUUID().getShortUUID(), HEX);
00324     if(characteristicP->getUUID().getShortUUID() == 0x2A37) {
00325         pc.printf("characteristicDiscoveryCallback -2- \r\n");
00326       //Serial.println("Found HRM characteristic ");
00327       characteristic_is_fond = 1;
00328       chars_hrm = *characteristicP;
00329     }
00330   } else {
00331     pc.printf("characteristicDiscoveryCallback -3- \r\n");
00332     uint8_t index;
00333     const uint8_t *uuid = characteristicP->getUUID().getBaseUUID();
00334     for(index=0; index<16; index++) {
00335      pc.printf("characteristicDiscoveryCallback -4- \r\n");
00336      // Serial.print(uuid[index], HEX);
00337       //Serial.print(" ");
00338     }
00339     //Serial.println(" ");
00340   }
00341    // end 
00342    
00343    
00344     for(uint8_t i=0; i<2; i++){
00345          pc.printf("characteristicDiscoveryCallback -5- \r\n");
00346         if(gs_peripheral[i].connected){
00347             pc.printf("characteristicDiscoveryCallback -6- \r\n");
00348             ble.gattClient().read(characteristicP->getConnectionHandle(), characteristicP->getValueHandle(), 0);
00349         }
00350     }
00351 
00352 }
00353 
00354 
00355 // Coomet 4
00356 static void discoveryTerminationCallBack(Gap::Handle_t connectionHandle) {
00357     pc.printf("terminated SD for handle %u\r\n", connectionHandle);
00358     
00359     // New edit FP
00360     if(characteristic_is_fond == 1) {
00361     ble.gattClient().discoverCharacteristicDescriptors(chars_hrm, discoveredCharsDescriptorCallBack, discoveredDescTerminationCallBack);
00362   }
00363   // end
00364 }
00365 
00366 
00367 /** @brief Discovered descriptor of characteristic callback handle
00368  *
00369  *  @param[in] *params  params->characteristic  DiscoveredCharacteristic
00370  *                      params->descriptor  descriptor.getUUID() : The uuid of descriptor atrribute
00371  *                                          descriptor.getConnectionHandle()
00372  *                                          descriptor.getAttributeHandle() : Arrtibute handle
00373  */
00374  static void discoveredCharsDescriptorCallBack(const CharacteristicDescriptorDiscovery::DiscoveryCallbackParams_t *params) {
00375   //Serial.println("\r\n----discovered descriptor");
00376   //Serial.print("Desriptor UUID         : ");
00377   //Serial.println(params->descriptor.getUUID().getShortUUID(), HEX);
00378   if(params->descriptor.getUUID().getShortUUID() == 0x2902) {
00379     // Save characteristic info
00380     descriptor_is_found = 1;
00381     desc_of_chars_hrm = params->descriptor;
00382   }
00383   //Serial.print("connectionHandle       : ");
00384   //Serial.println(params->descriptor.getConnectionHandle(), HEX);
00385   //Serial.print("descriptor Handle      : ");
00386   //Serial.println(params->descriptor.getAttributeHandle(), HEX);
00387 }
00388 
00389 /** @brief Discovered descriptor of characteristic termination callback handle
00390  *
00391  *  @param[in] *params  params->characteristic  DiscoveredCharacteristic
00392  *                      params->status  Status of the discovery operation
00393  */
00394  static void discoveredDescTerminationCallBack(const CharacteristicDescriptorDiscovery::TerminationCallbackParams_t *params) {
00395  // Serial.println("\r\n----discovery descriptor Termination");
00396   if(descriptor_is_found) {
00397    // Serial.println("Open HRM notify");
00398     uint16_t value = 0x0001;
00399     ble.gattClient().write(GattClient::GATT_OP_WRITE_REQ, chars_hrm.getConnectionHandle(), desc_of_chars_hrm.getAttributeHandle(), 2, (uint8_t *)&value);
00400   }
00401 }
00402 
00403 /** @brief  write callback handle
00404  *
00405  *  @param[in] *params   params->connHandle : The handle of the connection that triggered the event
00406  *                       params->handle : Attribute Handle to which the write operation applies
00407  *                       params->writeOp : OP_INVALID               = 0x00,  // Invalid operation.
00408  *                                           OP_WRITE_REQ             = 0x01,  // Write request.
00409  *                                           OP_WRITE_CMD             = 0x02,  // Write command.
00410  *                                           OP_SIGN_WRITE_CMD        = 0x03,  // Signed write command.
00411  *                                           OP_PREP_WRITE_REQ        = 0x04,  // Prepare write request.
00412  *                                           OP_EXEC_WRITE_REQ_CANCEL = 0x05,  // Execute write request: cancel all prepared writes.
00413  *                                           OP_EXEC_WRITE_REQ_NOW    = 0x06,  // Execute write request: immediately execute all prepared writes.
00414  *                       params->offset : Offset for the write operation
00415  *                       params->len : Length (in bytes) of the data to write
00416  *                       params->data : Pointer to the data to write
00417  */
00418  // Attention
00419  void onDataWriteCallBack(const GattWriteCallbackParams *params) {
00420   //Serial.println("GattClient write call back ");
00421 }
00422  
00423  /** @brief  read callback handle
00424  *
00425  *  @param[in] *params   params->connHandle : The handle of the connection that triggered the event
00426  *                       params->handle : Attribute Handle to which the write operation applies
00427  *                       params->offset : Offset for the write operation
00428  *                       params->len : Length (in bytes) of the data to write
00429  *                       params->data : Pointer to the data to write
00430  */
00431  
00432  // Attention
00433 void onDataReadCallBack(const GattReadCallbackParams *params) {
00434  // delete comment 
00435  /* Serial.println("GattClient read call back ");
00436   Serial.print("The handle : ");
00437   Serial.println(params->handle, HEX);
00438   Serial.print("The offset : ");
00439   Serial.println(params->offset, DEC);
00440   Serial.print("The len : ");
00441   Serial.println(params->len, DEC);
00442   Serial.print("The data : ");*/
00443   
00444     pc.printf("triggerRead.....\r\n");
00445     pc.printf("len: %d\r\n", params->len);
00446     const uint8_t *data = params->data;
00447     pc.printf("data ");
00448   
00449   for(uint8_t index=0; index<params->len; index++) {
00450     //Serial.print( params->data[index], HEX);
00451      pc.printf("%f ", (float)data[index]);  
00452   }
00453     pc.printf("\r\n");
00454   //Serial.println("");
00455 }
00456 
00457 /** @brief  hvx callback handle
00458  *
00459  *  @param[in] *params   params->connHandle : The handle of the connection that triggered the event
00460  *                       params->handle : Attribute Handle to which the write operation applies
00461  *                       params->type : BLE_HVX_NOTIFICATION = 0x01
00462  *                                      BLE_HVX_INDICATION   = 0x02
00463  *                       params->len : Length (in bytes) of the data to write
00464  *                       params->data : Pointer to the data to write
00465  */
00466  // Attention
00467 void hvxCallBack(const GattHVXCallbackParams *params) {
00468  // delete Cooment
00469  /* Serial.println("GattClient notify call back ");
00470   Serial.print("The len : ");
00471   Serial.println(params->len, DEC);*/
00472   for(unsigned char index=0; index<params->len; index++) {
00473     //Serial.print(params->data[index], HEX);
00474     pc.printf("%f ", (float)params->data[index]);  
00475   }
00476   // Serial.println("");
00477 }
00478 
00479 // Option
00480 /*void triggerRead(const GattReadCallbackParams *response) {
00481     pc.printf("triggerRead.....\r\n");
00482     
00483     pc.printf("len: %d\r\n", response->len);
00484     const uint8_t *data = response->data;
00485     pc.printf("data ");
00486     for(int i=0; i < response->len; i++){
00487         pc.printf("%f ", (float)data[i]);  
00488     }
00489     pc.printf("\r\n");
00490     // Comment property
00491     //accelChar.read();
00492 }*/
00493 
00494 // is set before
00495 /*void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
00496     pc.printf("disconnected\r\n");
00497     
00498     for(uint8_t i=0; i<3; i++){
00499         if(gs_peripheral[i].handle == params->handle){
00500             gs_peripheral[i].connected = false;
00501             gs_peripheral[i].handle = 0xFFFF;
00502         }
00503     }
00504     wait(8.0);
00505     ble.gap().startScan(scanCallback);
00506 }*/
00507 
00508 int main(void) {
00509     pc.baud(9600);
00510     wait(8.0);
00511     pc.printf("start\r\n");
00512     pc.printf("Avvio start\r\n");
00513 
00514 
00515     ble.init();
00516 
00517     ble.onConnection(connectionCallBack);
00518     ble.onDisconnection(disconnectionCallBack);
00519     
00520     // New edit FP
00521     ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallBack);
00522   ble.gattClient().onHVX(hvxCallBack);
00523   ble.gattClient().onDataWrite(onDataWriteCallBack);
00524   ble.gattClient().onDataRead(onDataReadCallBack);
00525   // scan interval : in milliseconds, valid values lie between 2.5ms and 10.24s
00526   // scan window :in milliseconds, valid values lie between 2.5ms and 10.24s
00527   // timeout : in seconds, between 0x0001 and 0xFFFF, 0x0000 disables timeout
00528   // activeScanning : true or false
00529   // end
00530     
00531     
00532     // Original
00533     //ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
00534     //ble.gattClient().onDataRead(triggerRead);
00535     //ble.gattClient().onDataWrite(triggerToggledWrite);
00536 
00537     ble.gap().setScanParams(500, 400);
00538     ble.gap().startScan(scanCallback);
00539     
00540     // Or
00541     /*
00542     ble.setScanParams(1000, 200, 0, false);
00543   // start scanning
00544   ble.startScan(scanCallBack);
00545   */
00546 
00547     while (true) {
00548         pc.printf("while true\r\n");
00549         ble.waitForEvent();
00550     }
00551 }
00552 
00553 // Altenativa
00554 /*void loop() {
00555   // put your main code here, to run repeatedly:
00556   ble.waitForEvent();
00557 }*/