From 3eb4b09459d752f9cd284e26dc65bb0a81f8df73 Mon Sep 17 00:00:00 2001 From: brian tetzlaff Date: Tue, 25 Jun 2019 13:38:30 -0700 Subject: [PATCH] Added BLE Radio Scanning. New bindings.js also needed. --- BLEServer/BLEServer/BLEServer.cpp | 55 +++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/BLEServer/BLEServer/BLEServer.cpp b/BLEServer/BLEServer/BLEServer.cpp index 7e186b6..14d8a88 100644 --- a/BLEServer/BLEServer/BLEServer.cpp +++ b/BLEServer/BLEServer/BLEServer.cpp @@ -25,8 +25,11 @@ #include using namespace Platform; +using namespace Windows::Foundation::Collections; using namespace Windows::Devices; +using namespace Windows::Devices::Radios; using namespace Windows::Data::Json; +using namespace concurrency; Bluetooth::Advertisement::BluetoothLEAdvertisementWatcher^ bleAdvertisementWatcher; auto devices = ref new Collections::Map(); @@ -463,10 +466,56 @@ int main(Array^ args) { // TODO manfuacturer data / flags / data sections ? writeObject(msg); }); + + // Added the ability to track the BLE Radio State + auto getRadiosOperation = Radio::GetRadiosAsync(); + create_task(getRadiosOperation).then( + [](task^> asyncInfo) + { + auto radios = asyncInfo.get(); + for (Windows::Devices::Radios::Radio^ radio : radios) + { + if (radio->Kind == RadioKind::Bluetooth) + { + if (radio->State == Windows::Devices::Radios::RadioState::On) + { + // Ble Radio On + JsonObject^ msg = ref new JsonObject(); + msg->Insert("_type", JsonValue::CreateStringValue("Start")); + writeObject(msg); + } + else + { + // Ble Radio Off + JsonObject^ msg = ref new JsonObject(); + msg->Insert("_type", JsonValue::CreateStringValue("Stop")); + writeObject(msg); + } + + radio->StateChanged += ref new Windows::Foundation::TypedEventHandler( + [](Windows::Devices::Radios::Radio^ sender, Platform::Object^ args) + { + if (sender->State == Windows::Devices::Radios::RadioState::On) + { + // Ble Radio Switched On + JsonObject^ msg = ref new JsonObject(); + msg->Insert("_type", JsonValue::CreateStringValue("Start")); + writeObject(msg); + } + else + { + // Ble Radio Switched Off + JsonObject^ msg = ref new JsonObject(); + msg->Insert("_type", JsonValue::CreateStringValue("Stop")); + writeObject(msg); + } + } + ); + } + } + } + ); - JsonObject^ msg = ref new JsonObject(); - msg->Insert("_type", JsonValue::CreateStringValue("Start")); - writeObject(msg); // Set STDIN / STDOUT to binary mode if ((_setmode(0, _O_BINARY) == -1) || (_setmode(1, _O_BINARY) == -1)) {