流套接字访问被拒绝Windows Phone 8.1蓝牙

时间:2015-03-27 10:49:08

标签: c# windows-phone-8 bluetooth windows-phone-8.1

我尝试将蓝牙设备与我的Windows Phone(8.1)与StreamSocket-Class连接。

现在,每当我调用socket.CallAsync(HostName,RemoteServiceName)时,我都会收到AccessDenied错误。我在AppManifest中激活了接近传感器。

以下是代码:

private async void ConnectToDevice()
    {
        if (_socket != null)
        {
            // Disposing the socket with close it and release all resources associated with the socket
            _socket.Dispose();
        }

        try
        {
            _socket = new StreamSocket();

            // Note: If either parameter is null or empty, the call will throw an exception
            await _socket.ConnectAsync(_currentPeer.HostName, "{00001101-0000-1000-8000-00805f9b34fb}");

            // If the connection was successful, the RemoteAddress field will be populated
            MessageDialog md = new MessageDialog(_socket.Information.RemoteAddress.DisplayName);
            await md.ShowAsync();

            Start.IsEnabled = true;
        }
        catch (Exception ex)
        {
            MessageDialog md = new MessageDialog("Connection failed");
            await md.ShowAsync();

            _socket.Dispose();
            _socket = null;
        }
    }

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

我有同样的问题。但最后我在http://www.codefest.at/post/2014/02/03/Bluetooth-in-Windows-81-Apps-nutzen.aspx

找到了解决方案

互联网(客户端和服务器)功能无法解决问题。这对我来说是个谜,为什么MS不能在Visual Studio的GUI中提供蓝牙功能。

只需将以下xml添加到您的appxmanifest:

<Capabilities>
    <m2:DeviceCapability Name="bluetooth.rfcomm">
      <m2:Device Id="any">
        <m2:Function Type="serviceId:00001101-0000-1000-8000-00805F9B34FB" />
      </m2:Device>
    </m2:DeviceCapability>
  </Capabilities>

此致