BLe Central Role. It can connect to a number of peripheral that use the same uart ble services
Dependencies: BLE_API mbed nRF51822
main.cpp@2:4b53d13d9851, 2015-11-22 (annotated)
- Committer:
- mbed_tw_hoehoe
- Date:
- Sun Nov 22 11:51:48 2015 +0000
- Revision:
- 2:4b53d13d9851
- Parent:
- 1:2f1203d70643
- Child:
- 3:d6f80e11a7f4
2nd commit.
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:4b53d13d9851 | 5 | #include "ble_radio_notification.h" |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 6 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 7 | BLE ble; |
mbed_tw_hoehoe | 1:2f1203d70643 | 8 | Serial pc(USBTX, USBRX); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 9 | |
mbed_tw_hoehoe | 2:4b53d13d9851 | 10 | DiscoveredCharacteristic accelChar; |
mbed_tw_hoehoe | 2:4b53d13d9851 | 11 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 12 | const uint8_t MPU6050_service_uuid[] = { |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 13 | 0x45,0x35,0x56,0x80,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 | const uint8_t MPU6050_Accel_Characteristic_uuid[] = { |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 17 | 0x45,0x35,0x56,0x81,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9 |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 18 | }; |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 19 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 20 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 21 | UUID serviceUUID(MPU6050_service_uuid); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 22 | UUID accelUUID(MPU6050_Accel_Characteristic_uuid); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 23 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 24 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 25 | void scanCallback(const Gap::AdvertisementCallbackParams_t *params) { |
mbed_tw_hoehoe | 1:2f1203d70643 | 26 | pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 27 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 28 | params->rssi, params->isScanResponse, params->type); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 29 | |
mbed_tw_hoehoe | 2:4b53d13d9851 | 30 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 31 | ble.connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 32 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 33 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 34 | void serviceDiscoveryCallback(const DiscoveredService *service) { |
mbed_tw_hoehoe | 1:2f1203d70643 | 35 | pc.printf("service found...\r\n"); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 36 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 37 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 38 | void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { |
mbed_tw_hoehoe | 1:2f1203d70643 | 39 | pc.printf("characteristicDiscoveryCallback\r\n"); |
mbed_tw_hoehoe | 2:4b53d13d9851 | 40 | |
mbed_tw_hoehoe | 2:4b53d13d9851 | 41 | const uint8_t *longUUIDBytes = characteristicP->getUUID().getBaseUUID(); |
mbed_tw_hoehoe | 2:4b53d13d9851 | 42 | bool flag = true; |
mbed_tw_hoehoe | 2:4b53d13d9851 | 43 | |
mbed_tw_hoehoe | 2:4b53d13d9851 | 44 | if (characteristicP->getUUID().shortOrLong() != UUID::UUID_TYPE_SHORT) { |
mbed_tw_hoehoe | 2:4b53d13d9851 | 45 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++){ |
mbed_tw_hoehoe | 2:4b53d13d9851 | 46 | if( longUUIDBytes[i]!=MPU6050_Accel_Characteristic_uuid[i]){ |
mbed_tw_hoehoe | 2:4b53d13d9851 | 47 | flag=false; |
mbed_tw_hoehoe | 2:4b53d13d9851 | 48 | } |
mbed_tw_hoehoe | 2:4b53d13d9851 | 49 | } |
mbed_tw_hoehoe | 2:4b53d13d9851 | 50 | if(flag == true) { |
mbed_tw_hoehoe | 2:4b53d13d9851 | 51 | accelChar = *characteristicP; |
mbed_tw_hoehoe | 2:4b53d13d9851 | 52 | ble.gattClient().read(characteristicP->getConnHandle(), characteristicP->getValueHandle(), 0); |
mbed_tw_hoehoe | 2:4b53d13d9851 | 53 | } |
mbed_tw_hoehoe | 2:4b53d13d9851 | 54 | } |
mbed_tw_hoehoe | 2:4b53d13d9851 | 55 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 56 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 57 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 58 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { |
mbed_tw_hoehoe | 1:2f1203d70643 | 59 | pc.printf("GAP_EVT_CONNECTED"); |
mbed_tw_hoehoe | 2:4b53d13d9851 | 60 | |
mbed_tw_hoehoe | 2:4b53d13d9851 | 61 | if (params->role == Gap::CENTRAL) { |
mbed_tw_hoehoe | 2:4b53d13d9851 | 62 | ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, serviceUUID, accelUUID); |
mbed_tw_hoehoe | 2:4b53d13d9851 | 63 | //ble.stopScan(); |
mbed_tw_hoehoe | 2:4b53d13d9851 | 64 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 65 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 66 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 67 | void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { |
mbed_tw_hoehoe | 1:2f1203d70643 | 68 | pc.printf("terminated SD for handle %u\r\n", connectionHandle); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 69 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 70 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 71 | void triggerRead(const GattReadCallbackParams *response) { |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 72 | printf("triggerRead....."); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 73 | for(int i=0; i < response->len; i++){ |
mbed_tw_hoehoe | 1:2f1203d70643 | 74 | pc.printf("data %x", response->data[i]); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 75 | } |
mbed_tw_hoehoe | 1:2f1203d70643 | 76 | pc.printf("\r\n"); |
mbed_tw_hoehoe | 2:4b53d13d9851 | 77 | accelChar.read(); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 78 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 79 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 80 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){//(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) { |
mbed_tw_hoehoe | 1:2f1203d70643 | 81 | pc.printf("disconnected\r\n"); |
mbed_tw_hoehoe | 2:4b53d13d9851 | 82 | wait(8.0); |
mbed_tw_hoehoe | 2:4b53d13d9851 | 83 | ble.gap().startScan(scanCallback); |
mbed_tw_hoehoe | 2:4b53d13d9851 | 84 | } |
mbed_tw_hoehoe | 2:4b53d13d9851 | 85 | |
mbed_tw_hoehoe | 2:4b53d13d9851 | 86 | void your_radio_callback_handler(bool radio_active) |
mbed_tw_hoehoe | 2:4b53d13d9851 | 87 | { |
mbed_tw_hoehoe | 2:4b53d13d9851 | 88 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 89 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 90 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 91 | int main(void) { |
mbed_tw_hoehoe | 1:2f1203d70643 | 92 | pc.baud(9600); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 93 | wait(8.0); |
mbed_tw_hoehoe | 1:2f1203d70643 | 94 | pc.printf("start\r\n"); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 95 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 96 | ble.init(); |
mbed_tw_hoehoe | 2:4b53d13d9851 | 97 | ble_radio_notification_init(NRF_APP_PRIORITY_HIGH, |
mbed_tw_hoehoe | 2:4b53d13d9851 | 98 | NRF_RADIO_NOTIFICATION_DISTANCE_4560US, |
mbed_tw_hoehoe | 2:4b53d13d9851 | 99 | your_radio_callback_handler); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 100 | ble.onConnection(connectionCallback); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 101 | ble.onDisconnection(disconnectionCallback); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 102 | ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 103 | ble.gattClient().onDataRead(triggerRead); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 104 | //ble.gattClient().onDataWrite(triggerToggledWrite); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 105 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 106 | ble.gap().setScanParams(500, 400); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 107 | ble.gap().startScan(scanCallback); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 108 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 109 | while (true) { |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 110 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 111 | } |