This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

BLE masteremulator.dll - Problem with masteremulator. bond() method

Hi,

I have been experimenting with the master emulator .dll - I started with the HidDemo project which is written in C#. This seems to work perfectly well.

I have rewritten ( translated ?) the demo code into VB.NET ( Visual Studio Express 2010). Everything seems to be ok until I try to call masteremulator.bond(securityparameters). In my version this fails. As far as I can see the security parameters I am sending to the method are the same as those sent by the HidDemo bond function.

I have attached a word document with both sets of code and the securityparameters exposed. I have also attached attached the log that the masteremulator dll has churned out (sorry it is in verbose mode).

I fully expect to find that I am missing something glaringly obvious, but for the moment I can see the wood for the trees! :)

Thanks in advance to anyone who can spot the problem.

Regards

Godric

master_emulator_log.txt

  • HidDemo.sln (C#)

    void Bond() { SecurityParameters securityParams = new SecurityParameters(); securityParams.IoCapabilities = IoCapabilitiesEnum.KeyboardDisplay; securityParams.OobAvailability = OobAvailibilityEnum.OobNotAvailable; bool bondSuccess = masterEmulator.Bond(securityParams); if (!bondSuccess) { throw new Exception("Bonding failed."); } }

    BLETrial.sln VB Public Function Bond() As Boolean

        Dim securityParams As New Nordicsemi.SecurityParameters
        Dim bondSuccess As Boolean = False
    
        securityParams.IoCapabilities = IoCapabilitiesEnum.KeyboardDisplay
        securityParams.OobAvailability = OobAvailibilityEnum.OobNotAvailable
        bondSuccess = masteremulator.Bond(securityParams)
        If Not (bondSuccess) Then
    
            frmErrorMessage.txtClassName.Text = classname
            frmErrorMessage.txtVersion.Text = version
            frmErrorMessage.txtSystemMessage.Text = ""
            frmErrorMessage.txtApplicationMessages.Text = "Bond() - Bonding Failed"
    
        End If
        Return bondSuccess
    
    End Function
    
  • Well I have made some progress! I had tried to call masteremulator.bond from the main application thread - so bond might have acted as a blocking function. Calling it from a separate threat using

    Dim taskOne = Task.Factory.StartNew(Sub() DoBond())

    in my onConnected event handler seems to have done the trick. The fact that DiscoverPipes is failing is not too much of a concern at the moment because I have probably not set up the pipes properly !!

    Public Sub DoBond()

        If Bond() = True Then
            DiscoverPipes()
            OpenRemotePipes()
            ReadBatteryLevel()
            WriteHidProtocolMode()
        End If
    
    End Sub
    

    regards

    Godric

  • One thing you should be aware of when using the Master Emulator API is that you can not block or call Master Emulator API functions in any of the callback methods you receive. As you can see, the demo applications solve this by using the BeginInvoke construct or similar to do everything in the callbacks in a background thread, for example like this from the ProximityDemo:

    
    this.BeginInvoke((MethodInvoker)delegate()
    {
    	lblConnectedSymbol.BackColor = Color.LightGreen;
    	btnConnectDisconnect.Text = AppText.Disconnect;
    	if (pipeDiscoveryComplete)
    	{
    		grpRight.Enabled = true;
    		pnlRight.BackColor = Color.LightGreen;
    	}
    	isConnected = true;
    });
    
    

    or this from the HidDemo:

    
    /* The connection is up, proceed with Bond and pipe discovery. 
        * Using a background task in order not to block the event caller. */
    Task.Factory.StartNew(() =>
    {
        try
        {
            Bond();
            DiscoverPipes();
            OpenRemotePipes();
            ReadBatteryLevel();
            WriteHidProtocolMode();
            AddToLog("Ready");
        }
        catch (Exception ex)
        {
            LogErrorMessage(string.Format("Exception in OnConnected: {0}", ex.Message),
                ex.StackTrace);
        }
    });
    
    

    If you don't do this, you will get strange errors, for example failure to discover services at all, hangs or possibly just bogus errors returned. I'm afraid I'm not sure how to do similar things in VB, but I assume it's possible.

  • I have not made very much further progress I am afraid. I just don't seem to be able to call masterEmulator.DiscoverPipes() properly. It always throws me back out with a 'no pipes have been setup' message. I have gone as far as calling it from a separate thread with task.factory.StartNew - and that doesn't seem to make any difference.

    I have attached a .txt file with the code involved. Examples of how the classes are setup before DiscoverPipes is called - and the ex.StackTrace I get back. ( I don't know whether this will be of any help to Nordic).

    I have also included the logs and similar diagnostics for HidDemo by way of comparison.

    I may need to have to think of developing the application in Visual C - but I would prefer to avoid this if I can.

    Regards

    Godric

    BLEDiagnostics.txt

  • I'm sorry for the delay in answering this, but I just realized that there is a difference between Invoke and BeginInvoke, at least in C#.

    Invoke will wait for the invoked code to complete, while BeginInvoke just starts the code and then returns. Since the problem you see most likely comes from the callback function blocking, you should make sure that you use BeginInvoke and not Invoke, or whatever VB's equivalent is.

Related