/// <summary> /// Starts the Heart rate service /// </summary> public override async Task Init() { await CreateServiceProvider(GattServiceUuids.HeartRate); // Preparing the Blood pressure characteristics var heartRateCharacteristics = PlainNotifyParameters; heartRateCharacteristics.UserDescription = "Heart Rates in Beats per Minute"; heartRateCharacteristics.PresentationFormats.Add( GattPresentationFormat.FromParts( Convert.ToByte(PresentationFormats.FormatTypes.Unsigned16BitInteger), PresentationFormats.Exponent, Convert.ToUInt16(PresentationFormats.Units.PeriodBeatsPerMinute), Convert.ToByte(PresentationFormats.NamespaceId.BluetoothSigAssignedNumber), PresentationFormats.Description)); // Create the heart rate characteristic for the service GattLocalCharacteristicResult result = await ServiceProvider.Service.CreateCharacteristicAsync( GattCharacteristicUuids.HeartRateMeasurement, PlainNotifyParameters); // Grab the characterist object from the service set it to the HeartRate property which is of a specfic Characteristic type GattLocalCharacteristic baseHeartRateMeasurement = null; GattServicesHelper.GetCharacteristicsFromResult(result, ref baseHeartRateMeasurement); if (baseHeartRateMeasurement != null) { HeartRateMeasurement = new Characteristics.HeartRateMeasurementCharacteristic(baseHeartRateMeasurement, this); } }
/// <summary> /// Starts the Alert Notification Services for Specific categories /// </summary> /// <param name="connectable">True, starts the service as Connectable. False, starts the service as only Discoverable</param> /// <param name="value">Category ID</param> public async void StartWithValue(bool connectable, byte[] value) { await CreateServiceProvider(GattServiceUuids.AlertNotification); GattLocalCharacteristicResult result = await ServiceProvider.Service.CreateCharacteristicAsync(GattCharacteristicUuids.SupportedNewAlertCategory, GetPlainReadParameterWithValue(value)); GattServicesHelper.GetCharacteristicsFromResult(result, ref supportNewAlert); result = await ServiceProvider.Service.CreateCharacteristicAsync(GattCharacteristicUuids.SupportUnreadAlertCategory, GetPlainReadParameterWithValue(value)); GattServicesHelper.GetCharacteristicsFromResult(result, ref supportUnreadAlert); result = await ServiceProvider.Service.CreateCharacteristicAsync(GattCharacteristicUuids.NewAlert, PlainNotifyParameters); GattServicesHelper.GetCharacteristicsFromResult(result, ref newAlert); result = await ServiceProvider.Service.CreateCharacteristicAsync(GattCharacteristicUuids.UnreadAlertStatus, PlainNotifyParameters); GattServicesHelper.GetCharacteristicsFromResult(result, ref unreadAlertStatus); result = await ServiceProvider.Service.CreateCharacteristicAsync(GattCharacteristicUuids.AlertNotificationControlPoint, PlainWriteOrWriteWithoutRespondsParameter); GattServicesHelper.GetCharacteristicsFromResult(result, ref alertNotificationCtrlPnt); if (alertNotificationCtrlPnt != null) { alertNotificationCtrlPnt.WriteRequested += AlertNotificationCtrlPntOnWriteRequested; } // Once all characteristics have been added - publish to the system base.Start(connectable); }
/// <summary> /// Notifies the client about the alert /// </summary> public async void NotifyValue() { IBuffer buffer = GattServicesHelper.ConvertValueToBuffer(1); await newAlert.NotifyValueAsync(buffer); await unreadAlertStatus.NotifyValueAsync(buffer); }
/// <summary> /// Asynchronous initialization /// </summary> /// <returns>Initialization Task</returns> public override async Task Init() { await CreateServiceProvider(GattServiceUuids.Battery); // Preparing the Battery Level characteristics GattLocalCharacteristicParameters batteryCharacteristicsParameters = PlainReadNotifyParameters; // Set the user descriptions batteryCharacteristicsParameters.UserDescription = "Battery Level percentage remaining"; // Add presentation format - 16-bit integer, with exponent 0, the unit is percentage, defined per Bluetooth SIG with Microsoft as descriptor batteryCharacteristicsParameters.PresentationFormats.Add( GattPresentationFormat.FromParts( Convert.ToByte(PresentationFormats.FormatTypes.Unsigned8BitInteger), PresentationFormats.Exponent, Convert.ToUInt16(PresentationFormats.Units.Percentage), Convert.ToByte(PresentationFormats.NamespaceId.BluetoothSigAssignedNumber), PresentationFormats.Description)); // Create the characteristic for the service GattLocalCharacteristicResult result = await ServiceProvider.Service.CreateCharacteristicAsync( GattCharacteristicUuids.BatteryLevel, batteryCharacteristicsParameters); // Grab the characterist object from the service set it to the BatteryLevel property which is of a specfic Characteristic type GattLocalCharacteristic baseBatteryLevel = null; GattServicesHelper.GetCharacteristicsFromResult(result, ref baseBatteryLevel); if (baseBatteryLevel != null) { BatteryLevel = new Characteristics.BatteryLevelCharacteristic(baseBatteryLevel, this); } }
/// <summary> /// Asynchronous initialization /// </summary> /// <returns>Initialization task</returns> public override async Task Init() { await CreateServiceProvider(GattServiceUuids.AlertNotification); // Please refer Category ID bit mask https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.alert_category_id_bit_mask.xml // The value 0x21 is interpreted as "Simple Alert and SMS bits set" var categoryBitMask = new AlertCategoryIdBitMask(AlertCategoryIdBitMask_0.SimpleAlert); // SupportedNewAlertCategory GattLocalCharacteristicResult result = await ServiceProvider.Service.CreateCharacteristicAsync( GattCharacteristicUuids.SupportedNewAlertCategory, GetPlainReadParameterWithValue(categoryBitMask.Value)); GattServicesHelper.GetCharacteristicsFromResult(result, ref supportNewAlert); // SupportedUnreadAlertCatagory result = await ServiceProvider.Service.CreateCharacteristicAsync( GattCharacteristicUuids.SupportUnreadAlertCategory, GetPlainReadParameterWithValue(categoryBitMask.Value)); GattServicesHelper.GetCharacteristicsFromResult(result, ref supportUnreadAlert); // NewAlert result = await ServiceProvider.Service.CreateCharacteristicAsync(GattCharacteristicUuids.NewAlert, PlainNotifyParameters); GattLocalCharacteristic newAlertCharacteristic = null; GattServicesHelper.GetCharacteristicsFromResult(result, ref newAlertCharacteristic); if (newAlertCharacteristic != null) { NewAlert = new Characteristics.NewAlertCharacteristic(newAlertCharacteristic, this); } // UnreadAlertStatus result = await ServiceProvider.Service.CreateCharacteristicAsync(GattCharacteristicUuids.UnreadAlertStatus, PlainNotifyParameters); GattLocalCharacteristic unreadAlertCharacteristic = null; GattServicesHelper.GetCharacteristicsFromResult(result, ref unreadAlertCharacteristic); if (unreadAlertCharacteristic != null) { UnreadAlertStatus = new Characteristics.UnreadAlertStatusCharacteristic(unreadAlertCharacteristic, this); } // Control Point result = await ServiceProvider.Service.CreateCharacteristicAsync( GattCharacteristicUuids.AlertNotificationControlPoint, PlainWriteOrWriteWithoutRespondsParameter); GattLocalCharacteristic alertNotificationControlPointCharacteristic = null; GattServicesHelper.GetCharacteristicsFromResult(result, ref alertNotificationControlPointCharacteristic); if (alertNotificationControlPointCharacteristic != null) { AlertNotificationControlPoint = new Characteristics.AlertNotificationControlPointCharacteristic(alertNotificationControlPointCharacteristic, this); } }
/// <summary> /// Starts the Current time service /// </summary> /// <param name="connectable">True, starts the service as Connectable. False, starts the service as only Discoverable</param> public override async void Start(bool connectable) { await CreateServiceProvider(GattServiceUuids.CurrentTime); GattLocalCharacteristicResult result = await ServiceProvider.Service.CreateCharacteristicAsync(GattCharacteristicUuids.CurrentTime, PlainReadNotifyParameters); GattServicesHelper.GetCharacteristicsFromResult(result, ref currentTime); if (currentTime != null) { currentTime.ReadRequested += ReadCharacteristicReadRequested; } base.Start(connectable); }
/// <summary> /// Asynchronous initialization /// </summary> /// <returns>Initialization Task</returns> public override async Task Init() { await CreateServiceProvider(GattServiceUuids.CurrentTime); GattLocalCharacteristicResult result = await ServiceProvider.Service.CreateCharacteristicAsync( GattCharacteristicUuids.CurrentTime, PlainReadNotifyParameters); GattLocalCharacteristic currentTimeCharacterisitic = null; GattServicesHelper.GetCharacteristicsFromResult(result, ref currentTimeCharacterisitic); if (currentTimeCharacterisitic != null) { CurrentTime = new Characteristics.CurrentTimeCharacteristic(currentTimeCharacterisitic, this); } }
/// <summary> /// Event handler for notifying the current time characteristics value /// </summary> public async void NotifyValue() { await currentTime.NotifyValueAsync(GattServicesHelper.ConvertValueToBuffer(DateTime.Now)); }
/// <summary> /// Event handler for reading Current time /// </summary> /// <param name="sender">The source of the Write request</param> /// <param name="args">Details about the request</param> private async void ReadCharacteristicReadRequested(GattLocalCharacteristic sender, GattReadRequestedEventArgs args) { var request = await args.GetRequestAsync(); request.RespondWithValue(GattServicesHelper.ConvertValueToBuffer(DateTime.Now)); }
/// <summary> /// Override so we can update the value when the value is read /// </summary> private void UpdateValue() { int readValue = rand.Next(1, 20); Value = GattServicesHelper.ConvertValueToBuffer(Convert.ToByte(readValue)); }
/// <summary> /// Asynchronous initialization /// </summary> /// <returns>Initialization Task</returns> public override async Task Init() { await CreateServiceProvider(GattServiceUuids.BloodPressure); // Preparing the Blood pressure characteristics var bloodPressureCharacteristics = PlainIndicateParameters; bloodPressureCharacteristics.UserDescription = "Blood Pressure in mm Hg"; bloodPressureCharacteristics.PresentationFormats.Add( GattPresentationFormat.FromParts( Convert.ToByte(PresentationFormats.FormatTypes.OpaqueStructure), PresentationFormats.Exponent, Convert.ToUInt16(PresentationFormats.Units.PressureMilliMetreofmercury), Convert.ToByte(PresentationFormats.NamespaceId.BluetoothSigAssignedNumber), PresentationFormats.Description)); // Create the blood pressure measurement characteristic for the service GattLocalCharacteristicResult result = await ServiceProvider.Service.CreateCharacteristicAsync( GattCharacteristicUuids.BloodPressureMeasurement, bloodPressureCharacteristics); // Grab the characterist object from the service GattLocalCharacteristic baseBloodPressureMeasurement = null; GattServicesHelper.GetCharacteristicsFromResult(result, ref baseBloodPressureMeasurement); if (baseBloodPressureMeasurement != null) { BloodPressureMeasurement = new Characteristics.BloodPressureMeasurementCharacteristic(baseBloodPressureMeasurement, this); } result = null; // Preparing the Blood pressure feature characteristics var bloodPressureFeatureCharacteristics = PlainReadParameter; bloodPressureFeatureCharacteristics.UserDescription = "The Blood Pressure Feature characteristic is used to describe the supported features of the Blood Pressure Sensor."; bloodPressureFeatureCharacteristics.PresentationFormats.Add( GattPresentationFormat.FromParts( Convert.ToByte(PresentationFormats.FormatTypes.Unsigned16BitInteger), PresentationFormats.Exponent, Convert.ToUInt16(PresentationFormats.Units.Unitless), Convert.ToByte(PresentationFormats.NamespaceId.BluetoothSigAssignedNumber), PresentationFormats.Description)); // Create the blood pressure measurement characteristic for the service result = await ServiceProvider.Service.CreateCharacteristicAsync( GattCharacteristicUuids.BloodPressureFeature, bloodPressureFeatureCharacteristics); // Grab the characterist object from the service GattLocalCharacteristic baseBloodPressureFeature = null; GattServicesHelper.GetCharacteristicsFromResult(result, ref baseBloodPressureFeature); if (baseBloodPressureFeature != null) { BloodPressureFeature = new Characteristics.BloodPressureFeatureCharacteristic(baseBloodPressureFeature, this); } }
/// <summary> /// Asynchronous initialization /// </summary> /// <returns>Initialization Task</returns> public override async Task Init() { var serviceData = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, }; ServiceData = WindowsRuntimeBuffer.Create(serviceData, 0, serviceData.Length, serviceData.Length); await CreateServiceProvider(MSFTServiceUuid); GattLocalCharacteristicResult result = null; // Prepare the Read Characteristic GattLocalCharacteristicParameters readParam = PlainReadParameter; readParam.UserDescription = "Microsoft Read characteristic"; // Add presentation format - 16-bit integer, with exponent 0, the unit is percentage, defined per Bluetooth SIG with Microsoft as descriptor readParam.PresentationFormats.Add( GattPresentationFormat.FromParts( Convert.ToByte(PresentationFormats.FormatTypes.Signed32BitInteger), PresentationFormats.Exponent, Convert.ToUInt16(PresentationFormats.Units.Unitless), Convert.ToByte(PresentationFormats.NamespaceId.BluetoothSigAssignedNumber), PresentationFormats.Description)); GattLocalCharacteristic baseReadChar = null; result = await ServiceProvider.Service.CreateCharacteristicAsync(MSFTReadChar, readParam); GattServicesHelper.GetCharacteristicsFromResult(result, ref baseReadChar); if (baseReadChar != null) { ReadCharacteristic = new Characteristics.MicrosoftReadCharacteristic(baseReadChar, this); } result = null; // Prepare the Write Characteristic GattLocalCharacteristicParameters writeParam = PlainWriteOrWriteWithoutRespondsParameter; writeParam.UserDescription = "Microsoft Write characteristic"; // Add presentation format - 16-bit integer, with exponent 0, the unit is percentage, defined per Bluetooth SIG with Microsoft as descriptor writeParam.PresentationFormats.Add( GattPresentationFormat.FromParts( Convert.ToByte(PresentationFormats.FormatTypes.UTF8String), PresentationFormats.Exponent, Convert.ToUInt16(PresentationFormats.Units.Unitless), Convert.ToByte(PresentationFormats.NamespaceId.BluetoothSigAssignedNumber), PresentationFormats.Description)); GattLocalCharacteristic baseWriteChar = null; result = await ServiceProvider.Service.CreateCharacteristicAsync(MSFTWriteChar, writeParam); GattServicesHelper.GetCharacteristicsFromResult(result, ref baseWriteChar); if (baseWriteChar != null) { WriteCharacteristic = new Characteristics.MicrosoftWriteCharacteristic(baseWriteChar, this); } result = null; // Prepare the Notify Characteristic GattLocalCharacteristicParameters notifyParam = PlainReadNotifyParameters; notifyParam.UserDescription = "Microsoft Notify characteristic"; // Add presentation format - string, the unit is percentage, defined per Bluetooth SIG with Microsoft as descriptor notifyParam.PresentationFormats.Add( GattPresentationFormat.FromParts( Convert.ToByte(PresentationFormats.FormatTypes.UTF8String), PresentationFormats.Exponent, Convert.ToUInt16(PresentationFormats.Units.Unitless), Convert.ToByte(PresentationFormats.NamespaceId.BluetoothSigAssignedNumber), PresentationFormats.Description)); GattLocalCharacteristic baseNotifyChar = null; result = await ServiceProvider.Service.CreateCharacteristicAsync(MSFTNotifyChar, notifyParam); GattServicesHelper.GetCharacteristicsFromResult(result, ref baseNotifyChar); if (baseNotifyChar != null) { NotifyCharacteristic = new Characteristics.MicrosoftNotifyCharacteristic(baseNotifyChar, this); } result = null; // Prepare the Indicate Characteristic GattLocalCharacteristicParameters indicateParam = new GattLocalCharacteristicParameters { CharacteristicProperties = GattCharacteristicProperties.Read | GattCharacteristicProperties.Indicate, WriteProtectionLevel = GattProtectionLevel.Plain, ReadProtectionLevel = GattProtectionLevel.Plain }; indicateParam.UserDescription = "Microsoft Indicate characteristic"; // Add presentation format - 16-bit integer, with exponent 0, the unit is percentage, defined per Bluetooth SIG with Microsoft as descriptor indicateParam.PresentationFormats.Add( GattPresentationFormat.FromParts( Convert.ToByte(PresentationFormats.FormatTypes.UTF8String), PresentationFormats.Exponent, Convert.ToUInt16(PresentationFormats.Units.Unitless), Convert.ToByte(PresentationFormats.NamespaceId.BluetoothSigAssignedNumber), PresentationFormats.Description)); GattLocalCharacteristic baseIndicateChar = null; result = await ServiceProvider.Service.CreateCharacteristicAsync(MSFTIndicateChar, indicateParam); GattServicesHelper.GetCharacteristicsFromResult(result, ref baseIndicateChar); if (baseIndicateChar != null) { IndicateCharacteristic = new Characteristics.MicrosoftNotifyCharacteristic(baseIndicateChar, this); } result = null; // Prepare the Read Long Characteristic GattLocalCharacteristicParameters longParam = new GattLocalCharacteristicParameters { CharacteristicProperties = GattCharacteristicProperties.Read, WriteProtectionLevel = GattProtectionLevel.Plain, ReadProtectionLevel = GattProtectionLevel.Plain }; longParam.UserDescription = "Microsoft Read Long characteristic"; // Add presentation format - 16-bit integer, with exponent 0, the unit is percentage, defined per Bluetooth SIG with Microsoft as descriptor longParam.PresentationFormats.Add( GattPresentationFormat.FromParts( Convert.ToByte(PresentationFormats.FormatTypes.OpaqueStructure), PresentationFormats.Exponent, Convert.ToUInt16(PresentationFormats.Units.Unitless), Convert.ToByte(PresentationFormats.NamespaceId.BluetoothSigAssignedNumber), PresentationFormats.Description)); GattLocalCharacteristic baseLongReadChar = null; result = await ServiceProvider.Service.CreateCharacteristicAsync(MSFTLongChar, longParam); GattServicesHelper.GetCharacteristicsFromResult(result, ref baseLongReadChar); if (baseLongReadChar != null) { ReadLongCharacteristic = new Characteristics.MicrosoftReadLongCharacteristic(baseLongReadChar, this); } result = null; }