Coder Social home page Coder Social logo

huhaocong / blewinrtdll Goto Github PK

View Code? Open in Web Editor NEW

This project forked from adabru/blewinrtdll

0.0 1.0 0.0 2.95 MB

BLE for Unity 3d in Windows without UWP.

License: Do What The F*ck You Want To Public License

C++ 42.50% C 4.36% C# 52.84% Batchfile 0.30%

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 to install VisualStudio components when you open the project. The needed components are C++ Desktop and UWP, or if you want to save space, just the single components "MSVC C++ Buildtools", "Windows 10 SDK", ".NET Framework 4.7.2 SDK".
  • 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".
  • If you run into the error wait_for is not a member of winrt::impl, follow the steps on adabru#16 and leave a thumbs up. If enough thumbs accumulate, someone or me will try to make that more convenient.
  • 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.

Alternatives

win32 Bluetooth API, as used by https://github.com/DerekGn/WinBle (thanks to david-sackstein).

Bluetooth Framework by Soft Service Company. It is paid and you can get support. You also find answers on stackoverflow.

If you find more, you are welcome to report them, e.g. via opening an issue.

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.

As of now, .NET 4.6 or .NET Standard 2.0 allows you to access the WinRT API from C# in Unity, but only from the UWP and Xbox platforms. When Unity switches over to .NET Core, the API should be accessible from other platforms as well. However, the Unity Editor always uses Mono. Therefore, you may always need a dll solution for editor testing. If you plan on using the WinRT API directly from C# now through UWP/XBOX or in the future with .NET Core integration, know the dll's API is strongly leaned on Windows' own API. Actually, it is only a wrapper (plus some subtle thread handling). So doing so 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 abrahamh08 avatar ehauner avatar

Watchers

James Cloos 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.