我正在尝试使用visual studio 2013,windows phone app ...
通过蓝牙连接到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 =“”
有什么建议吗?
答案 0 :(得分: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...";
这个有效!!没有发送消息来控制机器人。