';System.IO.FileNotFoundException';调用RfcommDeviceService.FromIdAsync(…)后
我正在尝试创建一个连接到特定蓝牙设备的功能。我确信DeviceInformation参数是有效的,因此问题应该包含在下面的函数中。行';System.IO.FileNotFoundException';调用RfcommDeviceService.FromIdAsync(…)后,exception,bluetooth,windows-phone-8.1,Exception,Bluetooth,Windows Phone 8.1,我正在尝试创建一个连接到特定蓝牙设备的功能。我确信DeviceInformation参数是有效的,因此问题应该包含在下面的函数中。行RfcommDeviceService.FromIdAsync(…)之后的一段短时间内,我将在Visual Studio的输出中看到mscorlib.ni.dll中出现了类型为“System.IO.FileNotFoundException”的第一次意外异常,然后看到程序“…”已以代码-1(0xffffffff)退出。。此外,try{}catch(exception
RfcommDeviceService.FromIdAsync(…)
之后的一段短时间内,我将在Visual Studio的输出中看到mscorlib.ni.dll中出现了类型为“System.IO.FileNotFoundException”的第一次意外异常,然后看到程序“…”已以代码-1(0xffffffff)退出。
。此外,try{}catch(exception e){}
没有捕获到异常,因此这可能意味着其他地方存在问题
public async Task<bool> Connect(DeviceInformation deviceInformation)
{
try
{
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
rfcommService = await RfcommDeviceService.FromIdAsync(deviceInformation.Id);
});
System.Diagnostics.Debug.WriteLine("edfdshjkfdsklfdjslkf");
if (rfcommService == null)
{
return false;
}
System.Diagnostics.Debug.WriteLine(rfcommService.Device.ToString());
await streamSocket.ConnectAsync(
rfcommService.ConnectionHostName,
rfcommService.ConnectionServiceName);
dataReader = new DataReader(streamSocket.InputStream);
dataWriter = new DataWriter(streamSocket.OutputStream);
return true;
}
catch (Exception e)
{
Debug.WriteLine("Exception while connecting: " + e.Message);
Debug.WriteLine(e.StackTrace);
return false;
}
}
事实证明,建立蓝牙连接的
设备信息
是针对WinRT桌面/平板电脑,而不是手机。解决方案是使用PeerInformation
方法
该函数现在看起来如下所示:
public async Task<bool> Connect(PeerInformation peerInfo)
{
streamSocket = new StreamSocket();
try
{
await streamSocket.ConnectAsync(peerInfo.HostName, "{00001101-0000-1000-8000-00805f9b34fb}");
}
catch (System.Exception)
{
return false;
}
return true;
}
公共异步任务连接(PeerInformation peerInfo)
{
streamSocket=新的streamSocket();
尝试
{
等待streamSocket.ConnectAsync(peerInfo.HostName,{00001101-0000-1000-8000-00805f9b34fb});
}
捕获(系统异常)
{
返回false;
}
返回true;
}
可以使用wait streamSocket.OutputStream.WriteAsync(rawMessage.AsBuffer())完成写入;
。阅读时,我还没有弄明白该怎么做,但我遇到的问题已通过上述方法解决。@user3956566在解决了这个问题后,我猜问题在于我使用的方法是WinRT desktop/tablet独有的,不适用于手机。正如您所建议的,最可能的原因是缺少DLL。
public async Task<bool> Connect(PeerInformation peerInfo)
{
streamSocket = new StreamSocket();
try
{
await streamSocket.ConnectAsync(peerInfo.HostName, "{00001101-0000-1000-8000-00805f9b34fb}");
}
catch (System.Exception)
{
return false;
}
return true;
}