locked
Windows 8.1 Bluetooth Low Energy GATT javascript event handler hangs

    Question

  • I have a simple function that identifies the BLE devices with the simple key service (e.g. TI CC2541 sensor tag).  I identify the device, service and notify characteristic.  I then set the event handler and write the notify characteristic descriptor all as shown in this file on github in the discoverBleTags function.  The code seems to work but after several key events from the BLE device are processed, the event handler appears to stop.  Subsequent key presses are not processed.  If I rerun the discoverBleTags function, it will work briefly again but often the entire UI just hangs and I must kill the app.

    What is the best way to determine why the events are stopping?  I've tested the BLE device on another operating system (iOS) and the notifications happen reliably so I do not suspect a problem with the BLE device.

    If you don't want to view the code in github, here is the flow without error checking, output, etc.:

    function discoverBleTags() {
        var gatt = Windows.Devices.Bluetooth.GenericAttributeProfile;
        Windows.Devices.Enumeration.DeviceInformation.findAllAsync(gatt.GattDeviceService.getDeviceSelectorFromUuid(keyServiceUuid), null)
        .done(function (devicesWithKeyService) {
            for (var i = 0; i < devicesWithKeyService.length; i++) {
                gatt.GattDeviceService.fromIdAsync(devicesWithKeyService[i].id)
                .done(function (service) {
                    var notifyCharacteristic = service.getCharacteristics(keyCharacteristicUuid)[0];
                    notifyCharacteristic.addEventListener("valuechanged", simpleKeyEvent, false);
                    notifyCharacteristic.writeClientCharacteristicConfigurationDescriptorAsync(gatt.GattClientCharacteristicConfigurationDescriptorValue.notify);
                });
            };
        });
    };
    
    function simpleKeyEvent(args) {
        // print a message
    };

    Thanks!

    Tom

    Wednesday, October 30, 2013 6:27 PM

Answers

  • Hello,

    The issue with the code above is that the GattDeviceService instance you obtain by calling the FromIsAsync method is the gatekeeper for all interaction with the device, as a consequence of this, the "service" variable you obtain from FromIdAsync must be kept alive for as long as you use the device.

    If you take a look at the Bluetooth Generic Attribute Profile samples (http://code.msdn.microsoft.com/windowsapps/Bluetooth-Generic-5a99ef95#content)  the HeartRateService object (heart-rate-service.js) is defined as a singleton which explicitly manages the lifetime of the service object (instantiates it in the initializeHeartRateServiceAsync method and cleans it up in the "oncheckpoint" event handler).

    Without holding on to the GattDeviceService instance, the object will just get cleaned up along with any device resources being used or any registered event handlers.

    Not sure what the source of the hang is though, it could happen if the service object gets cleaned up while the event handler is actually running, or it could even be unrelated.

    After you've made sure that the service instance doesn't get released while you're using it, please let us know if the hang still happens, and we can help troubleshoot this further.

    Thank you,

    Florin


    • Edited by FlorinB [MSFT] Wednesday, November 6, 2013 1:38 AM
    • Marked as answer by trpropst Wednesday, November 6, 2013 3:09 AM
    Wednesday, November 6, 2013 1:34 AM

All replies

  • update:

    I built and deployed in the Release configuration and see the app actually terminate after this issue appears.  Below is the text from the Windows event report:

    Version=1
    EventType=MoAppHang
    EventTime=130276334380906944
    ReportType=3
    Consent=1
    ReportIdentifier=06f2c242-4196-11e3-be7a-6c71d9974a88
    IntegratorReportIdentifier=06f2c243-4196-11e3-be7a-6c71d9974a88
    NsAppName=praid:App
    Response.type=4
    Sig[0].Name=Package Full Name
    Sig[0].Value=72ac2404-e852-4fa5-a0be-9eaa1f759dd1_1.0.0.0_neutral__vs7ccs9ww9cca
    Sig[1].Name=Application Name
    Sig[1].Value=praid:App
    Sig[2].Name=Application Version
    Sig[2].Value=6.3.9600.16384
    Sig[3].Name=Application Timestamp
    Sig[3].Value=5215d0bb
    Sig[4].Name=Hang Signature
    Sig[4].Value=44ce
    Sig[5].Name=Hang Type
    Sig[5].Value=2097152
    DynamicSig[1].Name=OS Version
    DynamicSig[1].Value=6.3.9600.2.0.0.256.48
    DynamicSig[2].Name=Locale ID
    DynamicSig[2].Value=1033
    DynamicSig[22].Name=Additional Hang Signature 1
    DynamicSig[22].Value=44ceb9da512bb6d55af34592ff3eb595
    DynamicSig[23].Name=Additional Hang Signature 2
    DynamicSig[23].Value=5681
    DynamicSig[24].Name=Additional Hang Signature 3
    DynamicSig[24].Value=5681cfc5f3b44ce8ecbd131b8f1460f2
    DynamicSig[25].Name=Additional Hang Signature 4
    DynamicSig[25].Value=44ce
    DynamicSig[26].Name=Additional Hang Signature 5
    DynamicSig[26].Value=44ceb9da512bb6d55af34592ff3eb595
    DynamicSig[27].Name=Additional Hang Signature 6
    DynamicSig[27].Value=5681
    DynamicSig[28].Name=Additional Hang Signature 7
    DynamicSig[28].Value=5681cfc5f3b44ce8ecbd131b8f1460f2
    UI[3]=Microsoft WWA Host is not responding
    UI[4]=Windows can check online for a solution. If you close the program, you might lose information.
    UI[5]=Check for a solution and close the program
    UI[6]=Check for a solution and close the program
    FriendlyEventName=Stopped responding and was closed
    ConsentKey=AppHangXProcB1
    AppName=72ac2404-e852-4fa5-a0be-9eaa1f759dd1_vs7ccs9ww9cca!App
    AppPath=C:\Windows\System32\WWAHost.exe
    ReportDescription=A problem caused this program to stop interacting with Windows.
    NsPartner=windows
    NsGroup=windows8
    ApplicationIdentity=7C4F8E8B9894B52C2EF7EDB4EED76951

    Wednesday, October 30, 2013 7:26 PM
  • In each iteration, you are adding an event listener without removing the old one. Can you try unregistering the event once you are done with it?

    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Tuesday, November 5, 2013 5:09 PM
    Moderator
  • Thanks for looking at this, Prashant.

    I have tested this where I only registered an event listener / handler for the first device instance and see the same behavior.

    To your point though, is there a problem registering an event listener for each device instance?  Once I get this problem resolved, I do want to respond to notifications from multiple BLE devices.

    I should also note that I did not see this problem on the Win 8.1 developer preview.

    Tuesday, November 5, 2013 5:40 PM
  • I don't think it should be a problem, but I am checking further on this issue internally. I will keep you updated.

    Btw, right before the app hangs, can you step through and tell what line of code is it actually hanging on? When the UI becomes unresponsive and the app hangs, can you go to Task manager, go to the details tab, locate your process, right click and "Create a dump file"? Once you create the dump file, you can zip it and share via Skydrive.

    I don't see any bug reports around this area, but let's get a dump file to understand what is going on here.

    Thanks,

    Prashant.


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Wednesday, November 6, 2013 12:33 AM
    Moderator
  • Hello,

    The issue with the code above is that the GattDeviceService instance you obtain by calling the FromIsAsync method is the gatekeeper for all interaction with the device, as a consequence of this, the "service" variable you obtain from FromIdAsync must be kept alive for as long as you use the device.

    If you take a look at the Bluetooth Generic Attribute Profile samples (http://code.msdn.microsoft.com/windowsapps/Bluetooth-Generic-5a99ef95#content)  the HeartRateService object (heart-rate-service.js) is defined as a singleton which explicitly manages the lifetime of the service object (instantiates it in the initializeHeartRateServiceAsync method and cleans it up in the "oncheckpoint" event handler).

    Without holding on to the GattDeviceService instance, the object will just get cleaned up along with any device resources being used or any registered event handlers.

    Not sure what the source of the hang is though, it could happen if the service object gets cleaned up while the event handler is actually running, or it could even be unrelated.

    After you've made sure that the service instance doesn't get released while you're using it, please let us know if the hang still happens, and we can help troubleshoot this further.

    Thank you,

    Florin


    • Edited by FlorinB [MSFT] Wednesday, November 6, 2013 1:38 AM
    • Marked as answer by trpropst Wednesday, November 6, 2013 3:09 AM
    Wednesday, November 6, 2013 1:34 AM
  • Florin,

    Releasing the GattDeviceService was indeed my problem.  I'm no longer able to reproduce the hang.

    Sincere thanks to you and Prashant for taking the time to look at this.  You've been a huge help.

    Tom

    Wednesday, November 6, 2013 3:09 AM