Coder Social home page Coder Social logo

blewinrtdll's Introduction

UWP BLE packed as C++ winrt dll

To try it out in Unity, open the project "BleWinrtDll Unity" in Unity. Then start the scene "Assets/Scenes/Demo.scene".

This VisualStudio-project compiles to a C++-dll that can be imported into Unity. It wraps a part of the UWP BLE API inside a dll. The dll can be simply dropped into your Unity project and be used in the Unity Editor and the Windows standalone version.

Screenshot of the demo scene.

The Demo.scene in this repo uses non-blocking calls to avoid creating Threads. I would recommend to use threads, though. After getting acquainted with the standard C# Thread API and after making sure you're closing all your threads OnApplicationQuit (Unity API) to avoid Unity Editor freezes, using them is the easier workflow for BLE in my opinion. You'll need less lines of code and you can have less latency. For a threaded version, you can try out Joelx's Unity Ble Demo.

Build

There is a prebuilt dll included in this repo, BleWinrtDll Unity\Assets\BleWinrtDll.dll or DebugBle\BleWinrtDll.dll (both are the same). But you can also build the dll yourself in VisualStudio. Follow these steps:

  • Open the file BleWinrtDll.sln with VisualStudio (tested with Community 2019). You may be asked install VisualStudio components when you open the project. The needed components are C++ Desktop and UWP.
  • Choose configuration "Release" and "x64" (I think it must match your machine architecture).
  • In the project explorer, right-click the project "BleWinrtDll" and choose "Compile".
  • Wait until the compilation finishes successfully.

Now you find the file BleWinrtDll.dll in the folder x64/Release. You can copy this dll into your Unity-project. To try it out, you can also copy the file into the DebugBle folder (replacing the existing file) and start the DebugBle project. If your computer has bluetooth enabled, you should see some scanned bluetooth devices. If you modify the file DebugBle/Program.cs and change the device name, service UUID and characteristic UUIDs to match your specific BLE device, you should also receive some packages from your BLE device.

Background

The Unity Editor is working with .NET-Framework but Microsoft's BLE API is only usable with .NET-Core. There is a C#-wrapper for .NET-Core functions, but these wrappers don't work in Unity for Unity-specific reasons. You can find some details in this blog-post by Mike Taulty. The blog-post also motivated me during the development of this dll.

With Unity's future integration of C# version 5 this repo most probably should become obsolete as C# version 5 is meant to merge .NET-Framework and .NET-Core so you should be able to call Windows' BLE API directly from C# then. Still this repo might be useful if you want to use BLE earlier. Furthermore the dll's API is strongly leaned on Windows' own API, actually it is only a wrapper (plus some subtle thread handling). So switching to C# 5 later shouldn't be much work.

This project is based on Microsoft's Bluetooth Low Energy sample. It contains UWP and the corresponding C++-winrt code for every function that you find in this repo.

Furthermore I used the included 420-slide presentation on bluetooth low energy to get an understanding of what maximum throughput and latency can be achieved with BLE. I found it on the web but don't know the author.

blewinrtdll's People

Contributors

adabru avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.