我正在开发一个应用程序来从带有 HM-10 模块的 Arduino 接收数据。我正在编写一个 WPF .NET 应用程序,同时使用 UWP 库连接到 BLE。我之前编写了一个程序来发送数据,并在 .NET 控制台应用程序中从 Arduino 发送数据,该程序运行良好,我可以向 Arduino 发送文本并接收回文本。当把它移到我原来的项目上时,它停止了工作。
没有给出错误代码,当我试图从我试图接收通知的特征订阅通知时,程序刚刚停止。
private static async void subscribeToData()
{
if (readwrite.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Notify))
{
Console.WriteLine("Attempting subscription");
GattCommunicationStatus status = await readwrite.WriteClientCharacteristicConfigurationDescriptorAsync(
GattClientCharacteristicConfigurationDescriptorValue.None);
if (status == GattCommunicationStatus.Success)
{
Console.WriteLine("Recieving notifcations from device.");
recieve = false;
}
else
{
Console.WriteLine(status);
}
}
}
在上面的代码中,变量“readwrite”是我尝试从中获取通知的特征。控制台打印“尝试订阅”然后停止,没有错误代码。此代码在控制台应用程序中运行良好,只是在复制时不行。
这是完整的课程:
class BluetoothHandler
{
private static BluetoothLEAdvertisementWatcher watcher = new
BluetoothLEAdvertisementWatcher { ScanningMode =
BluetoothLEScanningMode.Active };
private static BluetoothLEDevice ble;
private static List<GattCharacteristic> characterstics = new
List<GattCharacteristic> { };
private static GattCharacteristic readwrite;
private static bool subscribe = true;
private static bool recieve = true;
public static void InitiateConnection()
{
GetDiscoverableDevices();
while (subscribe)
{
//Block code
}
subscribeToData();
while (recieve)
{
//Block code
}
readwrite.ValueChanged += recieveDataAsync;
}
private static async void subscribeToData()
{
if (readwrite.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Notify))
{
Console.WriteLine("Attempting subscription");
GattCommunicationStatus status = await readwrite.WriteClientCharacteristicConfigurationDescriptorAsync(
GattClientCharacteristicConfigurationDescriptorValue.None);
if (status == GattCommunicationStatus.Success)
{
Console.WriteLine("Recieving notifcations from device.");
recieve = false;
}
else
{
Console.WriteLine(status);
}
}
}
private static void recieveDataAsync(GattCharacteristic sender, GattValueChangedEventArgs args)
{
var reader = DataReader.FromBuffer(args.CharacteristicValue);
//var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
uint bufferLength = (uint)reader.UnconsumedBufferLength;
string receivedString = "";
receivedString += reader.ReadString(bufferLength) + "\n";
Console.WriteLine("Recieved Message: " + receivedString);
}
public static void GetDiscoverableDevices()
{
Console.WriteLine("Search started");
watcher.Received += bluetoothFoundAsync;
watcher.ScanningMode = BluetoothLEScanningMode.Active;
watcher.Start();
}
private static async void bluetoothFoundAsync(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
string deviceName = args.Advertisement.LocalName;
if (deviceName == "Carduino")
{
var bdevice = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
watcher.Stop();
Console.WriteLine("Scan complete: Found Carduino");
bdevice.DeviceInformation.Pairing.Custom.PairingRequested +=
(ss, ev) =>
{
ev.Accept();
};
var result = await bdevice.DeviceInformation.Pairing.Custom.PairAsync(DevicePairingKinds.ConfirmOnly);
Console.WriteLine($"Pairing Result: {result.Status}");
if (result.Status == DevicePairingResultStatus.AlreadyPaired)
{
Console.WriteLine("Attempting Reconnection");
var a = await bdevice.DeviceInformation.Pairing.UnpairAsync();
var result2 = await bdevice.DeviceInformation.Pairing.Custom.PairAsync(DevicePairingKinds.ConfirmOnly);
Console.WriteLine($"Pairing Result: {result2.Status}");
}
ble = bdevice;
getServices();
}
else
{
Console.WriteLine("Couldn't find device: 'Carduino'");
}
}
private static async void getServices()
{
GattDeviceServicesResult result = await ble.GetGattServicesAsync();
if (result.Status == GattCommunicationStatus.Success)
{
var services = result.Services;
getCharacterstics(services);
}
}
private static async void getCharacterstics(IReadOnlyList<GattDeviceService> services)
{
foreach (GattDeviceService s in services)
{
GattCharacteristicsResult result = await s.GetCharacteristicsAsync();
if (result.Status == GattCommunicationStatus.Success)
{
var characteristicss = result.Characteristics;
foreach (GattCharacteristic c in characteristicss)
{
characterstics.Add(c);
if (c.Uuid == Guid.Parse("0000ffe1-0000-1000-8000-00805f9b34fb"))
{
readwrite = c;
Console.WriteLine("Found read/write characteristic");
}
}
}
}
subscribe = false;
Console.WriteLine("Got characteristics");
}
}
提前致谢。
编辑:
经过进一步测试,它似乎只能在控制台 .NET 应用程序中工作,而不能在 Windows 窗体应用程序或 .NET WPF 应用程序中工作。