/// <summary> /// Called to send a command to the BTLE device. This is a possible example of how tat would be done but the specifics /// will be based on device manufacturer specifications. /// </summary> /// <param name="commandCharacteristic"></param> /// <returns></returns> /// NOTE: Because unity uses the .c# 4.0 spec we don't have direct access to the async / Task functions /// but we will when running under the runtime as we will be in the WSA space #if UNITY_WSA_10_0 && !UNITY_EDITOR internal async Task Send(GattCharacteristicsWrapper commandCharacteristic) { byte[] message = new byte[CommandLength]; PopulateBuffer(message); IBuffer buff = message.AsBuffer(); await commandCharacteristic.Characteristic.WriteValueAsync(buff); }
/// <summary> /// Connect to the service and then list out the characteristics looking for our special characteristics by UUID /// </summary> /// <returns></returns> public bool ConnectService() { bool connectOk = false; ShowFeedback("Device service count: " + BTLEDevice.ServiceCount); ServiceCount = BTLEDevice.ServiceCount; connectOk = true; foreach (var service in BTLEDevice.Services) { ShowFeedback(service.Name); if (service.Name.Contains(sService_UUID)) { GattDeviceService = service; } } if (GattDeviceService != null) { ShowFeedback("Service found"); ShowFeedback("Characteristics count: " + GattDeviceService.Characteristics.Count); int retry = 5; while (GattDeviceService.Characteristics.Count == 0 && retry > 0) { Task.Delay(500); retry--; } if (GattDeviceService.Characteristics.Count == 0) { ShowFeedback("Characteristics count: " + GattDeviceService.Characteristics.Count); } ShowFeedback("Characteristics count: " + GattDeviceService.Characteristics.Count); foreach (var characteristic in GattDeviceService.Characteristics) { ShowFeedback("Characteristic Name: " + characteristic.Name); ShowFeedback("Characteristic UUID: " + characteristic.UUID); string UUID = characteristic.UUID.ToUpper(); // See if this is the command characteristic used for device control if (UUID.Contains(sCommandCharacteristic)) { ShowFeedback("Command characteristic found"); CommandCharacteristic = characteristic; ShowFeedback(CommandCharacteristic.Name); continue; } // See if this is the sensor (optional depending on device) characteristic if (UUID.Contains(sSensorCharacteristic)) { ShowFeedback("Sensor characteristic found"); SensorCharacteristic = characteristic; SensorCharacteristic.Characteristic.ValueChanged += SensorCharacteristic_ValueChanged; // If you don't call SetNotify you won't get the ValueChanged notifications. bool notifyOk = SensorCharacteristic.SetNotify(); continue; } } } return(connectOk); }