【发布时间】: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