Difficulties with New Windows 10 C# SDK

Hello all,

I am attempting to build a new metawear .net console application using the new Metawear.CSharp nuget package. 

I've built a very simple application to test out the functionality, I'll list the code here.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MbientLab.MetaWear.Win10;
using Windows.Devices.Bluetooth;
using MbientLab.MetaWear;
using System.Threading;
using MbientLab.MetaWear.Peripheral;

namespace SensorServer
{
    class SensorServer
    {
        BluetoothLEDevice device;

        async void Run()
        {
            await MacAddrToIMetaWearBoard(233414921927693);
            IMetaWearBoard metawear = Application.GetMetaWearBoard(device);
            await metawear.InitializeAsync();
            ILed led;
            if ((led = metawear.GetModule<ILed>()) != null)
                Console.WriteLine("LED module is present");
        }

        public async Task MacAddrToIMetaWearBoard(ulong mac)
        {
            device = await BluetoothLEDevice.FromBluetoothAddressAsync(mac);
            Console.WriteLine(device == null);
        }

        static void Main(string[] args)
        {
            SensorServer server = new SensorServer();
            server.Run();
            while (true)
                Thread.Sleep(100);
        }
    }
}

However, when I attempt to initialize the metawear I get the following exception:

metawear: Could not deserialize board attributes
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at MbientLab.MetaWear.Win10.Application.IO.<LocalLoadAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MbientLab.MetaWear.Impl.MetaWearBoard.<InitializeAsync>d__46.MoveNext()

Any insight? I can't find any documentation on this exception.


Comments

  • edited September 2017
    That is simply a warning message and does not mean the app has crashed.

    Since you are running a dotnet console app, you are most likely running into the same issue as this user:
  • Ah okay fair enough, so I can safely pipe that output to somewhere else so it doesn't look so ugly?

    I suppose the exception that is actually crashing the app is as follows:

    System.InvalidOperationException occurred
      HResult=0x80131509
      Message=GATT service '326a9000-85cb-9195-d9dd-464cfbbae75a' does not exist
      Source=MbientLab.MetaWear.DotNet
      StackTrace:
       at MbientLab.MetaWear.Impl.MetaWearBoard.<InitializeAsync>d__46.MoveNext()
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
       at SensorServer.SensorServer.<Run>d__1.MoveNext() in C:\Users\Daniel Clarke\Documents\GitRepositories\Work\Metawear_Cadence\SensorServer\SensorServer.cs:line 22
       at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_1(Object state)
       at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
       at System.Threading.ThreadPoolWorkQueue.Dispatch()
       at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

    Line 22 is where the program calls the initialize async method. Do you think the two exceptions are related?
  • edited September 2017
    I may have resolved that by just moving the sensor closer to the receiver. But I am also getting this exception:

    System.NullReferenceException occurred
      HResult=0x80004003
      Message=Object reference not set to an instance of an object.
      Source=MbientLab.MetaWear.DotNet
      StackTrace:
       at MbientLab.MetaWear.Impl.MetaWearBoard.<InitializeAsync>d__46.MoveNext()
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
       at SensorServer.SensorServer.<Run>d__1.MoveNext() in C:\Users\Daniel Clarke\Documents\GitRepositories\Work\Metawear_Cadence\SensorServer\SensorServer.cs:line 22
       at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_1(Object state)
       at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
       at System.Threading.ThreadPoolWorkQueue.Dispatch()
       at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

    It's unclear to me what the null reference is as I've made sure that the bluetoothledevice is getting assigned properly and (as far as I can tell) the IMetawearBoard is also getting assigned.
  • See the thread linked in my previous post.
  • edited September 2017
    Thanks for adding my reference. 
    1. Creators update made some breaking changes to my previously working dotNet application, and I have not able to get it resolved ever since. 
    2. Working with UWP on windows 10, I am unsure about the use case for the WinRT Nuget package vs CSharp package which is what is referenced in all sample UWP apps now. I am curious if there would be any reason to continue using WinRT package on Win 10.
    3. Going fwd with dotnet, I upgraded my firmware to 1.3.4 which breaks quite a few things, I wanted to be able to push the older firmware onto the hardware (MetamotionR) to use it with older windows editions. It would be great if I could be pointed to something.
  • edited September 2017
    Please post your specific questions in their own thread; do not hijack existing threads.
  • edited September 2017
    @Eric So you're saying that the null reference exception is related to the fact that the access is denied to the gatt characteristic? I just wasn't seen that in the stack trace.
  • Yes, that is correct.
  • edited September 2017
    I have the same problem with the same exceptions.
    Chasing what is causing the exception:
    The method WriteCharacteristicAsync in MbientLab.MetaWear.Win10.BluetoothLeGatt is not successful in getting the Windows BluetoothLEDevice object to return the corresponding GATT characteristic to the service for COMMAND_GATT_CHAR Guids. All this needs to happen before we write the value to the device.

    However the method EnableNotificationAsync which has to do similar things for NOTIFY_CHAR Guids is successful.
  • So it looks like calling `GetCharacteristicsForUuidAsync` will eventually fail at some point?  

    Does applying the fixes mentioned in the MSDN thread fix the issue?
  • edited September 2017
    Yes. And going by the history, there is a fair bit of chance that GetGattServicesForUuidAsync could fail too.

    And those fixes did not solve the problem for me. The fixes suggested are reg permissions for the exe, but my entry point into ble calls is through the api dlls.
  • Anyone got the CSharp SDK to work on DotNet after Win 10 Fall Creators Update?
  • I can't check as my dev PC hasn't received the FCU.
  • Coming back to this after a few iterations on the creator's update, I now get the following when I attempt to call the following:

    IMetaWearBoard metawear = Application.GetMetaWearBoard(device);
    await metawear.InitializeAsync();

    This seems to fail with the following exception:

    System.InvalidOperationException: 'GATT service '326a9000-85cb-9195-d9dd-464cfbbae75a' does not exist'

    I've checked a few possibilities, and my bluetoothLEDevice is being properly assigned. Is this still just an issue with Windows 10 trying to get at the gatt characteristics?
  • I have not encountered any "GATT service does not exist messages" with the Fall Creators Update.  I did make some changes to the BLE code for console apps that works well with the FCU on my machine.  I'll push those changes to GitHub later today.
  • I pushed a checkpoint commit to GitHub.  Let me know if this iteration of the C# SDK works for your Win10 device:
  • Hi Eric,

    I'm having the same issue as Uldrendan, getting the 'GATT service does not exist' message. This happened when I updated my computer to the Fall Creators Update. I know that this may be a silly question, but how do I use the C# SDK that you linked instead of the one that my application is using now?

    Thanks!
    Dan
  • Download the code and build the library in VS2017.  Then, add a reference to the compiled assembly in your project.

    Alternatively, since you just want to check if the new code is properly working, you can instead add a UWP app to the C# SDK solution that references the MetaWear.UWP project and code a simple app that turns on the LED.
  • I was able to download the code and build the library. I've tried to look up how to add the reference to my the complied assembly in my project, but I haven't had any luck. Could you help with me this and/or point me to a relevant resource?
  • Sorry for the slow reply. Alright, built the dotnet assembly and added a reference to it. I'm still getting the same warning (metawear: could not deserialize board attributes) which is fine. But the exception I ultimately get is this:

    System.InvalidOperationException: 'Failed to enable notifications (status = 1)'

    Code has not changed. I feel like we're getting close though!
  • I tested the aforementioned commit on two other PCs (Integrated Intel adapter and Broadcom BT dongle) and the SDK is working fine on those machines.  

    I suspect it could be a BT adapter issue on your end stemming from the first creator's update.  I suggest you try using USB BT dongle and see if that resolves your issue.

    You should also write a simple dotnet console app that uses the BluetoothLE APIs to see if you can discover characteristics and enable notifications.


  • Will do Eric. Do you see any reason that the code I have posted would fail otherwise? Am I making any bad calls to the Metawear API?
  • Update! For some reason something was accessing the characteristic that the initialization function needed, so I've torn it down and it's working. Thanks for the help.
  • So, to confirm, the checkpoint commit is working as is for you?
  • Yes, works great. Nuget package still does not, but what you have sent does.
  • C# SDK v0.3.0 was just pushed to NuGet with the FCU changes.
This discussion has been closed.