使用独特的服务协议使用32feet.net连接到BTLE设备
本文关键字:连接 BTLE 设备 net 协议 服务 32feet | 更新日期: 2023-09-27 18:21:31
我有一个蓝牙低能耗(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。连接时出现错误:发生类型为"System.Net.SocketException"的异常。附加信息:连接尝试失败是因为连接方在一段时间后没有正确响应,或者建立的连接失败是因为所连接的主机没有响应
windows API代码是否可以在32feet.net中重写,如果可以,如何继续
注意:我试图使用windows原生.net代码连接到BT2.1设备,但串行端口不稳定,令人头疼。凭借32英尺的网络,它现在可以在串行端口上出色地工作,非常稳定,每次都能连接
我遇到了与有关的模拟问题
信号量超时时间已过期。(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错误和固件问题,设备从未重新打开蓝牙模块。