【发布时间】:2018-01-10 01:32:08
【问题描述】:
我正在尝试创建一个监听蓝牙串行端口连接的 UWP 应用。当我在应用程序中设置侦听器时(通过单击按钮),我在该行遇到异常:
await socketListener.BindServiceNameAsync(
rfcommProvider.ServiceId.ToString(),
SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);`
异常消息是: System.ArgumentException: '参数不正确。 'protectionLevel':IP StreamSocketListeners 只允许使用普通套接字。'
代码如下:
private async void btnBluetoothServerClick(object sender, RoutedEventArgs e)
{
var rfcommProvider =
await RfcommServiceProvider.CreateAsync(RfcommServiceId.SerialPort);
var socketListener = new StreamSocketListener();
socketListener.ConnectionReceived += OnConnectionReceived;
await socketListener.BindServiceNameAsync(
rfcommProvider.ServiceId.ToString(),
SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
rfcommProvider.StartAdvertising(socketListener);
}
当protectionLevel参数SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication,改成SocketProtectionLevel.PlainSocket,如异常所示,新的异常信息为:System.Exception: '指定的类是未找到。 (HRESULT 异常:0x8007277D)'.
应用清单文件包括:
<Capabilities>
<Capability Name="internetClient" />
<DeviceCapability Name="bluetooth" />
<DeviceCapability Name="proximity" />
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
<DeviceCapability Name="bluetooth.rfcomm">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
<DeviceCapability Name="wifiControl" />
</Capabilities>
我做错了什么?
【问题讨论】:
标签: bluetooth uwp serial-port listener rfcomm