in BluetoothLEExplorer/BluetoothLEExplorer/Models/VirtualKeyboard.cs [169:229]
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;
}