gpt4 book ai didi

c# - 找不到蓝牙设备的 ID

转载 作者:太空宇宙 更新时间:2023-11-03 13:02:40 27 4
gpt4 key购买 nike

我正在尝试使用 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 = ""

有什么建议吗?

最佳答案

好吧……我明白了。 :)

我只是直接将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...";

这有效!!否继续发送消息来控制机器人。

关于c# - 找不到蓝牙设备的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31893946/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com