Reputation: 11
I need to write a Desktop application for Windows 8, that can communicate with Bluetooth Low Energy devices. After my Research I think this is only possible for Windows apps but not for a Desktop application because there are no APIs. Is there a way to use the APIs for the Windows apps in a Desktop application?
Thanks
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();
}
}
}
Upvotes: 1
Views: 4545
Reputation: 1696
You can get access to WinRT API (Windows Apps) from desktop applications. Just follow the steps in following link:
https://software.intel.com/en-us/articles/using-winrt-apis-from-desktop-applications
But be aware that you need Windows 8.1 in order to use BLE capabilities.
Upvotes: 1