C#winform程序电脑Ble蓝牙无接收问题

huajianying111 2019-11-01 03:39:05
问题如题,在winform程序下添加了System.Runtime.WindowsRuntime.dll
可以调用电脑蓝牙,完成搜索,发送数据功能了
但是莫名其妙的接收数据功能一直没反应。经测试发现:
1、确定已经BLE蓝牙服务UUID码正确,而且发送数据功能正常,通过串口可知下位机已接收到数据并返回数据
2、通过WriteClientCharacteristicConfigurationDescriptorAsync可知,往下位机写入ClientCharacteristicConfigurationDescriptor已成功
3、完全一摸一样的代码,在UWP程序下能正常收到(通过按钮点击连接发送数据,然后能触发接收事件),但在Winform程序下能连接能接收,但是不能接收
4、尝试过Winform程序放在主线程中完成连接注册接收事件,但还是没接收
实在是想不到还有什么问题了···有没有大佬用过winform程序开发与BLE蓝牙通信的,快来告知一下问题在哪

代码:

/// <summary>
/// 连接蓝牙设备
/// </summary>
/// <param name="id"></param>
public async Task StartBleDeviceConnect(string id)
{
CurrentDevice = await BluetoothLEDevice.FromIdAsync(id);
if (CurrentDevice != null)
{
Guid guid = new Guid(Guid);
GattDeviceServicesResult re = await CurrentDevice.GetGattServicesForUuidAsync(guid);
if (re.Services.Count > 0)
{
CurrentService = re.Services[0];
if (CurrentService != null)
{
GetCurrentNotifyCharacteristic();
}
else
{
//主服务发现失败
}
}
}
}
/// <summary>
/// 设置通知特征对象。
/// </summary>
/// <returns></returns>
private async void GetCurrentNotifyCharacteristic()
{
//读
Guid guid2 = new Guid(ReadGuid);
GattCharacteristicsResult result = await CurrentService.GetCharacteristicsForUuidAsync(guid2);
if (result.Characteristics.Count > 0)
{
CurrentNotifyCharacteristic = result.Characteristics[0];
CurrentNotifyCharacteristic.ProtectionLevel = GattProtectionLevel.Plain;
CurrentNotifyCharacteristic.ValueChanged += Characteristic_Changed;
Console.WriteLine("事件绑定成功");
await EnableNotifications(CurrentNotifyCharacteristic);
}
else
{
//读服务发现失败
}
}
/// <summary>
/// 设置特征对象为接收通知对象
/// </summary>
/// <param name="characteristic">CurrentNotifyCharacteristic</param>
/// <returns></returns>
public async Task EnableNotifications(GattCharacteristic characteristic)
{
GattCommunicationStatus status = await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);//status返回为success

await GetCurrentWriteCharacteristic();
}
/// <summary>
/// 设置写特征对象。
/// </summary>
/// <returns></returns>
public async Task GetCurrentWriteCharacteristic()
{

string msg = "";
Guid guid = new Guid(WriteGuid);
GattCharacteristicsResult result = await CurrentService.GetCharacteristicsForUuidAsync(guid);
if (result.Characteristics.Count > 0)
{
CurrentWriteCharacteristic = result.Characteristics[0];
}
else
{
//读服务发现失败
}
}

...全文
1342 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Cookies 2021-02-07
  • 打赏
  • 举报
回复
楼主,你的问题搞定了没?源码能给我一份不?8316499@qq.com谢谢
jones.s 2020-08-10
  • 打赏
  • 举报
回复
楼主你这设备从搜索到连接 慢吗?
huajianying111 2020-03-03
  • 打赏
  • 举报
回复
突然看到,想起来有这么个问题回来收个尾,问题解决了,是程序没有接收权限的问题,往注册表中写入权限才OK,跑到外国网去查才找到的答案 网站链接: https://social.msdn.microsoft.com/Forums/en-US/58da3fdb-a0e1-4161-8af3-778b6839f4e1/bluetooth-bluetoothledevicefromidasync-does-not-complete-on-10015063?forum=wdk 解决方案由Venkata Thumu提出
huajianying111 2019-11-01
  • 打赏
  • 举报
回复
引用 4 楼 wanghui0380 的回复:
先装busHound监听一下总线,你先确定真有数据发出,数据进来 然后再讨论你的代码问题。不然光听你说,发了没收。 谁知道具体发没发,发了啥。对方回复没回复,回复了啥。调试协议第一步就是找个能看得见得数据得玩意把数据显示出来,不然光凭嘴说,说不清楚得
数据发出和回复这个是已经确定是可以了的,因为我是先用UWP实现过一次,已经完成了收发,下位机能运动,串口Log能打印出下位机接收的数据和发送的数据,这个是我已经确定下来的。 然后才把代码移植到winform程序,下位机通过串口也能直接看到接收的数据和下返回给上位机的数据。所以数据源也能确定是存在的。然后现在的问题就是PC这边没反应。 最后我又把winform的代码直接复制粘贴到一个新的uwp程序,直接运行调试,又能触发接收事件
wanghui0380 2019-11-01
  • 打赏
  • 举报
回复
新手上位机和个下位机在现场吵架吵几个小时的我经常见 俺们通常上去就一句话“都给我闭嘴,用抓包工具抓包,谁发了,谁没发,清清楚楚,是谁的问题就谁来解决,不要欺负新人乱扔锅”
wanghui0380 2019-11-01
  • 打赏
  • 举报
回复
先装busHound监听一下总线,你先确定真有数据发出,数据进来 然后再讨论你的代码问题。不然光听你说,发了没收。 谁知道具体发没发,发了啥。对方回复没回复,回复了啥。调试协议第一步就是找个能看得见得数据得玩意把数据显示出来,不然光凭嘴说,说不清楚得
huajianying111 2019-11-01
  • 打赏
  • 举报
回复
引用 2 楼 OrdinaryCoder 的回复:
能发不能收 我第一反应是防火墙或者权限的问题 试试管理员启动VS 把防火墙关一下
把所有防火墙都关闭了,然后管理员权限启动调试,也还是没有接收,发送正常
OrdinaryCoder 2019-11-01
  • 打赏
  • 举报
回复
能发不能收 我第一反应是防火墙或者权限的问题 试试管理员启动VS 把防火墙关一下
huajianying111 2019-11-01
  • 打赏
  • 举报
回复
自顶,给个参考方向也是好的,现在一脸懵逼,就差接收这块就完成了

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧