从UWP中的可穿戴设备访问RfcommDeviceService时发生异常 [英] Exception while accessing RfcommDeviceService from a wearable in UWP

查看:107
本文介绍了从UWP中的可穿戴设备访问RfcommDeviceService时发生异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我所拥有的,

  1. 蓝牙可穿戴设备(MyoArm腕带).
  2. 具有周年纪念更新的Windows Mobile 10.

两者都已正确配对.

现在,这就是我想要做的

  1. 我正在尝试枚举连接到Windows Mobile的蓝牙设备公开的所有服务的列表.
  2. 如果服务提供了输入流,那么我想读取输入流.

我查看了MSDN文档,这是到目前为止我所做的.

I went though MSDN documentation and here is what I have done so far.

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();
            } 
        }
    }

运行代码并单击接收"按钮时,调用RfcommDeviceService.FromIdAsync方法时出现异常.

When I run the code and click the "Receive" button, I get an exception while calling the RfcommDeviceService.FromIdAsync method.

异常:找不到元素.(来自HRESULT的异常:0x80070490)

我在这里错过了什么吗?我是第一次使用蓝牙设备进行编程,所以我可以正确解决该问题吗?

Am I missing something here? I am new to programming with bluetooth devices, so am I approaching the problem correctly?

推荐答案

首先,请确保按设备名称查询的设备是Bluetooth设备,因为您发现所有设备不仅是要查询的Bluetooth设备.要查找蓝牙设备,请 建议使用DeviceWatcher ,示例代码请参考StartUnpairedDeviceWatcher()方法/master/Samples/BluetoothRfcommChat/cs/Scenario1_ChatClient.xaml.cs"rel =" nofollow noreferrer>此文件.

Firstly, please ensure the devices queried out by device name are Bluetooth devices since you find all devices not only Bluetooth devices for query. For find Bluetooth devices, DeviceWatcher is recommended and sample code please reference StartUnpairedDeviceWatcher() method in this file.

其次,我不确定为什么 RfcommDeviceService.FromIdAsync(myBluetoothDevice.Id); 无法获得 <设备上的code> GetRfcommServices .

Secondly, I'm not sure why RfcommDeviceService.FromIdAsync(myBluetoothDevice.Id); cannot get a RfcommDeviceService instance but the official sample is not using this method for getting the service. It got the BluetoothDeivce firstly and then GetRfcommServices from the device.

 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 与创建的 RfcommServiceProvider 相同.详细信息请参考我测试过的官方示例可以成功运行并找到 RfcommDeviceService 实例.

The RfcommServiceId is same as RfcommServiceProvider created. Details please reference the official sample which I have tested can run and find RfcommDeviceService instance successfully.

这篇关于从UWP中的可穿戴设备访问RfcommDeviceService时发生异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆