public CreateServiceException(GattServiceProviderResult createServiceExceptionResult) : base(string.Format($"An error occurred while creating the GATT" + $"service provider with this error code: " + $"{createServiceExceptionResult.Error}")) { CreateServiceExceptionResult = createServiceExceptionResult; }
internal async Task <bool> Initialize() { try { GattServiceProviderResult result = await GattServiceProvider.CreateAsync(_serviceId); if (result.Error != BluetoothError.Success) { Robot.Message("can't create GATT BlueTooth service with id " + _serviceId.ToString(), MessageType.Error); Robot.Message("without bluetooth service, remote controller won't work", MessageType.Warning); return(_init); } _service = result.ServiceProvider; byte[] value = new byte[] { 0x21 }; var constantParameters = new GattLocalCharacteristicParameters { CharacteristicProperties = (GattCharacteristicProperties.Read), StaticValue = value.AsBuffer(), ReadProtectionLevel = GattProtectionLevel.Plain, }; GattLocalCharacteristicResult characteristicResult = await _service.Service.CreateCharacteristicAsync(_notifyId, new GattLocalCharacteristicParameters { CharacteristicProperties = GattCharacteristicProperties.Notify, ReadProtectionLevel = GattProtectionLevel.Plain, StaticValue = value.AsBuffer() }); if (characteristicResult.Error != BluetoothError.Success) { Robot.Message("can't create GATT BlueTooth service with id " + _serviceId.ToString(), MessageType.Error); Robot.Message("without bluetooth service, remote controller won't work", MessageType.Warning); return(_init); } _notifyCharacteristic = characteristicResult.Characteristic; //_notifyCharacteristic.SubscribedClientsChanged += _btNotify_SubscribedClientsChanged; GattServiceProviderAdvertisingParameters advParameters = new GattServiceProviderAdvertisingParameters { IsDiscoverable = true, IsConnectable = true }; _service.StartAdvertising(advParameters); Robot.Message("created Bluetooth GATT service with id " + _serviceId.ToString(), MessageType.Success); _init = true; } catch (Exception x) { while (x != null) { Robot.Message(x.Message, MessageType.Error); x = x.InnerException; } Robot.Message("without bluetooth service, remote controller won't work", MessageType.Warning); } return(_init); }
//--------------------------------------------------------------------- // Method to create the GATT service provider //--------------------------------------------------------------------- // <param name="uuid"> The UUID of the service to create </param> protected async Task CreateServiceProvider(Guid uuid) { GattServiceProviderResult creationResult = await GattServiceProvider.CreateAsync(uuid); if (creationResult.Error != BluetoothError.Success) { throw new CreateServiceException(creationResult); } ServiceProvider = creationResult.ServiceProvider; }
/// <summary> /// Creates the Gatt Service provider /// </summary> /// <param name="uuid">UUID of the Service to create</param> protected async Task CreateServiceProvider(Guid uuid) { // Create Service Provider - similar to RFCOMM APIs GattServiceProviderResult result = await GattServiceProvider.CreateAsync(uuid); if (result.Error != BluetoothError.Success) { throw new CreateServiceException(result); } ServiceProvider = result.ServiceProvider; }
/// <summary> /// Creates the Gatt Service provider /// </summary> /// <param name="uuid">UUID of the Service to create</param> protected async Task CreateServiceProvider(Guid uuid) { // Create Service Provider - similar to RFCOMM APIs GattServiceProviderResult result = await GattServiceProvider.CreateAsync(uuid); if (result.Error != BluetoothError.Success) { throw new System.Exception(string.Format($"Error occured while creating the BLE service provider, Error Code:{result.Error}")); } ServiceProvider = result.ServiceProvider; }
public async Task Build(GattServiceProviderResult native) { var parameters = new GattLocalCharacteristicParameters { CharacteristicProperties = this.properties, ReadProtectionLevel = this.readProtection, WriteProtectionLevel = this.writeProtection }; var characteristic = await native .ServiceProvider .Service .CreateCharacteristicAsync(this.Uuid, parameters); }
/// <summary> /// 创建服务 /// </summary> private async Task CreateService() { GattServiceProviderResult result = await GattServiceProvider.CreateAsync(remoteGuid); if (result.Error == BluetoothError.Success) { serviceProvider = result.ServiceProvider; } else { throw new Exception(); } CreateCharacteristics(); }
public async Task Build() { this.native = await GattServiceProvider.CreateAsync(this.Uuid); foreach (var ch in this.characteristics) { await ch.Build(this.native); } this.native.ServiceProvider.StartAdvertising(new GattServiceProviderAdvertisingParameters { IsConnectable = true, IsDiscoverable = true }); }
public async Task Build(GattServiceProviderResult native) { var parameters = new GattLocalCharacteristicParameters { CharacteristicProperties = this.properties //ReadProtectionLevel = this.EncryptedRead // ? GattProtectionLevel.EncryptionAndAuthenticationRequired // : GattProtectionLevel.Plain, //WriteProtectionLevel = this.Write }; var characteristic = await native .ServiceProvider .Service .CreateCharacteristicAsync(this.Uuid, parameters); }
public async Task Init() { this.native = await GattServiceProvider.CreateAsync(this.Uuid); if (this.native.Error != BluetoothError.Success) { throw new ArgumentException(); } foreach (var ch in this.Characteristics.OfType <IUwpGattCharacteristic>()) { await ch.Init(this.native.ServiceProvider.Service); } this.native.ServiceProvider.StartAdvertising(new GattServiceProviderAdvertisingParameters { IsConnectable = true, IsDiscoverable = true }); }
/// <summary> /// Check that the service has started, start up the characteristics required for the BLE Service /// </summary> /// <returns></returns> private async Task ServiceProviderInitAsync() { // Initialize and starting a custom GATT Service MainService = await GattServiceProvider.CreateAsync(Constants.serviceProviderUuid); if (MainService.Error == BluetoothError.Success) { serviceProvider = MainService.ServiceProvider; } else { // An error occurred. serviceStarted = false; } characteristicResult = await serviceProvider.Service.CreateCharacteristicAsync(Constants.ReadCharacteristicUuid, Constants.gattReadParameters); if (characteristicResult.Error != BluetoothError.Success) { // An error occurred. serviceStarted = false; } readCharacteristic = characteristicResult.Characteristic; readCharacteristic.ReadRequested += ReadCharacteristic_ReadRequested; characteristicResult = await serviceProvider.Service.CreateCharacteristicAsync(Constants.WriteCharacteristicUuid, Constants.gattWriteParameters); if (characteristicResult.Error != BluetoothError.Success) { // An error occurred. serviceStarted = false; } writeCharacteristic = characteristicResult.Characteristic; writeCharacteristic.WriteRequested += WriteCharacteristic_WriteRequested; }
/// <summary> /// Uses the relevant Service/Characteristic UUIDs to initialize, hook up event handlers and start a service on the local system. /// </summary> /// <returns></returns> private async Task <bool> ServiceProviderInitAsync() { // BT_Code: Initialize and starting a custom GATT Service using GattServiceProvider. GattServiceProviderResult serviceResult = await GattServiceProvider.CreateAsync(Constants.CalcServiceUuid); if (serviceResult.Error == BluetoothError.Success) { serviceProvider = serviceResult.ServiceProvider; } else { rootPage.NotifyUser($"Could not create service provider: {serviceResult.Error}", NotifyType.ErrorMessage); return(false); } GattLocalCharacteristicResult result = await serviceProvider.Service.CreateCharacteristicAsync(Constants.Op1CharacteristicUuid, Constants.gattOperandParameters); if (result.Error == BluetoothError.Success) { op1Characteristic = result.Characteristic; } else { rootPage.NotifyUser($"Could not create operand1 characteristic: {result.Error}", NotifyType.ErrorMessage); return(false); } op1Characteristic.WriteRequested += Op1Characteristic_WriteRequestedAsync; result = await serviceProvider.Service.CreateCharacteristicAsync(Constants.Op2CharacteristicUuid, Constants.gattOperandParameters); if (result.Error == BluetoothError.Success) { op2Characteristic = result.Characteristic; } else { rootPage.NotifyUser($"Could not create operand2 characteristic: {result.Error}", NotifyType.ErrorMessage); return(false); } op2Characteristic.WriteRequested += Op2Characteristic_WriteRequestedAsync; result = await serviceProvider.Service.CreateCharacteristicAsync(Constants.OperatorCharacteristicUuid, Constants.gattOperatorParameters); if (result.Error == BluetoothError.Success) { operatorCharacteristic = result.Characteristic; } else { rootPage.NotifyUser($"Could not create operator characteristic: {result.Error}", NotifyType.ErrorMessage); return(false); } operatorCharacteristic.WriteRequested += OperatorCharacteristic_WriteRequestedAsync; // Add presentation format - 32-bit unsigned integer, with exponent 0, the unit is unitless, with no company description GattPresentationFormat intFormat = GattPresentationFormat.FromParts( GattPresentationFormatTypes.UInt32, PresentationFormats.Exponent, Convert.ToUInt16(PresentationFormats.Units.Unitless), Convert.ToByte(PresentationFormats.NamespaceId.BluetoothSigAssignedNumber), PresentationFormats.Description); Constants.gattResultParameters.PresentationFormats.Add(intFormat); result = await serviceProvider.Service.CreateCharacteristicAsync(Constants.ResultCharacteristicUuid, Constants.gattResultParameters); if (result.Error == BluetoothError.Success) { resultCharacteristic = result.Characteristic; } else { rootPage.NotifyUser($"Could not create result characteristic: {result.Error}", NotifyType.ErrorMessage); return(false); } resultCharacteristic.ReadRequested += ResultCharacteristic_ReadRequestedAsync; resultCharacteristic.SubscribedClientsChanged += ResultCharacteristic_SubscribedClientsChanged; // BT_Code: Indicate if your sever advertises as connectable and discoverable. GattServiceProviderAdvertisingParameters advParameters = new GattServiceProviderAdvertisingParameters { // IsConnectable determines whether a call to publish will attempt to start advertising and // put the service UUID in the ADV packet (best effort) IsConnectable = peripheralSupported, // IsDiscoverable determines whether a remote device can query the local device for support // of this service IsDiscoverable = true }; serviceProvider.AdvertisementStatusChanged += ServiceProvider_AdvertisementStatusChanged; serviceProvider.StartAdvertising(advParameters); return(true); }
/// <summary> /// Initializes a new instance of the <see cref="CreateServiceException"/> class /// </summary> /// <param name="createServiceExceptionResult">Gatt Service provider result</param> public CreateServiceException(GattServiceProviderResult createServiceExceptionResult) : base(string.Format($"Error occured while creating the provider, Error Code:{createServiceExceptionResult.Error}")) { CreateServiceExceptionResult = createServiceExceptionResult; }