Central Test.
Dependencies: BLE_API mbed nRF51822
main.cpp
- Committer:
- mbed_tw_hoehoe
- Date:
- 2016-03-30
- Revision:
- 3:d6f80e11a7f4
- Parent:
- 2:4b53d13d9851
- Child:
- 5:f6b6bf98c686
File content as of revision 3:d6f80e11a7f4:
#include "mbed.h" #include "ble/BLE.h" #include "ble/DiscoveredCharacteristic.h" #include "ble/DiscoveredService.h" #include "ble/GapScanningParams.h" #include "ble_radio_notification.h" #include "ble_gap.h" BLE ble; Serial pc(USBTX, USBRX); const uint8_t MPU6050_service_uuid[] = { 0x45,0x35,0x56,0x80,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9 }; const uint8_t MPU6050_Accel_Characteristic_uuid[] = { 0x45,0x35,0x56,0x81,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9 }; DiscoveredCharacteristic accelChar; UUID serviceUUID(MPU6050_service_uuid); UUID accelUUID(MPU6050_Accel_Characteristic_uuid); #define NUMBER_OF_PERIPHERALS 3 typedef struct { Gap::Handle_t handle; Gap::Address_t address; bool connected; uint8_t* deviceName; } peripheral_t; static peripheral_t gs_peripheral[NUMBER_OF_PERIPHERALS]; uint32_t ble_advdata_parser(uint8_t type, uint8_t advdata_len, uint8_t *p_advdata, uint8_t *len, uint8_t *p_field_data) { uint8_t index=0; uint8_t field_length, field_type; while(index<advdata_len) { field_length = p_advdata[index]; field_type = p_advdata[index+1]; if(field_type == type) { memcpy(p_field_data, &p_advdata[index+2], (field_length-1)); *len = field_length - 1; return NRF_SUCCESS; } index += field_length + 1; } return NRF_ERROR_NOT_FOUND; } void scanCallback(const Gap::AdvertisementCallbackParams_t *params) { pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], params->rssi, params->isScanResponse, params->type); uint8_t len; uint8_t adv_name[31]; if( NRF_SUCCESS == ble_advdata_parser(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME, params->advertisingDataLen, (uint8_t *)params->advertisingData, &len, adv_name)){ for(uint8_t i=0; i<3; i++){ if(gs_peripheral[i].connected == false){ memcpy(gs_peripheral[i].address, params->peerAddr, sizeof(params->peerAddr)); gs_peripheral[i].deviceName = adv_name; ble.connect(params->peerAddr, BLEProtocol::AddressType::RANDOM_STATIC, NULL, NULL); break; } } ble.stopScan(); } } void serviceDiscoveryCallback(const DiscoveredService *service) { pc.printf("service found...\r\n"); } void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { pc.printf("characteristicDiscoveryCallback\r\n"); accelChar = *characteristicP; for(uint8_t i=0; i<3; i++){ if(gs_peripheral[i].connected){ ble.gattClient().read(characteristicP->getConnectionHandle(), characteristicP->getValueHandle(), 0); } } } void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { pc.printf("GAP_EVT_CONNECTED\r\n"); if (params->role == Gap::CENTRAL) { for(uint8_t i=0; i<3; i++){ if(gs_peripheral[i].connected == false){ gs_peripheral[i].handle = params->handle; gs_peripheral[i].connected = true; break; } } ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, serviceUUID, accelUUID); } } void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { pc.printf("terminated SD for handle %u\r\n", connectionHandle); } void triggerRead(const GattReadCallbackParams *response) { pc.printf("triggerRead.....\r\n"); pc.printf("len: %d\r\n", response->len); const uint8_t *data = response->data; pc.printf("data "); for(int i=0; i < response->len; i++){ pc.printf("%f ", (float)data[i]); } pc.printf("\r\n"); accelChar.read(); } void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){ pc.printf("disconnected\r\n"); for(uint8_t i=0; i<3; i++){ if(gs_peripheral[i].handle == params->handle){ gs_peripheral[i].connected = false; gs_peripheral[i].handle = 0xFFFF; } } wait(8.0); ble.gap().startScan(scanCallback); } int main(void) { pc.baud(9600); wait(8.0); pc.printf("start\r\n"); ble.init(); ble.onConnection(connectionCallback); ble.onDisconnection(disconnectionCallback); ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); ble.gattClient().onDataRead(triggerRead); //ble.gattClient().onDataWrite(triggerToggledWrite); ble.gap().setScanParams(500, 400); ble.gap().startScan(scanCallback); while (true) { ble.waitForEvent(); } }