Код: Выделить всё
static GattCharacteristicsResult gattCharacteristic;
private GattCharacteristicProperties properties;
private GattCharacteristic characteristic;
private IReadOnlyList services;
private ulong LEdevice = 0;
private BluetoothLEDevice bleDevice;
private BluetoothLEAdvertisementWatcher watcher;
//TopazController _topazController = new TopazController();
public async Task BackgroundBluetooth()
{
watcher = new BluetoothLEAdvertisementWatcher();
watcher.Received += Watcher_Received;
watcher.AllowExtendedAdvertisements = true;
watcher.ScanningMode = BluetoothLEScanningMode.Active;
watcher.Start();
Console.WriteLine("Background bluetooth starting...");
while (true)
{
if (LEdevice == 0)
{
Thread.Sleep(200);
}
else
{
if (bleDevice != null)
{
//Console.WriteLine("BLE is already connected");
Thread.Sleep(3000);
continue;
}
/* This hangs for some reason if Topaz Object is created ?? */
Console.WriteLine("Start");
bleDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(LEdevice);
Console.WriteLine("Stop");
var serviceID = new Guid("e20a39f4-73f5-4bc4-a12f-17d1ad07a961");
var result = await bleDevice.GetGattServicesForUuidAsync(serviceID);
if (result?.Status == GattCommunicationStatus.Success)
{
Console.WriteLine("Connected to Service");
services = result.Services;
gattCharacteristic = await services[0].GetCharacteristicsAsync();
if (gattCharacteristic.Status == GattCommunicationStatus.Success)
{
Console.WriteLine("Connected to Characteristic");
characteristic = gattCharacteristic.Characteristics[0];
characteristic.ValueChanged += Characteristic_ValueChanged;
properties = characteristic.CharacteristicProperties;
if (properties.HasFlag(GattCharacteristicProperties.Notify))
{
Console.WriteLine("Notify Found");
GattCommunicationStatus status =
await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(
GattClientCharacteristicConfigurationDescriptorValue.Notify);
if (status == GattCommunicationStatus.Success)
{
Console.WriteLine("Gatt Communication Success");
}
}
}
else
{
Console.WriteLine("Error: " + gattCharacteristic.Status);
}
}
else
{
Console.WriteLine("Error: " + result.Status);
}
}
}
}
void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
{
var reader = DataReader.FromBuffer(args.CharacteristicValue);
reader.UnicodeEncoding = UnicodeEncoding.Utf8;
reader.ByteOrder = ByteOrder.LittleEndian;
var value = reader.ReadString(args.CharacteristicValue.Length);
Console.WriteLine(value);
// ProcessCommand(value);
}
void Watcher_Received(BluetoothLEAdvertisementWatcher sender,
BluetoothLEAdvertisementReceivedEventArgs args)
{
foreach (var uuid in args.Advertisement.ServiceUuids)
{
if (uuid.ToString() == "e20a39f4-73f5-4bc4-a12f-17d1ad07a961")
{
if (LEdevice == 0)
{
LEdevice = args.BluetoothAddress;
}
}
}
}
Код: Выделить всё
TopazController _topazController = new TopazController();
Код: Выделить всё
bleDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(LEdevice)
Я ищу рекомендации по решению этой проблемы. Я провел небольшое исследование в Интернете и обнаружил ошибку, связанную с в очень старой сборке Windows 10 возникли проблемы с зависанием этой функции. Я пошел дальше и внес исправления в реестр, но проблема все еще остается.
Что же могло привести к такому конфликту этих двух устройств? Мы будем очень признательны за любую помощь!!
Также некоторая техническая информация, которая может оказаться полезной.
Net 4.8.1
Windows UWP
Windows SDK 10.0. 19041.0
Было ли у кого-нибудь такое раньше? Это проблема Windows 10?
Подробнее здесь: https://stackoverflow.com/questions/790 ... -net-4-8-1
Мобильная версия