【问题标题】:How to send array via Bluetooth Low Energy in Windows Desktop APP?如何在 Windows 桌面 APP 中通过蓝牙低功耗发送阵列?
【发布时间】:2015-08-09 17:23:15
【问题描述】:

我在我的 Windows 桌面应用程序中通过 BLE 与我的 arduino 通信时遇到了巨大的麻烦。我确实明白我必须在我的应用程序中启用 WinRT API 才能访问 GATT 并使用 win 8.1 等(我遵循了本教程 https://software.intel.com/en-us/articles/using-winrt-apis-from-desktop-applications)。从这一点开始,我不明白如何与我的设备进行通信。

所以我有这个设备的值(adafruit nrf8001):

UART服务UUID:6E400001-B5A3-F393-E0A9-E50E24DCCA9E TX 特性 UUID:6E400002-B5A3-F393-E0A9-E50E24DCCA9E RX 特征 UUID:6E400003-B5A3-F393-E0A9-E50E24DCCA9E

我如何向设备发送一个简单的数组——或者更简单的一个简单的 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)”

【问题讨论】:

  • 如果没有可靠地重现问题的a good, minimal, complete code example 代码示例,并且没有完整 异常信息(包括异常消息的确切文本和完整的堆栈跟踪。我会说,在我自己的 BLE 代码中,我使用 WindowsRuntimeBufferExtensions.AsBuffer() 扩展方法来创建要写入的缓冲区,而不是创建 DataWriter 对象并从中分离缓冲区。恕我直言,这是一种更简单且不易出错的方法。
  • 抱歉彼得在这里不专业 - 我添加了上面的信息..你能给我一个例子如何在上面的代码中使用WindowsRuntimeBufferExtensions.AsBuffer()吗?

标签: c# bluetooth windows-runtime arduino bluetooth-lowenergy


【解决方案1】:

哇,我花了几个小时才找到解决方案:

我只需要添加写入选项(我也删除了可靠写入)

    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);
    }

【讨论】:

    猜你喜欢
    • 2014-06-17
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多