|
| 1 | +/* |
| 2 | + Peripheral Explorer |
| 3 | +
|
| 4 | + This example scans for BLE peripherals until one with a particular name ("LED") |
| 5 | + is found. Then connects, and discovers + prints all the peripheral's attributes. |
| 6 | +
|
| 7 | + The circuit: |
| 8 | + - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, |
| 9 | + Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. |
| 10 | +
|
| 11 | + You can use it with another board that is compatible with this library and the |
| 12 | + Peripherals -> LED example. |
| 13 | +
|
| 14 | + This example code is in the public domain. |
| 15 | +*/ |
| 16 | + |
| 17 | +#include <ArduinoBLE.h> |
| 18 | + |
| 19 | +void setup() { |
| 20 | + Serial.begin(9600); |
| 21 | + while (!Serial); |
| 22 | + |
| 23 | + // begin initialization |
| 24 | + if (!BLE.begin()) { |
| 25 | + Serial.println("starting BLE failed!"); |
| 26 | + |
| 27 | + while (1); |
| 28 | + } |
| 29 | + |
| 30 | + Serial.println("BLE Central - Peripheral Explorer"); |
| 31 | + |
| 32 | + // start scanning for peripherals |
| 33 | + BLE.scan(); |
| 34 | +} |
| 35 | + |
| 36 | +void loop() { |
| 37 | + // check if a peripheral has been discovered |
| 38 | + BLEDevice peripheral = BLE.available(); |
| 39 | + |
| 40 | + if (peripheral) { |
| 41 | + // discovered a peripheral, print out address, local name, and advertised service |
| 42 | + Serial.print("Found "); |
| 43 | + Serial.print(peripheral.address()); |
| 44 | + Serial.print(" '"); |
| 45 | + Serial.print(peripheral.localName()); |
| 46 | + Serial.print("' "); |
| 47 | + Serial.print(peripheral.advertisedServiceUuid()); |
| 48 | + Serial.println(); |
| 49 | + |
| 50 | + // see if peripheral is a LED |
| 51 | + if (peripheral.localName() == "LED") { |
| 52 | + // stop scanning |
| 53 | + BLE.stopScan(); |
| 54 | + |
| 55 | + explorerPeripheral(peripheral); |
| 56 | + |
| 57 | + // peripheral disconnected, we are done |
| 58 | + while (1) { |
| 59 | + // do nothing |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +void explorerPeripheral(BLEDevice peripheral) { |
| 66 | + // connect to the peripheral |
| 67 | + Serial.println("Connecting ..."); |
| 68 | + |
| 69 | + if (peripheral.connect()) { |
| 70 | + Serial.println("Connected"); |
| 71 | + } else { |
| 72 | + Serial.println("Failed to connect!"); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + // discover peripheral attributes |
| 77 | + Serial.println("Discovering attributes ..."); |
| 78 | + if (peripheral.discoverAttributes()) { |
| 79 | + Serial.println("Attributes discovered"); |
| 80 | + } else { |
| 81 | + Serial.println("Attribute discovery failed!"); |
| 82 | + peripheral.disconnect(); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // read and print device name of peripheral |
| 87 | + Serial.println(); |
| 88 | + Serial.print("Device name: "); |
| 89 | + Serial.println(peripheral.deviceName()); |
| 90 | + Serial.print("Appearance: 0x"); |
| 91 | + Serial.println(peripheral.appearance(), HEX); |
| 92 | + Serial.println(); |
| 93 | + |
| 94 | + // loop the services of the peripheral and explore each |
| 95 | + for (int i = 0; i < peripheral.serviceCount(); i++) { |
| 96 | + BLEService service = peripheral.service(i); |
| 97 | + |
| 98 | + exploreService(service); |
| 99 | + } |
| 100 | + |
| 101 | + Serial.println(); |
| 102 | + |
| 103 | + // we are done exploring, disconnect |
| 104 | + Serial.println("Disconnecting ..."); |
| 105 | + peripheral.disconnect(); |
| 106 | + Serial.println("Disconnected"); |
| 107 | +} |
| 108 | + |
| 109 | +void exploreService(BLEService service) { |
| 110 | + // print the UUID of the service |
| 111 | + Serial.print("Service "); |
| 112 | + Serial.println(service.uuid()); |
| 113 | + |
| 114 | + // loop the characteristics of the service and explore each |
| 115 | + for (int i = 0; i < service.characteristicCount(); i++) { |
| 116 | + BLECharacteristic characteristic = service.characteristic(i); |
| 117 | + |
| 118 | + exploreCharacteristic(characteristic); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +void exploreCharacteristic(BLECharacteristic characteristic) { |
| 123 | + // print the UUID and properies of the characteristic |
| 124 | + Serial.print("\tCharacteristic "); |
| 125 | + Serial.print(characteristic.uuid()); |
| 126 | + Serial.print(", properties 0x"); |
| 127 | + Serial.print(characteristic.properties(), HEX); |
| 128 | + |
| 129 | + // check if the characteristic is readable |
| 130 | + if (characteristic.canRead()) { |
| 131 | + // read the characteristic value |
| 132 | + characteristic.read(); |
| 133 | + |
| 134 | + if (characteristic.valueLength() > 0) { |
| 135 | + // print out the value of the characteristic |
| 136 | + Serial.print(", value 0x"); |
| 137 | + printData(characteristic.value(), characteristic.valueLength()); |
| 138 | + } |
| 139 | + } |
| 140 | + Serial.println(); |
| 141 | + |
| 142 | + // loop the descriptors of the characteristic and explore each |
| 143 | + for (int i = 0; i < characteristic.descriptorCount(); i++) { |
| 144 | + BLEDescriptor descriptor = characteristic.descriptor(i); |
| 145 | + |
| 146 | + exploreDescriptor(descriptor); |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +void exploreDescriptor(BLEDescriptor descriptor) { |
| 151 | + // print the UUID of the descriptor |
| 152 | + Serial.print("\t\tDescriptor "); |
| 153 | + Serial.print(descriptor.uuid()); |
| 154 | + |
| 155 | + // read the descriptor value |
| 156 | + descriptor.read(); |
| 157 | + |
| 158 | + // print out the value of the descriptor |
| 159 | + Serial.print(", value 0x"); |
| 160 | + printData(descriptor.value(), descriptor.valueLength()); |
| 161 | + |
| 162 | + Serial.println(); |
| 163 | +} |
| 164 | + |
| 165 | +void printData(const unsigned char data[], int length) { |
| 166 | + for (int i = 0; i < length; i++) { |
| 167 | + unsigned char b = data[i]; |
| 168 | + |
| 169 | + if (b < 16) { |
| 170 | + Serial.print("0"); |
| 171 | + } |
| 172 | + |
| 173 | + Serial.print(b, HEX); |
| 174 | + } |
| 175 | +} |
0 commit comments