繁体   English   中英

蓝牙低功耗API Windows 8

[英]Bluetooth Low Energy API Windows 8

我需要为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();
        }
    }
}

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

https://software.intel.com/zh-CN/articles/using-winrt-apis-from-desktop-applications

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM