iOS & macOS Unity Native Plugin for Core Bluetooth framework
Download VideoCreator.unitypakcage from Releases and install it in your project.
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());
}
}
SeeMore SampleUser.cs
Get raw value form Daydream controller
Property | Target |
---|---|
Peripheral Name | Daydream controller |
Service UUID | FE55 |
Characteristic Usage | notify |
Get shared CoreBluetoothManager instance.
manager = CoreBluetoothManager.Shared;
manager.OnUpdateState((string state) =>
{
Debug.Log("state: " + state);
if (state != "poweredOn") return;
manager.StartScan();
});
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);
});
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();
});
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);
});
manager.OnUpdateValue((CoreBluetoothCharacteristic characteristic, byte[] data) =>
{
this.value = data;
this.flag = true;
});
manager.Start();
byte[] value = { 0x64, 0x68 };
characteristic.Write(value);