|
| 1 | + |
| 2 | +#include "ArduinoBLE.h" |
| 3 | +#include "BLEAttribute.h" |
| 4 | +#include "BLECharacteristicImp.h" |
| 5 | +#include "BLEProfileManager.h" |
| 6 | + |
| 7 | +// LED pin |
| 8 | +#define LED_PIN 13 |
| 9 | + |
| 10 | +void setup() { |
| 11 | + Serial1.begin(115200); |
| 12 | + Serial1.println("test---"); |
| 13 | + |
| 14 | + // set LED pin to output mode |
| 15 | + pinMode(LED_PIN, OUTPUT); |
| 16 | + |
| 17 | + // begin initialization |
| 18 | + BLE.begin(); |
| 19 | + Serial1.println(BLE.address()); |
| 20 | + |
| 21 | + BLE.startScanning("LED"); |
| 22 | +} |
| 23 | + |
| 24 | +void controlLed(BLEDevice &peripheral) |
| 25 | +{ |
| 26 | + static bool discovered = false; |
| 27 | + // connect to the peripheral |
| 28 | + Serial1.print("Connecting ... "); |
| 29 | + Serial1.println(peripheral.address()); |
| 30 | + |
| 31 | + if (peripheral.connect()) |
| 32 | + { |
| 33 | + Serial1.print("Connected: "); |
| 34 | + Serial1.println(peripheral.address()); |
| 35 | + } |
| 36 | + else |
| 37 | + { |
| 38 | + Serial1.println("Failed to connect!"); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + peripheral.discoverAttributes(); |
| 43 | + |
| 44 | + BLECharacteristic ledCharacteristic = peripheral.characteristic("19b10101-e8f2-537e-4f6c-d104768a1214"); |
| 45 | + |
| 46 | + if (!ledCharacteristic) |
| 47 | + { |
| 48 | + //peripheral.disconnect(); |
| 49 | + while(1) |
| 50 | + { |
| 51 | + Serial1.println("Peripheral does not have LED characteristic!"); |
| 52 | + delay(5000); |
| 53 | + } |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + unsigned char ledstate = 0; |
| 59 | + |
| 60 | + discovered = false; |
| 61 | + while (peripheral.connected()) |
| 62 | + { |
| 63 | + if (ledstate == 1) |
| 64 | + { |
| 65 | + ledstate = 0; |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + ledstate = 1; |
| 70 | + } |
| 71 | + ledCharacteristic.write(&ledstate, sizeof(ledstate)); |
| 72 | + delay(5000); |
| 73 | + } |
| 74 | + Serial1.print("Disconnected"); |
| 75 | + Serial1.println(peripheral.address()); |
| 76 | +} |
| 77 | + |
| 78 | +void loop() { |
| 79 | + //pr_debug(LOG_MODULE_BLE, "%s-%d",__FUNCTION__, __LINE__); |
| 80 | + BLEDevice peripheral = BLE.available(); |
| 81 | + //pr_debug(LOG_MODULE_BLE, "%s-%d",__FUNCTION__, __LINE__); |
| 82 | + if (peripheral) |
| 83 | + { |
| 84 | + Serial1.println(peripheral.address()); |
| 85 | + BLE.stopScanning(); |
| 86 | + delay (1000); |
| 87 | + // central connected to peripheral |
| 88 | + controlLed(peripheral); |
| 89 | + delay (4000); |
| 90 | + BLE.startScanning("LED"); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | + |
0 commit comments