使用32feet.net使用独特的服务协议

时间:2015-11-12 00:42:35

标签: c# wpf bluetooth 32feet btle

我有一个蓝牙低功耗(BTLE)设备,我需要连接到我的电脑。为此,我在桌面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年根本不可能link使用ble,但现在可能是这样吗?使用此答案link中的扫描方法,我可以在未配对的情况下找到我的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'类型的异常。 附加信息:连接尝试失败,因为连接方在一段时间后没有正确响应,或者由于连接主机无法响应而建立连接失败

是否可以在32feet.net中重写Windows API代码,ifso,如何继续?

注意:我尝试使用Windows原生.net代码连接到BT2.1设备,但串口不稳定且头痛。有了32feet.net,它现在可以在SerialPort上运行得非常好,非常稳定,每次都可以连接!

1 个答案:

答案 0 :(得分:0)

我有与此相关的类似问题

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

我的问题已通过.net 4.7.1解决。另外,我需要确保我使用的蓝牙设备有最新的微软蓝牙驱动程序

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

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

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