|  | 
|  | 1 | +/** | 
|  | 2 | + * Bluetooth Classic Example | 
|  | 3 | + * Scan for devices - asyncronously, print device as soon as found | 
|  | 4 | + * query devices for SPP - SDP profile | 
|  | 5 | + * connect to first device offering a SPP connection | 
|  | 6 | + *  | 
|  | 7 | + * Example python server: | 
|  | 8 | + * source: https://gist.github.com/ukBaz/217875c83c2535d22a16ba38fc8f2a91 | 
|  | 9 | + * | 
|  | 10 | + * Tested with Raspberry Pi onboard Wifi/BT, USB BT 4.0 dongles, USB BT 1.1 dongles,  | 
|  | 11 | + * 202202: does NOT work with USB BT 2.0 dongles when esp32 aduino lib is compiled with SSP support! | 
|  | 12 | + * see https://github.com/espressif/esp-idf/issues/8394 | 
|  | 13 | + *  | 
|  | 14 | + * use ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE in connect() if remote side requests 'RequireAuthentication': dbus.Boolean(True), | 
|  | 15 | + * use ESP_SPP_SEC_NONE or ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE in connect() if remote side has Authentication: False | 
|  | 16 | + */ | 
|  | 17 | + | 
|  | 18 | +#include <map> | 
|  | 19 | +#include <BluetoothSerial.h> | 
|  | 20 | + | 
|  | 21 | +#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) | 
|  | 22 | +#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it | 
|  | 23 | +#endif | 
|  | 24 | + | 
|  | 25 | +#if !defined(CONFIG_BT_SPP_ENABLED) | 
|  | 26 | +#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip. | 
|  | 27 | +#endif | 
|  | 28 | + | 
|  | 29 | +BluetoothSerial SerialBT; | 
|  | 30 | + | 
|  | 31 | + | 
|  | 32 | +#define BT_DISCOVER_TIME 10000 | 
|  | 33 | +esp_spp_sec_t sec_mask=ESP_SPP_SEC_NONE; // or ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE to request pincode confirmation | 
|  | 34 | +esp_spp_role_t role=ESP_SPP_ROLE_SLAVE; // or ESP_SPP_ROLE_MASTER | 
|  | 35 | + | 
|  | 36 | +// std::map<BTAddress, BTAdvertisedDeviceSet> btDeviceList; | 
|  | 37 | + | 
|  | 38 | +void setup() { | 
|  | 39 | + Serial.begin(115200); | 
|  | 40 | + if(! SerialBT.begin("ESP32test", true) ) { | 
|  | 41 | + Serial.println("========== serialBT failed!"); | 
|  | 42 | + abort(); | 
|  | 43 | + } | 
|  | 44 | + // SerialBT.setPin("1234"); // doesn't seem to change anything | 
|  | 45 | + // SerialBT.enableSSP(); // doesn't seem to change anything | 
|  | 46 | + | 
|  | 47 | + | 
|  | 48 | + Serial.println("Starting discoverAsync..."); | 
|  | 49 | + BTScanResults* btDeviceList = SerialBT.getScanResults(); // maybe accessing from different threads! | 
|  | 50 | + if (SerialBT.discoverAsync([](BTAdvertisedDevice* pDevice) { | 
|  | 51 | + // BTAdvertisedDeviceSet*set = reinterpret_cast<BTAdvertisedDeviceSet*>(pDevice); | 
|  | 52 | + // btDeviceList[pDevice->getAddress()] = * set; | 
|  | 53 | + Serial.printf(">>>>>>>>>>>Found a new device asynchronously: %s\n", pDevice->toString().c_str()); | 
|  | 54 | + } ) | 
|  | 55 | + ) { | 
|  | 56 | + delay(BT_DISCOVER_TIME); | 
|  | 57 | + Serial.print("Stopping discoverAsync... "); | 
|  | 58 | + SerialBT.discoverAsyncStop(); | 
|  | 59 | + Serial.println("discoverAsync stopped"); | 
|  | 60 | + delay(5000); | 
|  | 61 | + if(btDeviceList->getCount() > 0) { | 
|  | 62 | + BTAddress addr; | 
|  | 63 | + int channel=0; | 
|  | 64 | + Serial.println("Found devices:"); | 
|  | 65 | + for (int i=0; i < btDeviceList->getCount(); i++) { | 
|  | 66 | + BTAdvertisedDevice *device=btDeviceList->getDevice(i); | 
|  | 67 | + Serial.printf(" ----- %s %s %d\n", device->getAddress().toString().c_str(), device->getName().c_str(), device->getRSSI()); | 
|  | 68 | + std::map<int,std::string> channels=SerialBT.getChannels(device->getAddress()); | 
|  | 69 | + Serial.printf("scanned for services, found %d\n", channels.size()); | 
|  | 70 | + for(auto const &entry : channels) { | 
|  | 71 | + Serial.printf(" channel %d (%s)\n", entry.first, entry.second.c_str()); | 
|  | 72 | + } | 
|  | 73 | + if(channels.size() > 0) { | 
|  | 74 | + addr = device->getAddress(); | 
|  | 75 | + channel=channels.begin()->first; | 
|  | 76 | + } | 
|  | 77 | + } | 
|  | 78 | + if(addr) { | 
|  | 79 | + Serial.printf("connecting to %s - %d\n", addr.toString().c_str(), channel); | 
|  | 80 | + SerialBT.connect(addr, channel, sec_mask, role); | 
|  | 81 | + } | 
|  | 82 | + } else { | 
|  | 83 | + Serial.println("Didn't find any devices"); | 
|  | 84 | + } | 
|  | 85 | + } else { | 
|  | 86 | + Serial.println("Error on discoverAsync f.e. not workin after a \"connect\""); | 
|  | 87 | + } | 
|  | 88 | +} | 
|  | 89 | + | 
|  | 90 | + | 
|  | 91 | +String sendData="Hi from esp32!\n"; | 
|  | 92 | + | 
|  | 93 | +void loop() { | 
|  | 94 | + if(! SerialBT.isClosed() && SerialBT.connected()) { | 
|  | 95 | + if( SerialBT.write((const uint8_t*) sendData.c_str(),sendData.length()) != sendData.length()) { | 
|  | 96 | + Serial.println("tx: error"); | 
|  | 97 | + } else { | 
|  | 98 | + Serial.printf("tx: %s",sendData.c_str()); | 
|  | 99 | + } | 
|  | 100 | + if(SerialBT.available()) { | 
|  | 101 | + Serial.print("rx: "); | 
|  | 102 | + while(SerialBT.available()) { | 
|  | 103 | + int c=SerialBT.read(); | 
|  | 104 | + if(c >= 0) { | 
|  | 105 | + Serial.print((char) c); | 
|  | 106 | + } | 
|  | 107 | + } | 
|  | 108 | + Serial.println(); | 
|  | 109 | + } | 
|  | 110 | + } else { | 
|  | 111 | + Serial.println("not connected"); | 
|  | 112 | + } | 
|  | 113 | + delay(1000); | 
|  | 114 | +} | 
0 commit comments