'Send value from BLE Peripheral to iOS device
I am working on an WPF application where it works as a Peripheral. I have another iPhone app which is connected to WPF peripheral and is also able to send dat to WPF. Now I want to send data from WPF peripheral to iOS device. Initially I was able to send the data as part of Static Value of characteristics but now I have to send data multiple times on click of button. It will be like a chat between WPF Peripheral and iOS Client. How do I send these messages from WPF. I have already created following read and write characteristics:
GattServiceProvider serviceProvider;
private GattLocalCharacteristic _readCharacteristic;
private GattLocalCharacteristic _writeCharacteristic;
public event EventHandler<BLEPeripheralManagerEventArgs> RaiseCustomEvent;
public BLEPeripheralManager()
{
}
public async void InitiateBluetoothConn()
{
var peripheralSupported = await BluetoothAdapter.GetDefaultAsync();
if (peripheralSupported != null)
{
if (serviceProvider == null)
{
var serviceStarted = await ServiceProviderInitAsync();
if (serviceStarted)
{
Debug.WriteLine("Service successfully started");
}
}
}
}
private async Task<bool> ServiceProviderInitAsync()
{
GattServiceProviderResult serviceResult = await GattServiceProvider.CreateAsync(Constants.ServiceUuid);
if (serviceResult.Error == BluetoothError.Success)
{
serviceProvider = serviceResult.ServiceProvider;
}
var ReadParameters = new GattLocalCharacteristicParameters
{
CharacteristicProperties = (GattCharacteristicProperties.Read | GattCharacteristicProperties.Notify),
ReadProtectionLevel = GattProtectionLevel.Plain,
UserDescription = "Read Characteristic"
};
GattLocalCharacteristicResult characteristicResult = await serviceProvider.Service.CreateCharacteristicAsync(Constants.ReadCharacteristicUuid, ReadParameters);
if (characteristicResult.Error != BluetoothError.Success)
{
return false;
}
_readCharacteristic = characteristicResult.Characteristic;
_readCharacteristic.ReadRequested += ReadCharacteristic_ReadRequested;
var WriteParameters = new GattLocalCharacteristicParameters
{
CharacteristicProperties = (GattCharacteristicProperties.Read | GattCharacteristicProperties.Write | GattCharacteristicProperties.Notify),
ReadProtectionLevel = GattProtectionLevel.Plain,
UserDescription = "Write Characteristic"
};
characteristicResult = await serviceProvider.Service.CreateCharacteristicAsync(Constants.WriteCharacteristicUuid, WriteParameters);
if (characteristicResult.Error != BluetoothError.Success)
{
// An error occurred.
return false;
}
_writeCharacteristic = characteristicResult.Characteristic;
_writeCharacteristic.ReadRequested += ReadCharacteristic_ReadRequested;
_writeCharacteristic.WriteRequested += WriteCharacteristic_WriteRequested;
GattServiceProviderAdvertisingParameters advParameters = new GattServiceProviderAdvertisingParameters
{
IsDiscoverable = true,
IsConnectable = true
};
serviceProvider.StartAdvertising(advParameters);
return true;
}
I want to frequently send message from WPF UI to iOS device. Almost all the resources that I could found over internet were on other way i.e, central to peripheral but I want to send it from WPF Peripheral to iOS client.
Edit1: I have tried it like following but it doesn't work.
public void SendMessage(String toSend)
{
//_writeCharacteristic..NotifyValueAsync()
_ = _readCharacteristic.NotifyValueAsync(CryptographicBuffer.ConvertStringToBinary(toSend, BinaryStringEncoding.Utf8));
_writeCharacteristic.NotifyValueAsync(CryptographicBuffer.ConvertStringToBinary(toSend, BinaryStringEncoding.Utf8));
}
I don't get anything in my iOS app.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
