new version for central test two peripherals

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_Nano_Central_Type3 by Francesco Pavoni

Committer:
mbed_tw_hoehoe
Date:
Sun Nov 22 10:28:59 2015 +0000
Revision:
1:2f1203d70643
Parent:
0:83b5c6efd8d7
Child:
2:4b53d13d9851
First publish.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_tw_hoehoe 0:83b5c6efd8d7 1 #include "mbed.h"
mbed_tw_hoehoe 0:83b5c6efd8d7 2 #include "ble/BLE.h"
mbed_tw_hoehoe 0:83b5c6efd8d7 3 #include "ble/DiscoveredCharacteristic.h"
mbed_tw_hoehoe 0:83b5c6efd8d7 4 #include "ble/DiscoveredService.h"
mbed_tw_hoehoe 0:83b5c6efd8d7 5
mbed_tw_hoehoe 0:83b5c6efd8d7 6 BLE ble;
mbed_tw_hoehoe 1:2f1203d70643 7 Serial pc(USBTX, USBRX);
mbed_tw_hoehoe 0:83b5c6efd8d7 8
mbed_tw_hoehoe 0:83b5c6efd8d7 9 const uint8_t MPU6050_service_uuid[] = {
mbed_tw_hoehoe 0:83b5c6efd8d7 10 0x45,0x35,0x56,0x80,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9
mbed_tw_hoehoe 0:83b5c6efd8d7 11 };
mbed_tw_hoehoe 0:83b5c6efd8d7 12
mbed_tw_hoehoe 0:83b5c6efd8d7 13 const uint8_t MPU6050_Accel_Characteristic_uuid[] = {
mbed_tw_hoehoe 0:83b5c6efd8d7 14 0x45,0x35,0x56,0x81,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9
mbed_tw_hoehoe 0:83b5c6efd8d7 15 };
mbed_tw_hoehoe 0:83b5c6efd8d7 16
mbed_tw_hoehoe 0:83b5c6efd8d7 17
mbed_tw_hoehoe 0:83b5c6efd8d7 18 UUID serviceUUID(MPU6050_service_uuid);
mbed_tw_hoehoe 0:83b5c6efd8d7 19 UUID accelUUID(MPU6050_Accel_Characteristic_uuid);
mbed_tw_hoehoe 0:83b5c6efd8d7 20
mbed_tw_hoehoe 0:83b5c6efd8d7 21
mbed_tw_hoehoe 0:83b5c6efd8d7 22 void scanCallback(const Gap::AdvertisementCallbackParams_t *params) {
mbed_tw_hoehoe 1:2f1203d70643 23 pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
mbed_tw_hoehoe 0:83b5c6efd8d7 24 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
mbed_tw_hoehoe 0:83b5c6efd8d7 25 params->rssi, params->isScanResponse, params->type);
mbed_tw_hoehoe 0:83b5c6efd8d7 26
mbed_tw_hoehoe 0:83b5c6efd8d7 27 ble.stopScan();
mbed_tw_hoehoe 0:83b5c6efd8d7 28 ble.connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
mbed_tw_hoehoe 0:83b5c6efd8d7 29 }
mbed_tw_hoehoe 0:83b5c6efd8d7 30
mbed_tw_hoehoe 0:83b5c6efd8d7 31 void serviceDiscoveryCallback(const DiscoveredService *service) {
mbed_tw_hoehoe 1:2f1203d70643 32 pc.printf("service found...\r\n");
mbed_tw_hoehoe 0:83b5c6efd8d7 33 }
mbed_tw_hoehoe 0:83b5c6efd8d7 34
mbed_tw_hoehoe 0:83b5c6efd8d7 35 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
mbed_tw_hoehoe 1:2f1203d70643 36 pc.printf("characteristicDiscoveryCallback\r\n");
mbed_tw_hoehoe 0:83b5c6efd8d7 37 ble.gattClient().read(characteristicP->getConnHandle(), characteristicP->getValueHandle(), 0);
mbed_tw_hoehoe 0:83b5c6efd8d7 38 }
mbed_tw_hoehoe 0:83b5c6efd8d7 39
mbed_tw_hoehoe 0:83b5c6efd8d7 40 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
mbed_tw_hoehoe 1:2f1203d70643 41 pc.printf("GAP_EVT_CONNECTED");
mbed_tw_hoehoe 0:83b5c6efd8d7 42 ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, serviceUUID, accelUUID);
mbed_tw_hoehoe 0:83b5c6efd8d7 43 }
mbed_tw_hoehoe 0:83b5c6efd8d7 44
mbed_tw_hoehoe 0:83b5c6efd8d7 45 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
mbed_tw_hoehoe 1:2f1203d70643 46 pc.printf("terminated SD for handle %u\r\n", connectionHandle);
mbed_tw_hoehoe 0:83b5c6efd8d7 47 }
mbed_tw_hoehoe 0:83b5c6efd8d7 48
mbed_tw_hoehoe 0:83b5c6efd8d7 49 void triggerRead(const GattReadCallbackParams *response) {
mbed_tw_hoehoe 0:83b5c6efd8d7 50 printf("triggerRead.....");
mbed_tw_hoehoe 0:83b5c6efd8d7 51 for(int i=0; i < response->len; i++){
mbed_tw_hoehoe 1:2f1203d70643 52 pc.printf("data %x", response->data[i]);
mbed_tw_hoehoe 0:83b5c6efd8d7 53 }
mbed_tw_hoehoe 1:2f1203d70643 54 pc.printf("\r\n");
mbed_tw_hoehoe 0:83b5c6efd8d7 55 }
mbed_tw_hoehoe 0:83b5c6efd8d7 56
mbed_tw_hoehoe 0:83b5c6efd8d7 57 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){//(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) {
mbed_tw_hoehoe 1:2f1203d70643 58 pc.printf("disconnected\r\n");
mbed_tw_hoehoe 0:83b5c6efd8d7 59 }
mbed_tw_hoehoe 0:83b5c6efd8d7 60
mbed_tw_hoehoe 0:83b5c6efd8d7 61 int main(void) {
mbed_tw_hoehoe 1:2f1203d70643 62 pc.baud(9600);
mbed_tw_hoehoe 0:83b5c6efd8d7 63 wait(8.0);
mbed_tw_hoehoe 1:2f1203d70643 64 pc.printf("start\r\n");
mbed_tw_hoehoe 0:83b5c6efd8d7 65
mbed_tw_hoehoe 0:83b5c6efd8d7 66 ble.init();
mbed_tw_hoehoe 0:83b5c6efd8d7 67 ble.onConnection(connectionCallback);
mbed_tw_hoehoe 0:83b5c6efd8d7 68 ble.onDisconnection(disconnectionCallback);
mbed_tw_hoehoe 0:83b5c6efd8d7 69 ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
mbed_tw_hoehoe 0:83b5c6efd8d7 70 ble.gattClient().onDataRead(triggerRead);
mbed_tw_hoehoe 0:83b5c6efd8d7 71 //ble.gattClient().onDataWrite(triggerToggledWrite);
mbed_tw_hoehoe 0:83b5c6efd8d7 72
mbed_tw_hoehoe 0:83b5c6efd8d7 73 ble.gap().setScanParams(500, 400);
mbed_tw_hoehoe 0:83b5c6efd8d7 74 ble.gap().startScan(scanCallback);
mbed_tw_hoehoe 0:83b5c6efd8d7 75
mbed_tw_hoehoe 0:83b5c6efd8d7 76 while (true) {
mbed_tw_hoehoe 0:83b5c6efd8d7 77 }
mbed_tw_hoehoe 0:83b5c6efd8d7 78 }