RFCOMM connection works unstable

1.8k views Asked by At

I have Windows Phone app, that controls a device by Bluetooth (Bluetooth App To Device). In WP8 it works good. This is code

1)searching all paired devices

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

2)connect to device

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

3)connect to PC or to other Phone to send file by Obex

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

After migration to Windows Phone 8.1(SilverLight) this thing doesn't work:

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

I've got a runtime exception during connection: element not found, data are not available any more, access denied

I already have got SilverLight capabilities ID_CAP_NETWORK, ID_CAP_PROXIMITY

I set .... in 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>    

I tried to use "Rfcomm" classes

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

Connection

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

Disconnection

if (dataWriter != null)
{
   dataWriter.DetachStream();
   dataWriter.Dispose();
   dataWriter = null;
}                   
if (socket != null)
{
   socket.Dispose();
   socket = null;
}

The problem is that it works very strange. When I try to connect, disconnect and then connect again I can't. Device disappears from list. Device is in list of paired devices but it disappears from list of SerialPort devices. When I try to connect I've got "Element not found". It seems like phone doesn't break Bluetooth connection. I have to go to Bluetooth settings and break it manually.

Similar problem was observed here.

0

There are 0 answers