蓝牙低功耗API Windows 8

时间:2015-02-12 13:16:55

标签: windows bluetooth bluetooth-lowenergy

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

1 个答案:

答案 0 :(得分:1)

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

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

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