RFCOMM连接工作不稳定

Nic*_*ick 7 windows bluetooth rfcomm windows-phone windows-phone-8.1

我有Windows Phone应用程序,通过蓝牙(Bluetooth App To Device)控制设备.在WP8中它运行良好.这是代码

1)搜索所有配对设备

PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
var peers = await PeerFinder.FindAllPeersAsync();
ObservableCollection<PeerInformation> devicesCollection = new ObservableCollection<PeerInformation>();
foreach (var peer in peers)
{
   devicesCollection.Add(peer); 
} 
Run Code Online (Sandbox Code Playgroud)

2)连接到设备

socket = new StreamSocket();
await socket.ConnectAsync(selectedPeer.HostName, "{00001101-0000-1000-8000-00805F9B34FB}");
Run Code Online (Sandbox Code Playgroud)

3)连接到PC或其他手机以通过Obex发送文件

socket = new Windows.Networking.Sockets.StreamSocket();
await socket.ConnectAsync(_selectedDevice.HostName, "{00001105-0000-1000-8000-00805f9b34fb}");
Run Code Online (Sandbox Code Playgroud)

迁移到Windows Phone 8.1(SilverLight)后,这个东西不起作用:

await socket.ConnectAsync(selectedPeer.HostName, "{00001101-0000-1000-8000-00805F9B34FB}");
Run Code Online (Sandbox Code Playgroud)

我在连接期间遇到了运行时异常:找不到元素,数据不再可用,访问被拒绝

我已经拥有SilverLight功能ID_CAP_NETWORK,ID_CAP_PROXIMITY

我在Package.appmainfest中设置....

<m2:DeviceCapability Name="bluetooth.rfcomm">
  <m2:Device Id="any">
    <m2:Function Type="name:serialPort" />
    <m2:Function Type="name:obexObjectPush" />
    <m2:Function Type="name:obexFileTransfer" />        
  </m2:Device>    
Run Code Online (Sandbox Code Playgroud)

我试图使用"Rfcomm"类

var devicesInfoCollection = await DeviceInformation.FindAllAsync(
 RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
sensorCollection.Clear();
foreach (DeviceInformation dInfo in devicesInfoCollection)
{
  sensorCollection.Add(dInfo);
}
Run Code Online (Sandbox Code Playgroud)

连接

RfcommDeviceService rfcommService = await RfcommDeviceService.FromIdAsync(selectedDeviceInfo.Id);
socket = new StreamSocket();
await socket.ConnectAsync(rfcommService.ConnectionHostName, rfcommService.ConnectionServiceName, 
  SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
Run Code Online (Sandbox Code Playgroud)

断开

if (dataWriter != null)
{
   dataWriter.DetachStream();
   dataWriter.Dispose();
   dataWriter = null;
}                   
if (socket != null)
{
   socket.Dispose();
   socket = null;
}
Run Code Online (Sandbox Code Playgroud)

问题是它很奇怪.当我尝试连接,断开然后再连接我不能.设备从列表中消失.设备位于配对设备列表中,但它从SerialPort设备列表中消失.当我尝试连接时,我有"未找到元素".好像手机不会破坏蓝牙连接.我必须转到蓝牙设置并手动中断.

在这里观察到类似的问题.