【问题标题】:Bluetooth Low Energy API Windows 8蓝牙低功耗 API Windows 8
【发布时间】:2015-02-12 13:16:54
【问题描述】:

我需要为 Windows 8 编写一个桌面应用程序,它可以与低功耗​​蓝牙设备进行通信。经过我的研究,我认为这仅适用于 Windows 应用程序,但不适用于桌面应用程序,因为没有 API。有没有办法在桌面应用程序中使用 Windows 应用程序的 API?

谢谢

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Enumeration;

namespace WinRTNutzungVS2013
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            try
            {
                Initialize().GetAwaiter();
            }
            catch (Exception e)
            {
                MessageBox.Show("Fehler");
            }
        }

        double convertTemperatureData(byte[] temperatureData)
        {
            UInt32 mantissa = ((UInt32)temperatureData[3] << 16 | ((UInt32)temperatureData[2] << 8) | ((UInt32)temperatureData[1]));

            Int32 exponent = (Int32)temperatureData[4];

            return mantissa * Math.Pow(10.0, exponent);
        }

        private async Task Initialize()
        {
            var themometerServices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(GattServiceUuids.HealthThermometer), null);
            GattDeviceService firstThermometerService = await GattDeviceService.FromIdAsync(themometerServices[0].Id);
            tbServices.Text = "Using service: " + themometerServices[0].Name;
            GattCharacteristic thermometerCharacteristic = firstThermometerService.GetCharacteristics(GattCharacteristicUuids.TemperatureMeasurement)[0];
            thermometerCharacteristic.ValueChanged += temperatureMeasurementChanged;
            await thermometerCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
        }

        void temperatureMeasurementChanged(GattCharacteristic sender, GattValueChangedEventArgs eventArgs)
        {
            byte[] temperatureData = new byte[eventArgs.CharacteristicValue.Length];
            Windows.Storage.Streams.DataReader.FromBuffer(eventArgs.CharacteristicValue).ReadBytes(temperatureData);
            var temperatureValue = convertTemperatureData(temperatureData);
            tbTemperature.Text = temperatureValue.ToString();
        }
    }
}

【问题讨论】:

    标签: windows bluetooth bluetooth-lowenergy


    【解决方案1】:

    您可以从桌面应用程序访问 WinRT API(Windows 应用程序)。只需按照以下链接中的步骤操作:

    https://software.intel.com/en-us/articles/using-winrt-apis-from-desktop-applications

    但请注意,您需要 Windows 8.1 才能使用 BLE 功能。

    【讨论】:

    • 非常感谢您提供的链接。但是当我这样做时,我总是得到错误:'await' 要求类型 'Windows.Foundation.IAsyncOperation' 具有合适的 GetAwaiter 方法。您是否缺少“系统”的 using 指令?
    • 这是因为您需要 Windows 8.1 才能使用 BLE 功能。它不适用于 Windows 8。
    • 是的,你是对的,但我使用的是 Windows 8.1,但我还是得到了错误:(
    • 按照步骤使用 WinRT API 后,我使用完全相同的代码从 MSDN 网站 ([link] (msdn.microsoft.com/en-us/library/windows/apps/xaml/…)) 中检索 BLE 设备的数据
    • 在博客中,TargetPlatformVersion 设置为 8.0。您是否将其修改为 8.1,因为您使用的是 Windows 8.1?。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 2018-10-12
    • 2012-05-28
    相关资源
    最近更新 更多