' System.IO.FileNotFoundException'在调用RfcommDeviceService.FromIdAsync(...)之后

时间:2015-06-07 09:37:29

标签: exception bluetooth windows-phone-8.1

我正在尝试创建一个连接到特定蓝牙设备的功能。我有点确定DeviceInformation参数是有效的,所以问题应该只包含在下面的函数中。在RfcommDeviceService.FromIdAsync(...)行后很短的一段时间我将在Visual Studio的输出中看到A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll,然后看The program '...' has exited with code -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;
    }
}

我在Capabilities中也有以下Package.appxmanifest

<Capabilities>
<Capability Name="internetClientServer" />
<DeviceCapability Name="proximity" />
  <m2:DeviceCapability Name="bluetooth.rfcomm">
    <m2:Device Id="any">
      <m2:Function Type="name:serialPort" />
    </m2:Device>
  </m2:DeviceCapability>
</Capabilities>

1 个答案:

答案 0 :(得分:1)

原来DeviceInformation制作蓝牙连接适用于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;
}

可以使用await streamSocket.OutputStream.WriteAsync(rawMessage.AsBuffer()); 完成写作。阅读我仍然没有想出怎么做但是我对这个问题的问题已经解决了。