KeyboardStream example from BLE_HID
Dependencies: BLE_API BLE_HID mbed nRF51822
examples_common.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "ble/services/BatteryService.h" 00018 #include "ble/services/DeviceInformationService.h" 00019 00020 #include "examples_common.h" 00021 00022 static void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey) 00023 { 00024 printf("Input passKey: "); 00025 for (unsigned i = 0; i < Gap::ADDR_LEN; i++) { 00026 printf("%c", passkey[i]); 00027 } 00028 printf("\r\n"); 00029 } 00030 00031 static void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status) 00032 { 00033 if (status == SecurityManager::SEC_STATUS_SUCCESS) { 00034 printf("Security success %d\r\n", status); 00035 } else { 00036 printf("Security failed %d\r\n", status); 00037 } 00038 } 00039 00040 static void securitySetupInitiatedCallback(Gap::Handle_t, bool allowBonding, bool requireMITM, SecurityManager::SecurityIOCapabilities_t iocaps) 00041 { 00042 printf("Security setup initiated\r\n"); 00043 } 00044 00045 void initializeSecurity(BLE &ble) 00046 { 00047 bool enableBonding = true; 00048 bool requireMITM = HID_SECURITY_REQUIRE_MITM; 00049 00050 ble.securityManager().onSecuritySetupInitiated(securitySetupInitiatedCallback); 00051 ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback); 00052 ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback); 00053 00054 ble.securityManager().init(enableBonding, requireMITM, HID_SECURITY_IOCAPS); 00055 } 00056 00057 void initializeHOGP(BLE &ble) 00058 { 00059 static const uint16_t uuid16_list[] = {GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE, 00060 GattService::UUID_DEVICE_INFORMATION_SERVICE, 00061 GattService::UUID_BATTERY_SERVICE}; 00062 00063 DeviceInformationService deviceInfo(ble, "ARM", "m1", "abc", "def", "ghi", "jkl"); 00064 00065 BatteryService batteryInfo(ble, 80); 00066 00067 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | 00068 GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00069 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, 00070 (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00071 00072 // see 5.1.2: HID over GATT Specification (pg. 25) 00073 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00074 // 30ms to 50ms is recommended (5.1.2) 00075 ble.gap().setAdvertisingInterval(50); 00076 }
Generated on Wed Jul 13 2022 01:14:53 by 1.7.2