new version for central test two peripherals

Dependencies:   BLE_API mbed nRF51822

Committer:
mbed_tw_hoehoe
Date:
Sat Nov 21 11:13:58 2015 +0000
Revision:
0:83b5c6efd8d7
Child:
1:2f1203d70643
BLE Central Test

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