C# metro 应用程序蓝牙 rfcomm

标签 c# windows bluetooth microsoft-metro rfcomm

我正在制作一个连接到蓝牙设备的应用程序。当我搜索我得到的设备时

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)

最佳答案

您没有对 Device.Name 进行空检查。不幸的是,您很可能会遇到这种错误。我在办公室看到大约 4 台设备没有名字,这让我有些头疼。希望你解决了这个问题!

关于C# metro 应用程序蓝牙 rfcomm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20008891/

相关文章:

c# - 双败赛数据结构

windows - Docker 端口绑定(bind)失败。为什么是 "permission denied"?

windows - 从软件中调用防病毒程序来扫描内存中的图像

c# - 同时建立多个蓝牙SPP

iphone - Swift 中的字典不起作用?

c# - svcutil.exe - 错误 : Cannot import wsdl:portType

c# - Response.Redirect() 禁用后退按钮

c# - 将 MyClass<string> 转换为 MyClass<object> 在运行时失败

Windows 中的 Java : files in a directory ordered with a specific name

Android 通过免提协议(protocol)连接到蓝牙