【问题标题】:Cannot find ID for bluetooth device找不到蓝牙设备的 ID
【发布时间】:2015-08-08 14:10:57
【问题描述】:

我正在尝试使用 Visual Studio 2013、Windows Phone 应用程序通过蓝牙连接到 Arduino...

当我使用以下代码时,我可以找到没有任何问题的设备,但出现“找不到元素”的错误:

await socket.ConnectAsync(MakeBlock.HostName, "5",
                        SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);

我尝试修改代码使用 RfcommDeviceService 获取服务名称,但来自 PeerFinder 对象的 Id 为 "" 并且无法设置 connectService。

connectService = RfcommDeviceService.FromIdAsync(MakeBlock.Id);

这是我尝试连接的完整代码:

 #region App to Device....
            PeerFinder.AlternateIdentities["Bluetooth:SDP"] = "{00001101-0000-1000-8000-00805F9B34FB}";
            var pairedDevices = await PeerFinder.FindAllPeersAsync();
            tbLogger.Text = "Seaching for Connections...";

            if (pairedDevices.Count == 0)
            {
                tbLogger.Text = "Makeblock is not found...";
            }
            else
            {
                tbLogger.Text = pairedDevices.Count.ToString() + " connections found!";

                for (int i = 0; i < pairedDevices.Count; i++)
                {
                    PeerInformation selectedPeer = pairedDevices[i];
                    tbLogger.Text = tbLogger.Text + "\r\n" + selectedPeer.DisplayName;
                    if (selectedPeer.DisplayName == "Makeblock")
                    {
                        MakeBlock = pairedDevices[i];
                    }
                }

                tbLogger.Text = tbLogger.Text + "\r\n" + "---------------------------";

                try
                {
                    StreamSocket socket = new StreamSocket();
                    IAsyncOperation<RfcommDeviceService> connectService;
                    connectService = RfcommDeviceService.FromIdAsync(MakeBlock.Id);
                    RfcommDeviceService rfcommService = await connectService;
                    await socket.ConnectAsync(rfcommService.ConnectionHostName, rfcommService.ConnectionServiceName,
                        SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
                    tbLogger.Text = tbLogger.Text + "\r\n" + "Connection to MakeBlock has been made...";
                }
                catch (Exception ex)
                {
                    tbLogger.Text = tbLogger.Text + "\r\n" + "Could not connect to " + MakeBlock.DisplayName;
                    tbLogger.Text = tbLogger.Text + "\r\n" + ex.Message;
                }
            }
            #endregion

代码失败,因为 MakeBlock.Id = ""

有什么建议吗?

【问题讨论】:

    标签: c# visual-studio-2013 bluetooth arduino windows-phone-8.1


    【解决方案1】:

    好吧...我想通了。 :)

    我只是将 GUID 直接添加到 serviceName 中...

                StreamSocket socket = new StreamSocket();
                await socket.ConnectAsync(MakeBlock.HostName, "{00001101-0000-1000-8000-00805F9B34FB}",
                    SocketProtectionLevel.PlainSocket);
                tbLogger.Text = tbLogger.Text + "\r\n" + "Connection to MakeBlock has been made...";
    

    这行得通!不发送消息来控制机器人。

    【讨论】:

    • 这个答案有效...我忘了切换到另一部手机时必须配对。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    相关资源
    最近更新 更多