|
| 1 | +// |
| 2 | +// (C) Dr. Michael 'Mickey' Lauer <mickey@vanille-media.de> |
| 3 | +// |
| 4 | + |
| 5 | +#include <Arduino.h> |
| 6 | +#include <NimBLEDevice.h> |
| 7 | + |
| 8 | +#if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM <= 0 |
| 9 | +# error "CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM must be set to 1 or greater" |
| 10 | +#endif |
| 11 | + |
| 12 | +// See the following for generating UUIDs: |
| 13 | +// https://www.uuidgenerator.net/ |
| 14 | + |
| 15 | +// The remote service we wish to connect to. |
| 16 | +static NimBLEUUID serviceUUID("dcbc7255-1e9e-49a0-a360-b0430b6c6905"); |
| 17 | +// The characteristic of the remote service we are interested in. |
| 18 | +static NimBLEUUID charUUID("371a55c8-f251-4ad2-90b3-c7c195b049be"); |
| 19 | + |
| 20 | +#define L2CAP_CHANNEL 150 |
| 21 | +#define L2CAP_MTU 5000 |
| 22 | + |
| 23 | +const NimBLEAdvertisedDevice* theDevice = NULL; |
| 24 | +NimBLEClient* theClient = NULL; |
| 25 | +NimBLEL2CAPChannel* theChannel = NULL; |
| 26 | + |
| 27 | +size_t bytesSent = 0; |
| 28 | +size_t bytesReceived = 0; |
| 29 | +size_t numberOfSeconds = 0; |
| 30 | + |
| 31 | +class L2CAPChannelCallbacks : public NimBLEL2CAPChannelCallbacks { |
| 32 | + public: |
| 33 | + void onConnect(NimBLEL2CAPChannel* channel) { Serial.println("L2CAP connection established"); } |
| 34 | + |
| 35 | + void onMTUChange(NimBLEL2CAPChannel* channel, uint16_t mtu) { Serial.printf("L2CAP MTU changed to %d\n", mtu); } |
| 36 | + |
| 37 | + void onRead(NimBLEL2CAPChannel* channel, std::vector<uint8_t>& data) { |
| 38 | + Serial.printf("L2CAP read %d bytes\n", data.size()); |
| 39 | + } |
| 40 | + void onDisconnect(NimBLEL2CAPChannel* channel) { Serial.println("L2CAP disconnected"); } |
| 41 | +} L2Callbacks; |
| 42 | + |
| 43 | +class ClientCallbacks : public NimBLEClientCallbacks { |
| 44 | + void onConnect(NimBLEClient* pClient) { |
| 45 | + Serial.println("GAP connected"); |
| 46 | + pClient->setDataLen(251); |
| 47 | + |
| 48 | + theChannel = NimBLEL2CAPChannel::connect(pClient, L2CAP_CHANNEL, L2CAP_MTU, &L2Callbacks); |
| 49 | + } |
| 50 | + |
| 51 | + void onDisconnect(NimBLEClient* pClient, int reason) { |
| 52 | + printf("GAP disconnected (reason: %d)\n", reason); |
| 53 | + theDevice = NULL; |
| 54 | + theChannel = NULL; |
| 55 | + NimBLEDevice::getScan()->start(5 * 1000, true); |
| 56 | + } |
| 57 | +} clientCallbacks; |
| 58 | + |
| 59 | +class ScanCallbacks : public NimBLEScanCallbacks { |
| 60 | + void onResult(const NimBLEAdvertisedDevice* advertisedDevice) { |
| 61 | + if (theDevice) { |
| 62 | + return; |
| 63 | + } |
| 64 | + Serial.printf("BLE Advertised Device found: %s\n", advertisedDevice->toString().c_str()); |
| 65 | + |
| 66 | + if (!advertisedDevice->haveServiceUUID()) { |
| 67 | + return; |
| 68 | + } |
| 69 | + if (!advertisedDevice->isAdvertisingService(serviceUUID)) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + Serial.println("Found the device we're interested in!"); |
| 74 | + NimBLEDevice::getScan()->stop(); |
| 75 | + |
| 76 | + // Hand over the device to the other task |
| 77 | + theDevice = advertisedDevice; |
| 78 | + } |
| 79 | +} scanCallbacks; |
| 80 | + |
| 81 | +void setup() { |
| 82 | + Serial.begin(115200); |
| 83 | + Serial.println("Starting L2CAP client example"); |
| 84 | + |
| 85 | + NimBLEDevice::init("L2CAP-Client"); |
| 86 | + NimBLEDevice::setMTU(BLE_ATT_MTU_MAX); |
| 87 | + |
| 88 | + auto scan = NimBLEDevice::getScan(); |
| 89 | + scan->setScanCallbacks(&scanCallbacks); |
| 90 | + scan->setInterval(1349); |
| 91 | + scan->setWindow(449); |
| 92 | + scan->setActiveScan(true); |
| 93 | + scan->start(25 * 1000, false); |
| 94 | +} |
| 95 | + |
| 96 | +void loop() { |
| 97 | + static uint8_t sequenceNumber = 0; |
| 98 | + static unsigned long firstBytesTime = 0; |
| 99 | + auto now = millis(); |
| 100 | + |
| 101 | + if (!theDevice) { |
| 102 | + delay(1000); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + if (!theClient) { |
| 107 | + theClient = NimBLEDevice::createClient(); |
| 108 | + theClient->setConnectionParams(6, 6, 0, 42); |
| 109 | + theClient->setClientCallbacks(&clientCallbacks); |
| 110 | + if (!theClient->connect(theDevice)) { |
| 111 | + Serial.println("Error: Could not connect to device"); |
| 112 | + return; |
| 113 | + } |
| 114 | + delay(2000); |
| 115 | + } |
| 116 | + |
| 117 | + if (!theChannel) { |
| 118 | + Serial.println("l2cap channel not initialized"); |
| 119 | + delay(2000); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + if (!theChannel->isConnected()) { |
| 124 | + Serial.println("l2cap channel not connected\n"); |
| 125 | + delay(2000); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + std::vector<uint8_t> data(5000, sequenceNumber++); |
| 130 | + if (theChannel->write(data)) { |
| 131 | + if (bytesSent == 0) { |
| 132 | + firstBytesTime = now; |
| 133 | + } |
| 134 | + bytesSent += data.size(); |
| 135 | + if (now - firstBytesTime > 1000) { |
| 136 | + int bytesSentPerSeconds = bytesSent / ((now - firstBytesTime) / 1000); |
| 137 | + printf("Bandwidth: %d b/sec = %d KB/sec\n", bytesSentPerSeconds, bytesSentPerSeconds / 1024); |
| 138 | + } |
| 139 | + } else { |
| 140 | + Serial.println("failed to send!"); |
| 141 | + abort(); |
| 142 | + } |
| 143 | +} |
0 commit comments