找不到蓝牙设备的ID
本文关键字:ID 找不到 | 更新日期: 2023-09-27 18:07:09
我正在尝试通过蓝牙连接到Arduino使用visual studio 2013, windows phone应用程序…
当我使用以下代码时,我可以找到没有任何问题的设备,得到一个错误说"Element not found":
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...";
这是! !不允许发送消息来控制机器人