public static void SetUp_SensorsLists() { InitSensors(); Debug.WriteLine("Begin SetUp_SensorsLists() "); try { DisableSensorWithDisableNotifications = false; if (System.Threading.Interlocked.Increment(ref SetUpRunTimes) == 1) { SensorsCharacteristicsList = new CC2650SensorTag[NUM_SENSORS_ALL]; for (int i = 0; i < SensorsCharacteristicsList.Length; i++) { SensorsCharacteristicsList[i] = null; } ServiceList = new GattDeviceService[NUM_SENSORS_ALL]; for (int i = 0; i < ServiceList.Length; i++) { ServiceList[i] = null; } ActiveCharacteristicNotifications = new GattCharacteristic[NUM_SENSORS_ALL]; for (int i = 0; i < ActiveCharacteristicNotifications.Length; i++) { ActiveCharacteristicNotifications[i] = null; } } IncProgressCounter(); } catch (Exception ex) { Debug.WriteLine("Error: SetUp() - " + ex.Message); } Debug.WriteLine("End SetUp_SensorsLists() "); }
private async Task initSensor(CC2650SensorTag.SensorIndexes sensorIndx) { GattDeviceService gattService = CC2650SensorTag.ServiceList[(int)sensorIndx]; if (gattService != null) { CC2650SensorTag temp = new CC2650SensorTag(gattService, sensorIndx, CallMeBack); if (temp != null) { await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal, () => { // your code to access the UI thread widgets InitSensorCallback?.Invoke(sensorIndx); }); } //Sensor Specific NonUI actions post init switch (sensorIndx) { case (CC2650SensorTag.SensorIndexes.IR_SENSOR): break; case (CC2650SensorTag.SensorIndexes.MOVEMENT): temp.setSensorPeriod(1000); break; case (CC2650SensorTag.SensorIndexes.HUMIDITY): break; case (CC2650SensorTag.SensorIndexes.OPTICAL): break;; case (CC2650SensorTag.SensorIndexes.BAROMETRIC_PRESSURE):; break; case (CC2650SensorTag.SensorIndexes.KEYS): break; default: break; } } CC2650SensorTag.IncProgressCounter(); }
private async void EventTimerCallback(object state) { counter++; long currentCount = System.Threading.Interlocked.Read(ref CC2650SensorTag.EventCount); long diff = currentCount - LastEventCount; LastEventCount = currentCount; if ((counter > CC2650SensorTag.NumTimerEventsToWaitBeforeTurningOffUpdates) && (!CC2650SensorTag.SetSensorsManualMode)) { //Give sensors a change to switchto manual mode. CC2650SensorTag.SetSensorsManualMode = true; return; } if (sampleFile != null) { await Windows.Storage.FileIO.AppendTextAsync(sampleFile, counter.ToString() + " " + diff.ToString() + "\r\n"); } if (System.Threading.Interlocked.Read(ref updating) == 1) { return; } if (CC2650SensorTag.PeriodicUpdatesOnly) { if (CC2650SensorTag.SetSensorsManualMode) { if (((counter) % CC2650SensorTag.Period) == 0) { System.Threading.Interlocked.Exchange(ref updating, 1); await CC2650SensorTag.GetBatteryLevel(); await CC2650SensorTag.ReadAllSensors(); System.Threading.Interlocked.Exchange(ref updating, 0); } } } }
//Watcher for Bluetooth LE Services public void StartBLEWatcher(Page mainPage2, DeviceInfoDel SetDevInfo, SetupProgressDel setUpProgress2) { NainPage2 = mainPage2; HasOKd = false; int targetNoServices = CC2650SensorTag.NUM_SENSORS_TO_TEST; if (CC2650SensorTag.Use_DEVICE_BATTERY_SERVICE) { targetNoServices++; } if (CC2650SensorTag.Use_UUID_PROPERTIES_SERVICE) { targetNoServices++; } ManualResetEvent firstServiceStartedManualResetEvent = new ManualResetEvent(false); CC2650SensorTag.EventCount = 0; CC2650SensorTag.SetSensorsManualMode = false; //Init values for log long start = 0; counter = 0; System.Threading.Interlocked.Exchange(ref updating, 0); // Hook up handlers for the watcher events before starting the watcher OnBLEAdded = async(watcher, deviceInfo) => { if (System.Threading.Interlocked.Increment(ref notifiedServices) == 1) { //await Task.Run(async () => await NainPage2.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { //Set up event logging StorageFolder storageFolder = KnownFolders.DocumentsLibrary; sampleFile = await storageFolder.CreateFileAsync("sample.log", CreationCollisionOption.ReplaceExisting); if (CC2650SensorTag.DeviceAltSensorNames.Contains(deviceInfo.Name)) { setUpProgress2(); Debug.WriteLine("OnBLEAdded1 On UI Thread: " + deviceInfo.Id); GattDeviceService service = null; try { HasOKd = true; service = await GattDeviceService.FromIdAsync(deviceInfo.Id); SetDevInfo(deviceInfo); } catch (Exception ex) { HasOKd = false; string msg = ex.Message; Debug.WriteLine("Error: OnBLEAdded2() on UI Thread(): " + deviceInfo.Id + " " + msg); return; } if (service != null) { CC2650SensorTag.SensorIndexes sensorIndx = CC2650SensorTag.SensorIndexes.NOTFOUND; string svcGuid = service.Uuid.ToString().ToUpper(); Debug.WriteLine("Found Service: " + svcGuid); //Let other services start now firstServiceStartedManualResetEvent.Set(); // Add this service to the list if it conforms to the TI-GUID pattern for most sensors if (svcGuid == CC2650SensorTag.DEVICE_BATTERY_SERVICE) { CC2650SensorTag.SetUpBattery(service); byte[] bytes = await CC2650SensorTag.GetBatteryLevel(); } else if (svcGuid == CC2650SensorTag.UUID_PROPERTIES_SERVICE.ToUpper()) { CC2650SensorTag.DevicePropertyService = service; await CC2650SensorTag.GetProperties(false); } else { if (svcGuid == CC2650SensorTag.IO_SENSOR_GUID_STR) { sensorIndx = CC2650SensorTag.SensorIndexes.IO_SENSOR; } else if (svcGuid == CC2650SensorTag.REGISTERS_GUID_STR) { sensorIndx = CC2650SensorTag.SensorIndexes.REGISTERS; } // otherwise, if this is the GUID for the KEYS, then handle it special else if (svcGuid == CC2650SensorTag.BUTTONS_GUID_STR) { sensorIndx = CC2650SensorTag.SensorIndexes.KEYS; } else if (svcGuid.StartsWith(CC2650SensorTag.SENSOR_GUID_PREFIX)) { // The character at this position indicates the index into the ServiceList // container that we want to save this service to. The rest of this program // assumes that specific sensor types are at specific indexes in this array int Indx = (svcGuid[6] - '0'); sensorIndx = CC2650SensorTag.GetSensorIndex(Indx); } // If the index is legal and a service hasn't already been cached, then // cache this service in our ServiceList if (((sensorIndx >= 0) && (sensorIndx <= (CC2650SensorTag.SensorIndexes)CC2650SensorTag.SENSOR_MAX)) && (CC2650SensorTag.ServiceList[(int)sensorIndx] == null)) { CC2650SensorTag.ServiceList[(int)sensorIndx] = service; await initSensor(sensorIndx); System.Threading.Interlocked.Increment(ref discoveredServices); } else { } } } else //Service is Null: Need to let other services attempt start now { firstServiceStartedManualResetEvent.Set(); } CC2650SensorTag.IncProgressCounter(); } }); } else { firstServiceStartedManualResetEvent.WaitOne(); await Task.Run(async() => //await NainPage2.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => { if (CC2650SensorTag.DeviceAltSensorNames.Contains(deviceInfo.Name)) { CC2650SensorTag.IncProgressCounter(); Debug.WriteLine("OnBLEAdded2() Not on UI thread: " + deviceInfo.Id); GattDeviceService service = null; try { service = await GattDeviceService.FromIdAsync(deviceInfo.Id); } catch (Exception ex) { string msg = ex.Message; Debug.WriteLine("Error: OnBLEAdded2() Not on UI Thread: " + deviceInfo.Id + " " + msg); return; } if (service != null) { CC2650SensorTag.SensorIndexes sensorIndx = CC2650SensorTag.SensorIndexes.NOTFOUND; string svcGuid = service.Uuid.ToString().ToUpper(); Debug.WriteLine("Found Service: " + svcGuid); // Add this service to the list if it conforms to the TI-GUID pattern for most sensors if (svcGuid == CC2650SensorTag.DEVICE_BATTERY_SERVICE) { CC2650SensorTag.SetUpBattery(service); byte[] bytes = await CC2650SensorTag.GetBatteryLevel(); return; } else if (svcGuid == CC2650SensorTag.UUID_PROPERTIES_SERVICE.ToUpper()) { CC2650SensorTag.DevicePropertyService = service; await CC2650SensorTag.GetProperties(false); return; } else if (svcGuid == CC2650SensorTag.IO_SENSOR_GUID_STR) { sensorIndx = CC2650SensorTag.SensorIndexes.IO_SENSOR; } else if (svcGuid == CC2650SensorTag.REGISTERS_GUID_STR) { sensorIndx = CC2650SensorTag.SensorIndexes.REGISTERS; } // otherwise, if this is the GUID for the KEYS, then handle it special else if (svcGuid == CC2650SensorTag.BUTTONS_GUID_STR) { sensorIndx = CC2650SensorTag.SensorIndexes.KEYS; } else if (svcGuid.StartsWith(CC2650SensorTag.SENSOR_GUID_PREFIX)) { // The character at this position indicates the index into the ServiceList // container that we want to save this service to. The rest of this program // assumes that specific sensor types are at specific indexes in this array int Indx = (svcGuid[6] - '0'); sensorIndx = CC2650SensorTag.GetSensorIndex(Indx); } // If the index is legal and a service hasn't already been cached, then // cache this service in our ServiceList if (((sensorIndx >= 0) && (sensorIndx <= (CC2650SensorTag.SensorIndexes)CC2650SensorTag.SENSOR_MAX)) && (CC2650SensorTag.ServiceList[(int)sensorIndx] == null)) { CC2650SensorTag.ServiceList[(int)sensorIndx] = service; await initSensor(sensorIndx); System.Threading.Interlocked.Increment(ref discoveredServices); } else { } } CC2650SensorTag.IncProgressCounter(); } }); } }; OnBLEUpdated = async(watcher, deviceInfoUpdate) => { await Task.Run(() => Debug.WriteLine($"OnBLEUpdated: {deviceInfoUpdate.Id}")); }; OnBLERemoved = async(watcher, deviceInfoUpdate) => { await Task.Run(() => Debug.WriteLine("OnBLERemoved")); }; OnBLEEnumerationCompleted = async(watcher, obj) => { await Task.Run(() => { blewatcher.Stop(); UpdateButtons_WhenSensorsAreReady_CallBack?.Invoke(); Debug.WriteLine("blewatcher Stopped."); System.Threading.Interlocked.Exchange(ref CC2650SensorTag.EventCount, start); EventTimer = new Timer(EventTimerCallback, null, 0, (int)CC2650SensorTag.UpdatePeriod); CC2650SensorTag.IncProgressCounter(); Debug.WriteLine("OnBLEEnumerationCompleted"); }); }; string aqs = ""; if (CC2650SensorTag.ServiceSensors) { for (int ii = 0; ii < CC2650SensorTag.NUM_SENSORS_TO_TEST; ii++) { int i = CC2650SensorTag.FIRST_SENSOR + ii; CC2650SensorTag.SensorIndexes sensorIndx = (CC2650SensorTag.SensorIndexes)i; Guid BLE_GUID; Debug.WriteLine("NUMSENSORS " + sensorIndx.ToString()); if (sensorIndx == CC2650SensorTag.SensorIndexes.IO_SENSOR) { BLE_GUID = CC2650SensorTag.IO_SENSOR_GUID; } else if (sensorIndx == CC2650SensorTag.SensorIndexes.REGISTERS) { BLE_GUID = CC2650SensorTag.REGISTERS_GUID; } else if (sensorIndx != CC2650SensorTag.SensorIndexes.KEYS) { BLE_GUID = new Guid(CC2650SensorTag.UUIDBase[i] + CC2650SensorTag.SENSOR_GUID_SUFFFIX); } else { BLE_GUID = CC2650SensorTag.BUTTONS_GUID; } aqs += "(" + GattDeviceService.GetDeviceSelectorFromUuid(BLE_GUID) + ")"; if (ii < CC2650SensorTag.NUM_SENSORS_TO_TEST - 1) { aqs += " OR "; } } } if (CC2650SensorTag.Use_DEVICE_BATTERY_SERVICE) { if (CC2650SensorTag.ServiceSensors) { aqs += " OR "; } aqs += "(" + GattDeviceService.GetDeviceSelectorFromUuid(new Guid(CC2650SensorTag.DEVICE_BATTERY_SERVICE)) + ")"; } if (CC2650SensorTag.Use_UUID_PROPERTIES_SERVICE) { if ((CC2650SensorTag.ServiceSensors) || (CC2650SensorTag.Use_DEVICE_BATTERY_SERVICE)) { aqs += " OR "; } aqs += "(" + GattDeviceService.GetDeviceSelectorFromUuid(new Guid(CC2650SensorTag.UUID_PROPERTIES_SERVICE)) + ")"; } blewatcher = DeviceInformation.CreateWatcher(aqs); blewatcher.Added += OnBLEAdded; blewatcher.Updated += OnBLEUpdated; blewatcher.Removed += OnBLERemoved; blewatcher.EnumerationCompleted += OnBLEEnumerationCompleted;; blewatcher.Start(); CC2650SensorTag.IncProgressCounter(); }