none
properly close and reconnect to a Bluetooth RFComm device RRS feed

  • Question

  • Hello everyone,

    I'm trying to connect my Windows Runtime (Phone) App to connect to a Bluetooth device that only supports serial communication (more precisely, it's a HC-06 module). I'm looking for paired devices, using PeerFinder class and AlternateIdentities Key. Then I select my Device from a list, and try to establish a connection with the HostName from PeerInfromation and ServiceName "1".

    However, this appears to only properly work when I just paired the device with my phone, and the App didn't try to establish a connection to it before. When I have a connection and then close it I can't reconnect to that same device again simply using the method I described above. Sometimes it would eventually establish a connection after about a minute. Usually I get this error:

    No more Data is available.

    And less frequently this:

    Element not found.

    Restarting the app doesn't help. 

    Now I know this has to be an issue with my code because I can use the app "BT Terminal" with my BT-Module flawlessly and with as many reconnects as I like, while it never takes more than 30s to connect. But when I use my app to connect and then try "BT Terminal" it won't work for it either.

    So I figured it might have to do something, with how I close the connection:

    public async void Disconnect(object state)
    {
        _IsConnected = false;
    
        if (Socket != null)
        {
            Socket.Dispose();
            Socket = null;
        }
        if (writer != null)
        {
            writer.Dispose();
            writer = null;
        }
        if (reader != null)
        {
            reader.Dispose();
            reader = null;
        }
    
        // raise event "DisConnected"
        DisConnected(this,null);
    }

    What am I missing here?

    Thanks in advance,

    Patrik


    • Edited by Partik Monday, December 15, 2014 3:59 PM
    Monday, December 15, 2014 3:57 PM

All replies

  • I am working this exact problem for other people. It's an issue that I am trying to get to the bottom of. I will update this thread when I know more.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, December 15, 2014 7:37 PM
  • Any new insights?
    Thursday, January 22, 2015 6:03 PM
  • I wish.  Our development team is investigating this now.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, January 22, 2015 6:19 PM
  • I'm not sure if that helps, but I just did some more testing und Debugging.

    In a blank Windows Runtime Project I implemented PeerFinder Paired device discovery and Connection. When Setting PeerFinder.AllowBluetooth=true , I would almost always get an "element not found" Error.

    Also, Device capability "Bluetooth.rfcomm" must be set.

    I still get "No more data available" a lot, but it would connect succesfully after calling FindAllPeersAsync again.

    Here ist my test Project. I didn't Play around with disconnects yet, but restarting the app with the Debugger works for the Moment.

    • Edited by Partik Thursday, January 22, 2015 6:58 PM
    Thursday, January 22, 2015 6:55 PM
  • Some more additional Information, especially for everyone depending on this:

    This issue exists since Silverlight 8.1/Runtime 8.1

    Phone Silverlight 8.0 Apps do not face these issues. A simple (StreamSocket)mySocket.Dispose() is enough to Close a Connection. Reconnects work flawlessly.

    Tuesday, January 27, 2015 4:26 PM
  • I was taking a class at UW and we ran into this issue on newer phones (nokia 1520), but not older ones.  Any updates on this thread?

    Thanks!

    Monday, April 13, 2015 3:59 AM
  • I wish.  Our development team is investigating this now.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Is this solved by now? Is this working in win10?
    Thursday, September 3, 2015 12:17 PM
  • @Matt Small

    Any news on this? I'm struggling with this issue on Win 10.

    Friday, October 20, 2017 10:25 AM