2015-06-07 36 views
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){}未發現該例外,因此可能意味着其他地方存在問題。'System.IO.FileNotFoundException'調用RfcommDeviceService.FromIdAsync(...)後

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; 
    } 
} 

我也有以下CapabilitiesPackage.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> 
+0

不同的.dll同樣的問題http://stackoverflow.com/questions/9729691/an-unhandled-exception-of-type-system-io-filenotfoundexception-occurred-in-unk 也看看這個https: //social.msdn.microsoft.com/Forums/zh-CN/86e5fb0b-bc89-42d7-85d4-bd755385044e/a-first-chance-exception-of-type-systemiofilenotfoundexception-occurred-in-mscorlibdll?forum=microsoftdeviceemu –

+1

@ user3956566解決了這個問題之後,我猜想問題在於我使用的方法是WinRT桌面/平板電腦獨有的,不適用於手機。正如你所建議的,最可能的原因將是缺少DLL。 – njallam

+0

感謝您的反饋。 –

回答

1

原來做藍牙連接是WinRT的臺式機/平板電腦,而不是手機的DeviceInformation。解決方案是使用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()); 完成。閱讀我還沒有想出如何去做,但是我對這個問題的解決已經被上面的問題解決了。

相關問題