public async void Run(IBackgroundTaskInstance taskInstance) { _deferral = taskInstance.GetDeferral(); var details = taskInstance.TriggerDetails as GattServiceProviderTriggerDetails; service = details.Connection.Service; Debug.WriteLine($"task is triggered. TriggerId: {details?.Connection.TriggerId}"); Debug.WriteLine(""); var settings = ApplicationData.Current.LocalSettings; if (settings.Values["BackgroundNotes"] != null) { settings.Values.Remove("BackgroundNotes"); } settings.Values["BackgroundNotes2"] = "we started Bg task...."; ApplicationData.Current.LocalSettings.Values.Remove("Op1ReceivedEver"); { foreach (var gattLocalCharacteristic in service.Characteristics) { Debug.WriteLine($"user descr: {gattLocalCharacteristic.UserDescription}"); if (gattLocalCharacteristic.UserDescription == "Operand 1 Characteristic") { gattLocalCharacteristic.WriteRequested += Op1Characteristic_WriteRequestedAsync; } } } details.Connection.Start(); }
public async Task Init(GattLocalService gatt) { var ch = await gatt.CreateCharacteristicAsync( this.Uuid, new GattLocalCharacteristicParameters { CharacteristicProperties = this.ToNative(this.Properties), ReadProtectionLevel = this.Permissions.HasFlag(GattPermissions.ReadEncrypted) ? GattProtectionLevel.EncryptionAndAuthenticationRequired : GattProtectionLevel.Plain, WriteProtectionLevel = this.Permissions.HasFlag(GattPermissions.WriteEncrypted) ? GattProtectionLevel.EncryptionAndAuthenticationRequired : GattProtectionLevel.Plain, } ); foreach (var descriptor in this.Descriptors.OfType <IUwpGattDescriptor>()) { await descriptor.Init(ch.Characteristic); } this.native = ch.Characteristic; this.nativeReady.OnNext(ch.Characteristic); }
private async Task CreateHidService() { // HID service. var hidServiceProviderCreationResult = await GattServiceProvider.CreateAsync(GattServiceUuids.HumanInterfaceDevice); if (hidServiceProviderCreationResult.Error != BluetoothError.Success) { Debug.WriteLine("Failed to create the HID service provider: " + hidServiceProviderCreationResult.Error); throw new Exception("Failed to create the HID service provider: " + hidServiceProviderCreationResult.Error); } m_hidServiceProvider = hidServiceProviderCreationResult.ServiceProvider; m_hidService = m_hidServiceProvider.Service; // HID keyboard Report characteristic. var hidKeyboardReportCharacteristicCreationResult = await m_hidService.CreateCharacteristicAsync(GattCharacteristicUuids.Report, c_hidInputReportParameters); if (hidKeyboardReportCharacteristicCreationResult.Error != BluetoothError.Success) { Debug.WriteLine("Failed to create the keyboard report characteristic: " + hidKeyboardReportCharacteristicCreationResult.Error); throw new Exception("Failed to create the keyboard report characteristic: " + hidKeyboardReportCharacteristicCreationResult.Error); } m_hidKeyboardReport = hidKeyboardReportCharacteristicCreationResult.Characteristic; m_hidKeyboardReport.SubscribedClientsChanged += HidKeyboardReport_SubscribedClientsChanged; // HID keyboard Report Reference descriptor. var hidKeyboardReportReferenceCreationResult = await m_hidKeyboardReport.CreateDescriptorAsync(BluetoothUuidHelper.FromShortId(c_hidReportReferenceDescriptorShortUuid), c_hidKeyboardReportReferenceParameters); if (hidKeyboardReportReferenceCreationResult.Error != BluetoothError.Success) { Debug.WriteLine("Failed to create the keyboard report reference descriptor: " + hidKeyboardReportReferenceCreationResult.Error); throw new Exception("Failed to create the keyboard report reference descriptor: " + hidKeyboardReportReferenceCreationResult.Error); } m_hidKeyboardReportReference = hidKeyboardReportReferenceCreationResult.Descriptor; // HID Report Map characteristic. var hidReportMapCharacteristicCreationResult = await m_hidService.CreateCharacteristicAsync(GattCharacteristicUuids.ReportMap, c_hidReportMapParameters); if (hidReportMapCharacteristicCreationResult.Error != BluetoothError.Success) { Debug.WriteLine("Failed to create the HID report map characteristic: " + hidReportMapCharacteristicCreationResult.Error); throw new Exception("Failed to create the HID report map characteristic: " + hidReportMapCharacteristicCreationResult.Error); } m_hidReportMap = hidReportMapCharacteristicCreationResult.Characteristic; // HID Information characteristic. var hidInformationCharacteristicCreationResult = await m_hidService.CreateCharacteristicAsync(GattCharacteristicUuids.HidInformation, c_hidInformationParameters); if (hidInformationCharacteristicCreationResult.Error != BluetoothError.Success) { Debug.WriteLine("Failed to create the HID information characteristic: " + hidInformationCharacteristicCreationResult.Error); throw new Exception("Failed to create the HID information characteristic: " + hidInformationCharacteristicCreationResult.Error); } m_hidInformation = hidInformationCharacteristicCreationResult.Characteristic; // HID Control Point characteristic. var hidControlPointCharacteristicCreationResult = await m_hidService.CreateCharacteristicAsync(GattCharacteristicUuids.HidControlPoint, c_hidControlPointParameters); if (hidControlPointCharacteristicCreationResult.Error != BluetoothError.Success) { Debug.WriteLine("Failed to create the HID control point characteristic: " + hidControlPointCharacteristicCreationResult.Error); throw new Exception("Failed to create the HID control point characteristic: " + hidControlPointCharacteristicCreationResult.Error); } m_hidControlPoint = hidControlPointCharacteristicCreationResult.Characteristic; m_hidControlPoint.WriteRequested += HidControlPoint_WriteRequested; m_hidServiceProvider.AdvertisementStatusChanged += HidServiceProvider_AdvertisementStatusChanged; }