locked
bluetooth communication on windows 8.1

    Question

  • Hello everyone, I am having a problem with  bluetooth communication on windows 8.1 and I would really appreciate any suggestion.
    I am writing a windows store application that receives and processes data  from health devices (scales, glucometer, ECG apparatus, etc.) via bluetooth.
    When I first pair a new device (for example the scales) and run the application I am able to receive data from the device, and so for the rest of the day, but afterwards it randomly  stops working, that happens sometimes the next day, when I reboot the PC, or if  I recompile the solution.
    When it does not work the **OnConnectionReceivedAsync** method never gets called; but if I remove the bluetooth pairing and then I perform the pairing operation again 9 times out of 10 it starts working again.

    The above behavior happens with all the devices and not just a particular one.

    Here is the code I use:

    private RfcommServiceProvider rfcommProvider;
    private StreamSocketListener socketListener;
    private IAsyncOperation<RfcommDeviceService> connectService;
    private StreamSocket socket;
    private DataReader reader;
    private DataWriter writer;

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        Reset();
        await GetReadyAsync();
    }

    private void Reset()
    {
        if (reader != null)
        {
            reader.DetachStream();
            reader = null;
        }

        if (writer != null)
        {
            writer.DetachStream();
            writer = null;
        }

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

        if (rfcommProvider != null)
        {
            rfcommProvider.StopAdvertising();
            rfcommProvider = null;
        }

        if (socket != null)
    socket = null;

        if (socketListener != null)
        {
            socketListener.Dispose();
            socketListener = null;
        }
    }


    private async Task GetReadyAsync()
    {
        try
    {
    DeviceInformationCollection deviceInformationCollection = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));

    DeviceInformation deviceInformation = deviceInformationCollection.FirstOrDefault(x => x.Name.StartsWith("11"));

    connectService = RfcommDeviceService.FromIdAsync(deviceInformation.Id);

    rfcommProvider = await RfcommServiceProvider.CreateAsync(RfcommServiceId.SerialPort);

    if (rfcommProvider != null)
    {
    // Create a listener for this service and start listening
    socketListener = new StreamSocketListener();

    socketListener.ConnectionReceived += OnConnectionReceivedAsync;

    await socketListener.BindServiceNameAsync(rfcommProvider.ServiceId.AsString(), SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);

    rfcommProvider.StartAdvertising(socketListener);
    }
    }
    catch (Exception ex)
    {
    //to be handled..
    }
    }

    private async void OnConnectionReceivedAsync(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
    {
        try
        {
    // Stop advertising/listening so that we’re only serving one client
            rfcommProvider.StopAdvertising();

            // Don't need the listener anymore
            socketListener.Dispose();
            socketListener = null;

    socket = args.Socket;

            writer = new DataWriter(socket.OutputStream);
            reader = new DataReader(socket.InputStream);

            //........ more code that reads and writes to socket to extract data.
        }
        catch (Exception ex)
        {
            var m = ex.Message;
        }
     }

    Monday, September 22, 2014 1:51 PM

All replies

  • What exactly stops working - the Bluetooth connection or does the app crash? Does your device have a "connect" button that you need to press in order for it to reconnect to the PC?

    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, September 22, 2014 7:29 PM
    Moderator
  • First of all thank you for your reply.
    no exception is thown, simply the OnConnectionReceivedAsync never gets called, as if no device was trying to establish a communication, but, if I remove and re-establish the pairing that same code works, probably for the rest of the day and then the next day no longer works.
    The device, for example the scale, tries to communicate with the application immediately after stepping down from it for 20 seconds and if nobody answers it emits a warning and turns off.

    Wednesday, September 24, 2014 9:37 AM
  • I have a bug open for this behavior now. I will update you 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.

    Friday, October 3, 2014 5:10 PM
    Moderator
  • Hello Matt,

    any news for my bug?

    Thank you.

    Tuesday, October 28, 2014 9:00 AM
  • Ypu may refer to this link for more details

    http://blogs.msdn.com/b/flecoqui/archive/2013/12/13/windows-store-and-windows-phone-app-to-app-communication-over-bluetooth.aspx

    Monday, November 3, 2014 7:55 AM