SHARE
TWEET

Untitled

a guest Mar 25th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <BLEDevice.h>
  2. #include <BLEUtils.h>
  3. #include <BLEScan.h>
  4. #include <BLEAdvertisedDevice.h>//BLE lib include
  5. //declare a BLE scan object
  6. class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
  7. {void onResult(BLEAdvertisedDevice advertisedDevice)
  8.   {//Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
  9.     }
  10. };
  11. int scanTime=5;//set scantime, if time too less, you will lost some beacons that not discovered
  12. void setup()
  13. {
  14.   Serial.begin(9600);
  15. }
  16.  
  17. void loop()
  18. {
  19.   Serial.println("Beacon Scanning....");
  20.   BLEDevice::init("");//clear beacon list
  21.   BLEScan* pBLEScan = BLEDevice::getScan(); //create scan
  22.   pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  23.   pBLEScan->setActiveScan(true); //start to scan
  24.   BLEScanResults foundDevices = pBLEScan->start(scanTime);//wait for scanning
  25.   Serial.print("Found Beacons: ");
  26.   Serial.println(foundDevices.getCount());
  27.   Serial.println("End of Scanning");
  28.   int BeaconCount=foundDevices.getCount();
  29.   //list discover beacons data
  30.   Serial.println("Show Beacons content:");
  31.    for (int i=0; i<BeaconCount; i++)
  32.    {
  33.        BLEAdvertisedDevice d=foundDevices.getDevice(i);
  34.        Serial.print("Address=");
  35.        String BeaconAddress=d.getAddress().toString().c_str();
  36.        Serial.print(BeaconAddress);
  37.        int BeaconRSSI=d.getRSSI();
  38.        if (d.haveServiceUUID())//show UUID
  39.        {
  40.          Serial.print(",UUID=");
  41.          String BeaconUUID;
  42.          BeaconUUID=d.getServiceUUID().toString().c_str();
  43.          Serial.print(BeaconUUID);
  44.        }
  45.  
  46.        if (d.haveName())//Show name
  47.        {
  48.         Serial.printf(",name=");
  49.         String BeaconName=d.getName().c_str();
  50.         Serial.print(BeaconName);
  51.        }      
  52.        Serial.print(",RSSI=");
  53.        Serial.println(BeaconRSSI);//Show RSSI
  54.  
  55.        //-----add some codes here. ex
  56.        //if beacon's address equal to the address you want, and RSSI bigger than -50, then make some noise
  57.        //-----
  58.    }
  59.  
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top