简体   繁体   English

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

[英]How to send array via Bluetooth Low Energy in Windows Desktop APP?

I'm having enormous trouble communicating with my arduino over BLE in my Windows desktop application. 我在Windows桌面应用程序中通过BLE与arduino通信时遇到了很大的麻烦。 I did understand that i have to enable WinRT API in my application to access GATT and to use win 8.1 etc. (I followed this tutorial https://software.intel.com/en-us/articles/using-winrt-apis-from-desktop-applications ). 我确实知道我必须在应用程序中启用WinRT API才能访问GATT并使用Win 8.1等。(我遵循了本教程https://software.intel.com/zh-cn/articles/using-winrt-apis-从桌面应用程序 )。 I don't understand how to communicate with my device from this point. 从现在开始,我不知道如何与我的设备通信。

So i have this values for the device (adafruit nrf8001): 所以我有该设备的值(adafruit nrf8001):

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

how do i send a simple array - or easier a simple ASCII value to the device? 如何发送简单的数组-或更简单的将简单的ASCII值发送到设备? On iOS and android i find plenty of examples, but not for a windows desktop app ... 在iOS和android上,我可以找到很多示例,但对于Windows桌面应用程序却找不到。

would be super happy if someone can help! 如果有人可以帮助,我将非常高兴!

phil edit: i thought this should do the job... but the app crashes on the last point: 菲尔编辑:我认为这应该做的工作...但是应用程序在最后一点崩溃了:

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

The crash: Exception:Thrown: "The attribute cannot be written. (Exception from HRESULT: 0x80650003)" (System.Exception) A System.Exception was thrown: "The attribute cannot be written. (Exception from HRESULT: 0x80650003)" 崩溃:异常:抛出:“无法写入属性。(来自HRESULT的异常:0x80650003)”(System.Exception)引发了System.Exception:“无法写入的属性。(来自HRESULT的异常:0x80650003)”

wow just took me a few hours to get the solution: 哇,我花了几个小时才得到解决方案:

I Just had to add the write option (i deleted the reliable write as well) 我只需要添加写选项(我也删除了可靠的写操作)

    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.

相关问题 用于Windows桌面C#应用程序的蓝牙4.0(低能耗)API - Bluetooth 4.0 (low energy) API for windows desktop C# application 如何在Windows桌面应用程序中的手机上配对和搜索低功耗蓝牙服务? - How to pair and search Bluetooth Low Energy services on phone in windows desktop application? Windows Phone 8.1-低功耗蓝牙和身份验证 - Windows Phone 8.1 - Bluetooth Low Energy and Authentication 如何在Windows UWP App中正确订阅蓝牙低能耗设备的GattCharacteristic.ValueChanged通知(指示)? - How do I correctly subscribe to a bluetooth low energy device's GattCharacteristic.ValueChanged notification (indication) in a Windows UWP App? 低功耗蓝牙连接 - Bluetooth Low Energy connection 通过Windows Phone应用程序控制Windows桌面程序(蓝牙) - Control windows desktop program via windows phone app (bluetooth) Windows 10 低功耗蓝牙连接 c# - Windows 10 Bluetooth Low Energy Connection c# 打开Outlook应用程序/桌面以通过Windows 8 App发送邮件 - Open Outlook application/ desktop to send mail via Windows 8 App 无法订阅蓝牙低功耗中的通知特性 - Failed to subscribe to notification characteristic in bluetooth low energy UWP蓝牙低功耗应用尽早断开连接 - UWP Bluetooth Low Energy Application Disconnects Early
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM