[英]Bluetooth 4.0 (low energy) API for windows desktop C# application
[英]How to send array via Bluetooth Low Energy in Windows Desktop APP?
我在Windows桌面应用程序中通过BLE与arduino通信时遇到了很大的麻烦。 我确实知道我必须在应用程序中启用WinRT API才能访问GATT并使用Win 8.1等。(我遵循了本教程https://software.intel.com/zh-cn/articles/using-winrt-apis-从桌面应用程序 )。 从现在开始,我不知道如何与我的设备通信。
所以我有该设备的值(adafruit nrf8001):
UART服务UUID:6E400001-B5A3-F393-E0A9-E50E24DCCA9E TX特性UUID:6E400002-B5A3-F393-E0A9-E50E24DCCA9E RX特性UUID:6E400003-B5A3-F393-E0A9-E50E24DCCA9
如何发送简单的数组-或更简单的将简单的ASCII值发送到设备? 在iOS和android上,我可以找到很多示例,但对于Windows桌面应用程序却找不到。
如果有人可以帮助,我将非常高兴!
菲尔编辑:我认为这应该做的工作...但是应用程序在最后一点崩溃了:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Enumeration;
using Windows.UI.Xaml;
namespace LucidMobileCCproto
{
public partial class LucidMIControlPanel : Form
{
GattDeviceService service2 = null;
private async void Discover_Click(object sender, EventArgs e)
{
var Services = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(new Guid("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")), null);
GattDeviceService Service = await GattDeviceService.FromIdAsync(Services[0].Id);
//Check Service Name
textBox1.Text = "Using service: " + Services[0].Name;
GattReliableWriteTransaction gattTransaction = new GattReliableWriteTransaction(); //Orientation on MSDN Article
GattCharacteristic gattCharacteristic = Service.GetCharacteristics(new Guid("6E400002-B5A3-F393-E0A9-E50E24DCCA9E"))[0];
//Check Characteristic
textBox2.Text = Convert.ToString(gattCharacteristic.CharacteristicProperties);
//initialize Buffer
var writer = new Windows.Storage.Streams.DataWriter();
writer.WriteByte(2);
gattTransaction.WriteValue(gattCharacteristic, writer.DetachBuffer());
//Programm Crashes here
GattCommunicationStatus status = await gattTransaction.CommitAsync();
}
}
崩溃:异常:抛出:“无法写入属性。(来自HRESULT的异常:0x80650003)”(System.Exception)引发了System.Exception:“无法写入的属性。(来自HRESULT的异常:0x80650003)”
哇,我花了几个小时才得到解决方案:
我只需要添加写选项(我也删除了可靠的写操作)
private async void Discover_Click(object sender, EventArgs e)
{
var Services = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(new Guid("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")));
GattDeviceService Service = await GattDeviceService.FromIdAsync(Services[0].Id);
GattCharacteristic gattCharacteristic = Service.GetCharacteristics(new Guid("6E400002-B5A3-F393-E0A9-E50E24DCCA9E"))[0];
var writer = new DataWriter();
writer.WriteString("#FF00FF");
var res = await gattCharacteristic.WriteValueAsync(writer.DetachBuffer(), GattWriteOption.WriteWithoutResponse);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.