如何在Windows桌面APP中通过蓝牙低功耗发送阵列?

时间:2015-08-09 17:23:15

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

我在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)”

1 个答案:

答案 0 :(得分:3)

哇刚刚花了几个小时才得到解决方案:

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

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