Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 81:816977af0b75, committed 2019-03-07
- Comitter:
- sayhuthut
- Date:
- Thu Mar 07 09:38:15 2019 +0000
- Parent:
- 80:a1604e77ee53
- Commit message:
- This an example show Central and Peripheral working concurrently.
Changed in this revision
source/LEDService.h | Show annotated file Show diff for this revision Revisions of this file |
source/main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/LEDService.h Thu Mar 07 09:38:15 2019 +0000 @@ -0,0 +1,44 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 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. + */ + +#ifndef __BLE_LED_SERVICE_H__ +#define __BLE_LED_SERVICE_H__ + +class LEDService { +public: + const static uint16_t LED_SERVICE_UUID = 0xA000; + const static uint16_t LED_STATE_CHARACTERISTIC_UUID = 0xA001; + + LEDService(BLEDevice &_ble, bool initialValueForLEDCharacteristic) : + ble(_ble), ledState(LED_STATE_CHARACTERISTIC_UUID, &initialValueForLEDCharacteristic) + { + GattCharacteristic *charTable[] = {&ledState}; + GattService ledService(LED_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); + + ble.gattServer().addService(ledService); + } + + GattAttribute::Handle_t getValueHandle() const + { + return ledState.getValueHandle(); + } + +private: + BLEDevice &ble; + ReadWriteGattCharacteristic<bool> ledState; +}; + +#endif /* #ifndef __BLE_LED_SERVICE_H__ */
--- a/source/main.cpp Wed Feb 27 13:02:16 2019 +0000 +++ b/source/main.cpp Thu Mar 07 09:38:15 2019 +0000 @@ -21,9 +21,11 @@ #include "ble/DiscoveredService.h" #include "ble/gap/Gap.h" #include "ble/gap/AdvertisingDataParser.h" +#include "LEDService.h" #include "pretty_printer.h" const static char PEER_NAME[] = "LED"; +const static char DEVICE_NAME[] = "LED"; static EventQueue event_queue(/* event count */ 10 * EVENTS_EVENT_SIZE); @@ -91,7 +93,10 @@ _event_queue(event_queue), _alive_led(LED1, 1), _actuated_led(LED2, 0), - _is_connecting(false) { } + _is_connecting(false), + _led_uuid(LEDService::LED_SERVICE_UUID), + _led_service(NULL), + _adv_data_builder(_adv_buffer) { } ~LEDBlinkerDemo() { } @@ -105,6 +110,50 @@ _event_queue.dispatch_forever(); } + void start_advertising() { + /* Create advertising parameters and payload */ + + ble::AdvertisingParameters adv_parameters( + ble::advertising_type_t::CONNECTABLE_UNDIRECTED, + ble::adv_interval_t(ble::millisecond_t(1000)) + ); + + _adv_data_builder.setFlags(); + _adv_data_builder.setLocalServiceList(mbed::make_Span(&_led_uuid, 1)); + _adv_data_builder.setName(DEVICE_NAME); + + /* Setup advertising */ + + ble_error_t error = _ble.gap().setAdvertisingParameters( + ble::LEGACY_ADVERTISING_HANDLE, + adv_parameters + ); + + if (error) { + printf("_ble.gap().setAdvertisingParameters() failed\r\n"); + return; + } + + error = _ble.gap().setAdvertisingPayload( + ble::LEGACY_ADVERTISING_HANDLE, + _adv_data_builder.getAdvertisingData() + ); + + if (error) { + printf("_ble.gap().setAdvertisingPayload() failed\r\n"); + return; + } + + /* Start advertising */ + + error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); + + if (error) { + printf("_ble.gap().startAdvertising() failed\r\n"); + return; + } + } + private: /** Callback triggered when the ble initialization process has finished */ void on_init_complete(BLE::InitializationCompleteCallbackContext *params) { @@ -118,6 +167,9 @@ _ble.gattClient().onDataRead(trigger_toggled_write); _ble.gattClient().onDataWritten(trigger_read); + _led_service = new LEDService(_ble, false); + _ble.gattServer().onDataWritten(this, &LEDBlinkerDemo::on_data_written); + ble::ScanParameters scan_params; _ble.gap().setScanParameters(scan_params); _ble.gap().startScan(); @@ -136,6 +188,7 @@ } void onConnectionComplete(const ble::ConnectionCompleteEvent& event) { + printf("onConnectionComplete\r\n"); if (event.getOwnRole() == ble::connection_role_t::CENTRAL) { _ble.gattClient().onServiceDiscoveryTermination(discovery_termination); _ble.gattClient().launchServiceDiscovery( @@ -145,6 +198,10 @@ 0xa000, 0xa001 ); + printf("Connected as Central, start advertising\r\n"); + start_advertising(); + } else if (event.getOwnRole() == ble::connection_role_t::PERIPHERAL) { + printf("Connected as Peripheral\r\n"); } else { _ble.gap().startScan(); } @@ -203,14 +260,34 @@ } } + /** + * This callback allows the LEDService to receive updates to the ledState Characteristic. + * + * @param[in] params Information about the characterisitc being updated. + */ + void on_data_written(const GattWriteCallbackParams *params) { + printf("led_service getValueHandle() %x\r\n", _led_service->getValueHandle()); + printf("on_data_written, handle:%x len:%d\r\n", params->handle, params->len); + if ((params->handle == _led_service->getValueHandle()) && (params->len == 1)) { + _actuated_led = *(params->data); + } + } + private: BLE &_ble; events::EventQueue &_event_queue; DigitalOut _alive_led; DigitalOut _actuated_led; bool _is_connecting; + + UUID _led_uuid; + LEDService *_led_service; + + uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE]; + ble::AdvertisingDataBuilder _adv_data_builder; }; + /** Schedule processing of events from the BLE middleware in the event queue. */ void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) { event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents));