If no BLE device (with the specific characteristic requested) is available, ReadValueAsync(BluetoothCacheMode.Uncached) tries to read for 7-8 seconds before returning its result. Is there any safe way to cancel/abort the task without having to wait the full 7 seconds in this case?
This is my current implementation (based on Asynchronously wait for Task<T> to complete with timeout) :
int timeout = 1000;
var task = BleCommon.Instance.readCharacteristic(devName, serviceUuid, characteristicUuid, isEncryptionRequired);
if (await Task.WhenAny(task, Task.Delay(timeout)) == task)
{
// task completed within timeout
var result = await task;
// do something with result
}
else
{
// timeout occured
// do nothing
}
public async Task<List<byte>> readCharacteristic(string deviceName, Guid serviceUuid, Guid characteristicUuid, bool isEncryptionRequired)
Does anyone see any potential issues with this implementation?
Note that the readCharacteristic() function is basically just a wrapper-function for ReadValueAsync(BluetoothCacheMode.Uncached) (https://msdn.microsoft.com/en-us/library/windows/apps/dn263758.aspx). My main concern is that I am not completely sure what happens when ReadValueAsync(BluetoothCacheMode.Uncached) is called before the previous call to it has returned.