RFCOMM connection works unstable
Asked Answered
C

0

7

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.

Causation answered 25/11, 2014 at 9:4 Comment(7)
I am working with the Microsoft Bluetooth Development team at this time on this issue. You are one of many people having this problem. I hope to get to the bottom of this issue soon.Pressing
Have you found a solution for this? I am having a similar issue where it works on one device but not another. One note is the device that works runs Intel's Bluetooth driver: 17.0.1401.422 while the one that does not work runs Intel's Bluetooth driver: 17.0.1405.460Valdes
@user465427 there are no solution yet. But guys from Microsoft are working on this issue.Causation
The hardware manufacture walked me through removing the 17.0.1405.460 version of the Intel Bluetooth drivers and the installation of the older 17.0.1401.422 version. Once we did that my application started working again. So the issue, for me, was the newer drivers.Valdes
@Matt Small, I test my app with last Windows 10 Mobile TP (10136) and still get the same problem. My bluetooth module model is BTM-160.Causation
Have you solved the issue?Lifeboat
@Carl, No. Now I have two same devices (one with old bluetooth module and one with new bluetooth module). First device works only with WP8 app, but new device works with WP8.1. I think that it's a problem with compatibility some bluetooth modules with WP8.1Causation

© 2022 - 2024 — McMap. All rights reserved.