UnityCoreBluetooth
Unity Bluetooth Plugin for iOS & macOS Editor
Install / Use
/learn @fuziki/UnityCoreBluetoothREADME
UnityCoreBluetooth
iOS & macOS Unity Native Plugin for Core Bluetooth framework
Example: Get Raw Value from Daydream Controller
<img src="docs/videos/daydream.gif" width="800">Requirements
Unity
- Unity 2021.X
- Unity 2022.X
- Unity 2022.2+ (Apple Silicon)
Platforms
- iOS 14+
- macOS 12+ (Apple Silicon and Intel)
Installation
Download VideoCreator.unitypakcage from Releases and install it in your project.
Set Info.plist
Set privacy - bluetooth always usage description in Info.plist.
You can also set it in PostProcessBuild.
public class PostXcodeBuild
{
[PostProcessBuild]
public static void SetXcodePlist(BuildTarget buildTarget, string pathToBuiltProject)
{
if (buildTarget != BuildTarget.iOS) return;
var plistPath = pathToBuiltProject + "/Info.plist";
var plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath));
var rootDict = plist.root;
rootDict.SetString("NSBluetoothAlwaysUsageDescription", "use ble");
File.WriteAllText(plistPath, plist.WriteToString());
}
}
Features
- Peripheral
- [x] Get name
- [x] Search services
- Service
- [x] Get uuid
- [x] Search characteristics
- Characteristic
- [x] Get uuid
- [x] Get properties
- [x] Write value
- [ ] Read value
- [x] Receive notify
Usage
Example (Get Raw Value from Daydream Controller)
SeeMore SampleUser.cs
About Daydream Controller
Get raw value form Daydream controller
| Property | Target | |--|--| | Peripheral Name | Daydream controller | | Service UUID | FE55 | | Characteristic Usage | notify |
1. Get CoreBluetoothManager instance
Get shared CoreBluetoothManager instance.
manager = CoreBluetoothManager.Shared;
2. Start Scan On PowerOn
manager.OnUpdateState((string state) =>
{
Debug.Log("state: " + state);
if (state != "poweredOn") return;
manager.StartScan();
});
3. Discover And Connect Daydream Controller
manager.OnDiscoverPeripheral((CoreBluetoothPeripheral peripheral) =>
{
if (peripheral.name != "")
Debug.Log("discover peripheral name: " + peripheral.name);
if (peripheral.name != "Daydream controller") return;
manager.StopScan();
manager.ConnectToPeripheral(peripheral);
});
4. Discover Services And Characteristic On Connected
manager.OnConnectPeripheral((CoreBluetoothPeripheral peripheral) =>
{
Debug.Log("connected peripheral name: " + peripheral.name);
peripheral.discoverServices();
});
manager.OnDiscoverService((CoreBluetoothService service) =>
{
Debug.Log("discover service uuid: " + service.uuid);
if (service.uuid != "FE55") return;
service.discoverCharacteristics();
});
5. Enable Notify
manager.OnDiscoverCharacteristic((CoreBluetoothCharacteristic characteristic) =>
{
string uuid = characteristic.uuid;
string usage = characteristic.propertis[0];
Debug.Log("discover characteristic uuid: " + uuid + ", usage: " + usage);
if (usage != "notify") return;
characteristic.setNotifyValue(true);
});
6. Handle Notify Value
manager.OnUpdateValue((CoreBluetoothCharacteristic characteristic, byte[] data) =>
{
this.value = data;
this.flag = true;
});
7. Start CoreBluetoothManager
manager.Start();
8. Write Value
byte[] value = { 0x64, 0x68 };
characteristic.Write(value);
Examples
UnityExample
- Example for Unity
- Unity Version
- Work on Unity Editor And iOS device
- Show raw value from Daydream controller or M5StickC
M5Peripheral
- Peripheral Example for M5StickC (Plus)
- Support write and notify
NativeExamples
DaydreamExample iOS
- Native Examample for iOS
- Show raw value from Daydream controller
DaydreamExample macOS
- Native Examample for macOS
- Show raw value from Daydream controller
Related Skills
node-connect
347.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
