找不到ID为蓝牙设备
问题描述:
我想使用的Visual Studio 2013通过蓝牙连接到一个Arduino,Windows Phone的应用程序...找不到ID为蓝牙设备
我能找到的设备没有任何问题得到一个错误说“找不到元素“当我用下面的代码:
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到直接服务名...
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...";
这个作品!否发送消息来控制机器人。
这个答案工作...我忘记了,当切换到另一个电话时,我不得不配对。 –