init
Dependencies: BLE_API mbed nRF51822
main.cpp
- Committer:
- jslater8
- Date:
- 2015-07-21
- Revision:
- 0:ef5229893eb9
- Child:
- 1:30f1232e4c17
File content as of revision 0:ef5229893eb9:
/* mbed Microcontroller Library * Copyright (c) 2006-2015 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "mbed.h" #include "BLE.h" #include "ble/DiscoveredCharacteristic.h" #include "ble/DiscoveredService.h" #define DUMP_ADV_DATA 0 #define BLE_CHECK(X) (X == BLE_ERROR_NONE) ? (printf("{{success}}\r\n")) : printf("{{failure}} %s at line %u\r\n", #X, __LINE__); #define BLE_EQUAL(X,Y) ((X)==(Y)) ? (printf("{{sucess}}\n")) : printf("{{failure}}\n"); BLE ble; Gap::Address_t address; Gap::AddressType_t *addressType; DigitalOut myled(p21); Ticker tick; DiscoveredCharacteristic HRMCharacteristic; bool test = false; void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { for (int i = 0; i < 5; i++){ if(address[i] != params->peerAddr[i]){ return; } } printf("Advert Detected from HRM\r\n"); //BLE_CHECK(ble.gap().connect(params->peerAddr,Gap::ADDR_TYPE_RANDOM_STATIC,NULL,NULL)); } void blink(void){ if (test){ HRMCharacteristic.read(); } } void serviceDiscoveryCallback(const DiscoveredService *service) { if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); } else { printf("S UUID-"); const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { printf("%02x", longUUIDBytes[i]); } printf("attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); } } void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { if (characteristicP->getShortUUID() == 0x2a37) { /* !ALERT! Alter this filter to suit your device. */ HRMCharacteristic = *characteristicP; test = true; } } void connectionCallback(const Gap::ConnectionCallbackParams_t *params){ printf("Connected to: %d:%d:%d:%d:%d:%d\n", params->peerAddr[0], params->peerAddr[1], params->peerAddr[2], params->peerAddr[3], params->peerAddr[4], params->peerAddr[5]); if (params->role == Gap::CENTRAL) { // ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); BLE_CHECK(ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0x180d, 0x2a37)); } } void triggerToggledWrite(const GattReadCallbackParams *response) { if (response->handle == HRMCharacteristic.getValueHandle()) { printf("%d\n", response->data[1]); } } void connectTest(){ int x; scanf("%d",&x); BLE_CHECK(ble.gap().connect(address,Gap::ADDR_TYPE_RANDOM_STATIC,NULL,NULL)); } void deviceNameTest(){ } int main(void) { myled = 1; printf("{{success}}" "\n" "{{end}}" "\n"); ble.init(); scanf("%hhu",&address[0]); scanf("%hhu",&address[1]); scanf("%hhu",&address[2]); scanf("%hhu",&address[3]); scanf("%hhu",&address[4]); scanf("%hhu",&address[5]); BLE_CHECK(ble.gap().setScanParams(500 /* scan interval */, 200 /* scan window */)); BLE_CHECK(ble.gap().startScan(advertisementCallback)); ble.gap().onConnection(connectionCallback); ble.gattClient().onDataRead(triggerToggledWrite); connectTest(); tick.attach(blink,1); }