C# Metro 应用程序蓝牙 RFCOMM

3
我正在制作一个连接蓝牙设备的应用程序。当我搜索设备时,在列表框中会出现Windows.Devices.Enumeration.DeviceInformation,但我想要设备名称。我使用的代码是:“搜索设备”。
var devices = await DeviceInformation.FindAllAsync(
            RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));

将设备添加到列表框中。
foreach (var device in devices)
            {

                DevicesListBox.Items.Add(device);
            }

如果我将DevicesListBox.Items.Add(device)改为DevicesListBox.Items.Add(device.name),我会得到System.String错误。
连接设备。
var serviceInfo = (DeviceInformation)DevicesListBox.SelectedItem;

        try
        {
            rfcommService = await RfcommDeviceService.FromIdAsync(serviceInfo.Id);

            if (rfcommService != null)
            {
                socket = new StreamSocket();
                await socket.ConnectAsync(
                    rfcommService.ConnectionHostName,
                    rfcommService.ConnectionServiceName)

如果你已经找到了解决方案,我很感兴趣。 - Eric Smekens
1个回答

1

您没有对设备名称进行空值检查。很遗憾,您可能会遇到这种错误。我在办公室看到了大约4个没有名称并且让我感到头痛的设备。希望您已经解决了这个问题!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接