Bluetooth Low Energy (BLE) API for Unity

For Unity development, what APIs should we use to interface the Magic Leap 2 with a BLE device with a non-bonded GATT service? Also, is there any example we can reference?

1 Like

I did a little research on my end and found a class called MLBluetoothLE.
However, MLBluetoothLE does not seem to be a class that can acquire Bluetooth information.

Magic Leap 1 provided an API to access Blueooth LE information, but Magic Leap 2 does not seem to have one at this time.

It is unclear if this will be added in the future.
If anyone else has any pointers on this matter, please respond.

Sadao Tokuyama

On ML2 for Bluetooth we support standard Android SDK APIs with exception for GATT Server APIs that are not supported.

See here:

Does this mean that it is possible to access the Bluetooth API included in the Android SDK directly from Unity? I just don't personally know how to implement this.
This is not creating a native Android plugin, is it?

I have personally used Android standard bluetooth HID devices on Magic Leap 2. For me, I used a bluetooth scanner, which essentially id's as a keyboard in Android.This is the device I used:

What API did you use for the Bluetooth Barcode Scanner?

Note: The Barcode scanner is a Bluetooth device, but it may provide insights on what we could use for Bluetooth Low Energy APIs via GATT interfaces.

Like the previous controller battery issue, I believe this is a major, fatal problem with the Magic Leap Developer Portal documentation. There is far too little information.

I'm already confused about the support for Wacom tablets.

For the moment, this is a BLE post, so I'd like to see a clarification of the questioner's clarification.

I would also like to have a list of features supported by Magic Leap 2 that work with Unity's Android API at a glance. If possible, I think we should even present sample code.

There are many APIs that were closed in Magic Leap 1 because they provided functionality but did not provide any examples of use.

I would have liked to see this resolved while this site was open to only a few early access developers, i.e., before it was opened to the general public.

Sadao Tokuyama

I'd like to remain on-topic, but we have an open Pull Request from @cdorff to include more information about Controller battery information in the docs.

Wacom tablets are supported with their basic HID-like compatibility.

I am waiting on a reply internally to get better clarification on GATT communication. In short, yes, there is support. Standby, please.

1 Like

Sengelman, Have there been any updates regarding Bluetooth Low Energy (BLE) API (GATT services/characteristics) for Unity?

1 Like

Magic Leap 2 supports AOSP Bluetooth APIs for GATT client. If you want to use GATT in your application, you can use an existing 3rd party Unity BLE plugin for Android or you can write your own java AAR plugin which you import and use in Unity.
In case you decide to develop your own plugin, I explain here in more detail (it’s exactly the same process as any other Android device):

You can use Android studio to create an AAR Plugin, for this part take a look at Android developers documentation. , there you can find the functions you will need.
The process is the following:

  • First, you will have to set up the Bluetooth Adapter.
  • Start the scanning
  • For all devices you find, take the one with the name or MAC address you want.
  • Once you have detected the device use connectGatt to establish a connection with it.
  • Call discoverServices() where you check the Service and Characteristic you are interested in. You have to know the UUID of the characteristic you want to read from in your device.
  • You have to setCharacteristicNotification and set the Descriptor to be able to subscribe to new values of that characteristic.
  • After that, every new value from the device will be read from onCharacteristicChanged() callback function.

Also be sure you specify the correct permissions in the Manifest file you are using in Android Studio for building the AAR Plugin:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
   <uses-permission android:name="android.permission.BLUETOOTH" />
   <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
   <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
   <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
   <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

The unity part is quite simple, just place your AAR plugin built from Android Studio in Assets/Plugin/Android/ and you create a script to communicate with that plugin using AndroidJavaClass and AndroidJavaObject
Check this link for more documentation.

By the way, for debugging I recommend you to use Log.v() for Java/Android part and visualize it using AndroidStudio Logcat.
Hope this explanation helps you, please let me know if you need any more clarifications.

Sabanqueri, Thank you for the help. A quick follow-up question:

Can you help us with how we could debug using a Unity BLE plugin for Andriod?

Currently, we tried a Unity BLE plugin for Andriod however, when we try to debug the code by connecting (Unity->Visual Studio) to (zero-iteration->device mode), this approach appears to be reading the blue tooth LE from our laptop, which is running Unity and not the Magic Leap 2 device.

Are we debugging correctly?

1 Like

Zero Iteration is a host-side tool which runs apps on host and uses the device only for input and output of sensor data. That means, when running Zero Iteration the Android plugin will be executed on laptop. The only way to debug this functionality would be to create a build and run the application directly on device, checking the Logs with AndroidStudio Logcat or The Lab Log window.

1 Like

Zero Iteration bas been renamed to the Magic Leap App Simulator.