从 UWP 中的可穿戴设备访问 RfcommDeviceService 时出现异常
Exception while accessing RfcommDeviceService from a wearable in UWP
这是我的,
- 蓝牙可穿戴设备(MyoArm 手环)。
- Windows Mobile 10 周年更新。
他们都配对正确。
现在,这就是我正在尝试做的事情,
- 我正在尝试枚举连接到我的 windows 手机的蓝牙设备公开的所有服务的列表。
- 然后我想读取输入流,如果服务提供的话。
我查看了 MSDN 文档,这是我目前所做的。
P.S。我在应用程序清单中添加了对功能的蓝牙访问。
private async void OnReceiveClick(object sender, RoutedEventArgs e)
{
var devices = await DeviceInformation.FindAllAsync();
IList<DeviceInformation> myBluetoothDevices = new List<DeviceInformation>();
foreach (var device in devices)
{
if (device.Name.Contains("myo"))
{
var trace = string.Format("Name: {2} \t Paired: {3} \t Kind: {1} \t Id: {0}", device.Id, device.Kind, device.Name, device.Pairing?.IsPaired);
builder.AppendLine(trace);
myBluetoothDevices.Add(device);
}
}
foreach (var myBluetoothDevice in myBluetoothDevices)
{
try
{
if (myBluetoothDevice != null)
{
var service = await RfcommDeviceService.FromIdAsync(myBluetoothDevice.Id);
// TODO: Read input stream somehow here!!!
log.Text = builder.AppendLine(string.Format("Name: {0} \t Id: {1} \t Device Info Name: {2} \t Connection Host Name: {3} \t Service Id: {4}", service.Device.Name, service.Device.DeviceId, service.Device.DeviceInformation.Name, service.ConnectionHostName, service.ServiceId.Uuid)).ToString();
}
}
catch (Exception ex)
{
builder.AppendLine(ex.Message);
}
finally
{
log.Text = builder.ToString();
}
}
}
当我 运行 代码并单击 "Receive" 按钮时,我在调用 RfcommDeviceService.FromIdAsync 方法时遇到异常。
异常:未找到元素。 (HRESULT 异常:0x80070490)
我是不是漏掉了什么?我是蓝牙设备编程的新手,所以我是否正确地解决了这个问题?
首先,请确保通过设备名称查询到的设备是蓝牙设备,因为您查找的不是只有蓝牙设备,而是所有设备。对于查找蓝牙设备,DeviceWatcher
is recommended and sample code please reference StartUnpairedDeviceWatcher()
method in this file.
其次,我不确定为什么 RfcommDeviceService.FromIdAsync(myBluetoothDevice.Id);
无法从设备获取 RfcommDeviceService
instance but the official sample is not using this method for getting the service. It got the BluetoothDeivce firstly and then GetRfcommServices
。
var bluetoothDevice = await BluetoothDevice.FromIdAsync(myBluetoothDevice.Id);
var rfcommServices = await bluetoothDevice.GetRfcommServicesForIdAsync(RfcommServiceId.FromUuid(Constants.RfcommChatServiceUuid));
if (rfcommServices.Services.Count > 0)
{
service = rfcommServices.Services[0];
}
我测试过的RfcommServiceId is same as RfcommServiceProvider created. Details please reference the official sample可以运行并成功找到RfcommDeviceService
个实例。
这是我的,
- 蓝牙可穿戴设备(MyoArm 手环)。
- Windows Mobile 10 周年更新。
他们都配对正确。
现在,这就是我正在尝试做的事情,
- 我正在尝试枚举连接到我的 windows 手机的蓝牙设备公开的所有服务的列表。
- 然后我想读取输入流,如果服务提供的话。
我查看了 MSDN 文档,这是我目前所做的。
P.S。我在应用程序清单中添加了对功能的蓝牙访问。
private async void OnReceiveClick(object sender, RoutedEventArgs e)
{
var devices = await DeviceInformation.FindAllAsync();
IList<DeviceInformation> myBluetoothDevices = new List<DeviceInformation>();
foreach (var device in devices)
{
if (device.Name.Contains("myo"))
{
var trace = string.Format("Name: {2} \t Paired: {3} \t Kind: {1} \t Id: {0}", device.Id, device.Kind, device.Name, device.Pairing?.IsPaired);
builder.AppendLine(trace);
myBluetoothDevices.Add(device);
}
}
foreach (var myBluetoothDevice in myBluetoothDevices)
{
try
{
if (myBluetoothDevice != null)
{
var service = await RfcommDeviceService.FromIdAsync(myBluetoothDevice.Id);
// TODO: Read input stream somehow here!!!
log.Text = builder.AppendLine(string.Format("Name: {0} \t Id: {1} \t Device Info Name: {2} \t Connection Host Name: {3} \t Service Id: {4}", service.Device.Name, service.Device.DeviceId, service.Device.DeviceInformation.Name, service.ConnectionHostName, service.ServiceId.Uuid)).ToString();
}
}
catch (Exception ex)
{
builder.AppendLine(ex.Message);
}
finally
{
log.Text = builder.ToString();
}
}
}
当我 运行 代码并单击 "Receive" 按钮时,我在调用 RfcommDeviceService.FromIdAsync 方法时遇到异常。
异常:未找到元素。 (HRESULT 异常:0x80070490)
我是不是漏掉了什么?我是蓝牙设备编程的新手,所以我是否正确地解决了这个问题?
首先,请确保通过设备名称查询到的设备是蓝牙设备,因为您查找的不是只有蓝牙设备,而是所有设备。对于查找蓝牙设备,DeviceWatcher
is recommended and sample code please reference StartUnpairedDeviceWatcher()
method in this file.
其次,我不确定为什么 RfcommDeviceService.FromIdAsync(myBluetoothDevice.Id);
无法从设备获取 RfcommDeviceService
instance but the official sample is not using this method for getting the service. It got the BluetoothDeivce firstly and then GetRfcommServices
。
var bluetoothDevice = await BluetoothDevice.FromIdAsync(myBluetoothDevice.Id);
var rfcommServices = await bluetoothDevice.GetRfcommServicesForIdAsync(RfcommServiceId.FromUuid(Constants.RfcommChatServiceUuid));
if (rfcommServices.Services.Count > 0)
{
service = rfcommServices.Services[0];
}
我测试过的RfcommServiceId is same as RfcommServiceProvider created. Details please reference the official sample可以运行并成功找到RfcommDeviceService
个实例。