Here is what I have,
-
蓝牙可穿戴设备(MyoArm频段) .
-
Windows Mobile 10周年更新 .
两者都配对得当 .
Now, here is what I am trying to do,
-
我试图列举连接到我的Windows Mobile的蓝牙设备所暴露的所有服务的列表 .
-
如果服务提供输入流,我想读取输入流 .
我通过MSDN文档,这是我到目前为止所做的 .
P.S. I have added Bluetooth access to the capabilities in the application manifest.
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();
}
}
}
当我运行代码并单击“接收”按钮时,我在调用RfcommDeviceService.FromIdAsync方法时遇到异常 .
Exception: Element not found. (Exception from HRESULT: 0x80070490)
我在这里错过了什么吗?我是蓝牙设备编程的新手,所以我正确地解决了这个问题吗?
1 回答
首先,请确保按设备名称查询的设备是蓝牙设备,因为您发现所有设备不仅仅是用于查询的蓝牙设备 . 要查找蓝牙设备,建议使用DeviceWatcher,示例代码请参考this file中的
StartUnpairedDeviceWatcher()
方法 .其次,我不确定为什么
RfcommDeviceService.FromIdAsync(myBluetoothDevice.Id);
无法获得RfcommDeviceService实例,但官方样本没有使用此方法来获取服务 . 它首先获得了BluetoothDeivce,然后从设备获得了GetRfcommServices .RfcommServiceId与创建的RfcommServiceProvider相同 . 详细信息请参考我测试过的official sample可以运行并成功找到
RfcommDeviceService
实例 .