我想將myo腕帶連接到hololens。這是最終目標,但我無論如何,但接近: -/UWP藍牙LE InvalidCastException
這個想法是建立一個與UWP的藍牙LE連接。 我想這樣做,因爲在這個Microsoft Document
的設備搜索workes精細explanined,但是當我嘗試連接到一個設備,這條線(點「連接到設備」 ):GattDeviceServicesResult result = await device.GetGattServicesAsync();
引發錯誤:
System.InvalidCastException: "Unable to cast object of type 'Windows.Devices.Bluetooth.BluetoothLEDevice' to type 'Windows.Devices.Bluetooth.IBluetoothLEDevice3'."
我不知道什麼IBluetoothLEDevice3已經在那裏做:-) 我無法找到關於微軟的文檔了一個解決方案或互聯網: -/
我在Visual Studio 2017上工作,爲Windows 10構建(15063),並且在清單中啓用了藍牙。
這是我的代碼,所以票價。我只添加了一件事,那就是任務。我想確保BluetoothLEDDevice不爲空或任何東西,因爲它不同步。沒有它不工作。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Diagnostics;
using Windows.Devices.Enumeration;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth.Advertisement;
// Die Elementvorlage "Leere Seite" wird unter https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x407 dokumentiert.
namespace Bluetooth17
{
/// <summary>
/// Eine leere Seite, die eigenständig verwendet oder zu der innerhalb eines Rahmens navigiert werden kann.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
blue();
}
void blue()
{
// Query for extra properties you want returned
string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" };
DeviceWatcher deviceWatcher =
DeviceInformation.CreateWatcher(
BluetoothLEDevice.GetDeviceSelectorFromPairingState(false),
requestedProperties,
DeviceInformationKind.AssociationEndpoint);
// Register event handlers before starting the watcher.
// Added, Updated and Removed are required to get all nearby devices
deviceWatcher.Added += DeviceWatcher_Added;
deviceWatcher.Updated += DeviceWatcher_Updated;
deviceWatcher.Removed += DeviceWatcher_Removed;
// EnumerationCompleted and Stopped are optional to implement.
deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted;
deviceWatcher.Stopped += DeviceWatcher_Stopped;
// Start the watcher.
deviceWatcher.Start();
}
private void DeviceWatcher_Stopped(DeviceWatcher sender, object args)
{
Debug.WriteLine("Stopped");
}
private void DeviceWatcher_EnumerationCompleted(DeviceWatcher sender, object args)
{
Debug.WriteLine("Enum complete");
}
private void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate args)
{
Debug.WriteLine(args.Id + " Removed");
}
private void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate args)
{
Debug.WriteLine(args.Id + " Update");
}
private void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args)
{
Debug.WriteLine(args.Id + " " + args.Name);
if (args.Name.Equals("Myo"))
{
Debug.WriteLine("Try to connect to Myo");
getServices(args);
}
}
async Task<BluetoothLEDevice> ConnectDevice(DeviceInformation deviceInfo)
{
Debug.WriteLine("Asyc");
// Note: BluetoothLEDevice.FromIdAsync must be called from a UI thread because it may prompt for consent.
return await BluetoothLEDevice.FromIdAsync(deviceInfo.Id);
}
async void getServices(DeviceInformation deviceInfo)
{
Task<BluetoothLEDevice> task = ConnectDevice(deviceInfo);
task.Wait();
BluetoothLEDevice device = task.Result;
GattDeviceServicesResult result = await device.GetGattServicesAsync();
if (result.Status == GattCommunicationStatus.Success)
{
var services = result.Services;
// ...
}
}
}
}
謝謝
什麼hololens的您的操作系統版本? –
它的10.0.14393.0,但我現在沒有在HoloLens上工作。我試圖在普通的Windows 10電腦上設置它,這是發生錯誤的地方。 – Marcel