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="http://schemas.android.com/apk/res/android"
package="com.example.unityplugin">
<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" />
</manifest>
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.