嵌入式


BLE CLIENT

<pre><code>/** * A BLE client example that is rich in capabilities. * There is a lot new capabilities implemented. * author unknown * updated by chegewara */ #include "BLEDevice.h" //#include "BLEScan.h" // The remote service we wish to connect to. static BLEUUID serviceUUID("69400001-b5a3-f393-e0a9-e50e24dcca99"); // The characteristic of the remote service we are interested in. static BLEUUID writecharUUID("69400002-b5a3-f393-e0a9-e50e24dcca99"); // write no response static BLEUUID charUUID("69400003-b5a3-f393-e0a9-e50e24dcca99"); // notify //const std::string BLEName("XT-DE8BF9ABFD5A"); //const std::string BLEName("12345678"); const std::string BLEName("12345681"); //const std::string BLEName("12345679"); //const std::string BLEName("XT-EFC965DED19A"); //const std::string BLEName("XT-E85F8A5F8BB9"); std::string BLENameSet; static boolean doConnect = false; static boolean connected = false; static boolean doScan = false; static BLERemoteCharacteristic* pRemoteCharacteristic, *pRemoteWriteCharacteristic; static BLEAdvertisedDevice* myDevice; static void notifyCallback( BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) { /* Serial.print("Notify callback for characteristic "); Serial.print(pBLERemoteCharacteristic-&gt;getUUID().toString().c_str()); Serial.print(" of data length "); Serial.println(length); Serial.print("data: ");*/ if (pBLERemoteCharacteristic == pRemoteCharacteristic) { pData[length] = 0; Serial.print((char*)pData); } } class MyClientCallback : public BLEClientCallbacks { void onConnect(BLEClient* pclient) { } void onDisconnect(BLEClient* pclient) { connected = false; doConnect = true; Serial.println("onDisconnect"); } }; bool connectToServer() { Serial.print("Forming a connection to "); Serial.println(myDevice-&gt;getAddress().toString().c_str()); BLEClient* pClient = BLEDevice::createClient(); Serial.println(" - Created client"); pClient-&gt;setClientCallbacks(new MyClientCallback()); // Connect to the remove BLE Server. pClient-&gt;connect(myDevice); // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private) Serial.println(" - Connected to server"); // Obtain a reference to the service we are after in the remote BLE server. BLERemoteService* pRemoteService = pClient-&gt;getService(serviceUUID); if (pRemoteService == nullptr) { Serial.print("Failed to find our service UUID: "); Serial.println(serviceUUID.toString().c_str()); pClient-&gt;disconnect(); return false; } Serial.println(" - Found our service"); // Obtain a reference to the characteristic in the service of the remote BLE server. pRemoteCharacteristic = pRemoteService-&gt;getCharacteristic(charUUID); if (pRemoteCharacteristic == nullptr) { Serial.print("Failed to find our characteristic UUID: "); Serial.println(charUUID.toString().c_str()); pClient-&gt;disconnect(); return false; } Serial.println(" - Found our characteristic"); // Read the value of the characteristic. if(pRemoteCharacteristic-&gt;canRead()) { std::string value = pRemoteCharacteristic-&gt;readValue(); Serial.print("The characteristic value was: "); Serial.println(value.c_str()); } if(pRemoteCharacteristic-&gt;canNotify()) Serial.println("The characteristic can notify"); pRemoteCharacteristic-&gt;registerForNotify(notifyCallback); // Obtain a reference to the characteristic in the service of the remote BLE server. pRemoteWriteCharacteristic = pRemoteService-&gt;getCharacteristic(writecharUUID); if (pRemoteWriteCharacteristic == nullptr) { Serial.print("Failed to find our characteristic UUID: "); Serial.println(writecharUUID.toString().c_str()); pClient-&gt;disconnect(); return false; } Serial.println(" - Found our characteristic"); if(pRemoteWriteCharacteristic-&gt;canWriteNoResponse()) Serial.println("The characteristic can write no response"); connected = true; } /** * Scan for BLE servers and find the first one that advertises the service we are looking for. */ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { /** * Called for each advertising BLE server. */ void onResult(BLEAdvertisedDevice advertisedDevice) { Serial.print("BLE Advertised Device found: "); Serial.println(advertisedDevice.toString().c_str()); // We have found a device, let us now see if it contains the service we are looking for. if ((advertisedDevice.getName() == BLENameSet) &amp;&amp; advertisedDevice.haveServiceUUID() &amp;&amp; advertisedDevice.isAdvertisingService(serviceUUID)) { BLEDevice::getScan()-&gt;stop(); myDevice = new BLEAdvertisedDevice(advertisedDevice); doConnect = true; doScan = true; } // Found our server } // onResult }; // MyAdvertisedDeviceCallbacks void setup() { Serial.begin(115200); Serial.println("Starting Arduino BLE Client application..."); Serial.println("Please select ble device first..."); while(1) { String setname = Serial.readStringUntil('\n'); if(setname.length()) { BLENameSet = setname.c_str(); break; } } Serial.println(String("ble name:") + BLENameSet.c_str()); BLEDevice::init(""); // Retrieve a Scanner and set the callback we want to use to be informed when we // have detected a new device. Specify that we want active scanning and start the // scan to run for 5 seconds. BLEScan* pBLEScan = BLEDevice::getScan(); pBLEScan-&gt;setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); pBLEScan-&gt;setInterval(1349); pBLEScan-&gt;setWindow(449); pBLEScan-&gt;setActiveScan(true); pBLEScan-&gt;start(5, false); } // End of setup. byte serial_recv_buf[512]; int serial_recv_len = 0; // This is the Arduino main loop function. void loop() { serial_recv_len = 0; while (Serial.available() &gt; 0) { serial_recv_buf[serial_recv_len++] = Serial.read(); } // If the flag "doConnect" is true then we have scanned for and found the desired // BLE Server with which we wish to connect. Now we connect to it. Once we are // connected we set the connected flag to be true. if (doConnect == true) { if (connectToServer()) { Serial.println("We are now connected to the BLE Server."); } else { Serial.println("We have failed to connect to the server; there is nothin more we will do."); } doConnect = false; } // If we are connected to a peer BLE Server, update the characteristic each time we are reached // with the current time since boot. if (connected) { if(serial_recv_len &gt; 0){ pRemoteWriteCharacteristic-&gt;writeValue(serial_recv_buf, serial_recv_len); } }/*else if(doScan){ BLEDevice::getScan()-&gt;start(0); // this is just eample to start scan after disconnect, most likely there is better way to do it in arduino }*/ delay(10); } // End of loop</code></pre>

页面列表

ITEM_HTML