11

我有一个蓝牙低功耗 (BTLE) 设备,我需要将其连接到我的 PC。为此,我在桌面 WPF 应用程序中使用了 Windows API 参考。

蓝牙设备相当简单:1 个服务,2 个特性(一个读取/通知,一个写入)。

To make below code work, add the following references to the WPF (for windows 10):
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll
C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd

C# 的 Windows API 代码

public GattDeviceService BLEService;
public GattCharacteristic BLEData_ReadNotify;
public GattCharacteristic BLEData_Write;
private static readonly Guid Guid_service = new Guid("25FB9E91-1616-448D-B5A3-F70A64BDA73A");
public static readonly Guid Characteristics_ReadNotify = new Guid("C3FBC9E2-676B-9FB5-3749-2F471DCF07B2");
public static readonly Guid Characteristics_Write = new Guid("D6AF9B3C-FE92-1CB2-F74B-7AFB7DE57E6D");


// Find all BTLE devices that have the service Guid: Guid_service
public void findandconnect() { 
    var BTLEdeviceList = await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(Guid_service));
    // lets assume at least one device is found, store it's id.
    id = BTLEdeviceList[0].id;

    // Connect to the SERVICE of the device
    BLEService = await GattDeviceService.FromIdAsync(id);

    // Get the first "ReadNotify" characteristics of that service
    BLEData_ReadNotify = BLEService.GetCharacteristics(Characteristics_ReadNotify).First();
    // If that value changes, update an event
    BLEData_ReadNotify.ValueChanged += BLEData_ValueChanged;

    // Also get the first "write" characteristics of that service
    BLEData_Write = BLEService.GetCharacteristics(Characteristics_Write).First();
    // and tell that characteristics to NOTIFY me if the BTLE device has an update.
    await BLEData_ReadNotify.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
    // Successfully connected

    // Send the command "RUN\r" to the write characteristics, to let it start to execute the program
    byte[] buffer = Encoding.ASCII.GetBytes("RUN" + "\r");
    IBuffer ibuffer = buffer.AsBuffer();
    // Sending "RUN"
    await BLEData_Write.WriteValueAsync(ibuffer);
}
// And here should be some event function for the ValueChanged

好吧,它可以工作,有点......它非常不稳定,大多数时候它会产生无法解决的错误:

行:BLEData_ReadNotify.WriteClientCharacteri... 产生 指定的登录会话没有用户会话密钥。(来自 HRESULT 的异常:0x80070572)

并且信号量超时期限已过。(来自 HRESULT 的异常:0x80070079)。第二个错误不太常见,当脚本首先没有收到任何内容时出现,然后通过计时器尝试再次发送 RUN 命令,然后全部崩溃并出现错误。

在stackoverflow和许多网站上搜索,我发现以上两个错误都无法解决。因此我的问题是:

上面的代码可以用 32Feet.net 重写吗?

在 2012 年,根本不可能使用 ble 的链接,但也许现在可以了?使用此答案链接中的扫描方法,我可以在未配对时找到我的 BLE 设备。

所以,我尝试了一些代码

// mac is mac address of local bluetooth device
Guid Guid_service = new Guid("25FB9E91-1616-448D-B5A3-F70A64BDA73A");
localEndpoint = new BluetoothEndPoint(myRadio.LocalAddress, Guid_service);
// client is used to manage connections
localClient = new BluetoothClient(localEndpoint);

BluetoothAddress a = new BluetoothAddress(new byte[] { 180, 99, 76, 86, 31, 21 });
Guid Guid_service = new Guid("25FB9E91-1616-448D-B5A3-F70A64BDA73A");
localClient.Connect(a, Guid_service);

NetworkStream stream = localClient.GetStream();

但我被困在 localClient.Connect 行并出现错误: 发生“System.Net.Sockets.SocketException”类型的异常。附加信息:连接尝试失败,因为连接方在一段时间后没有正确响应,或建立连接失败,因为连接的主机没有响应

windows API代码可以在32feet.net中重写吗,如果可以,如何进行?

注意:我尝试使用windows原生.net代码连接BT2.1设备,但是串口不稳定,头疼。使用 32feet.net,它现在可以在 SerialPort 上出色地工作,非常稳定并且每次都能连接!

4

1 回答 1

0

我对此有类似的问题

信号量超时期限已过。(来自 HRESULT 的异常:0x80070079)。

.net 4.7.1 解决了我的问题。我还需要确保我使用的蓝牙设备拥有最新的微软蓝牙驱动程序

当后台线程发生异常时,SerialPort 流不再终止进程。在使用中移除 USB 串行端口时可能会发生这种情况。此新行为由 Switch.System.IO.Ports.DoNotCatchSerialStreamThreadExceptions AppContext 开关控制。当面向 .NET 4.7 或更低版本时,此开关默认设置为 truetf get。[428461,System.dll,错误]

https://github.com/Microsoft/dotnet/blob/0745983093568027ff931672d9db021e3e120b5b/releases/net471/dotnet471-changes.md

更新:实际上它似乎是两个问题合二为一。.net 错误和固件问题,设备永远不会重新打开蓝牙模块。

于 2017-10-30T18:03:59.580 回答