簡體   English   中英

如何在Windows桌面APP中通過低功耗藍牙發送陣列?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM