示例#1
0
    // Use this for initialization
    void Start()
    {
        UnityCoreBluetooth.CreateSharedInstance();

        UnityCoreBluetooth.Shared.OnUpdateState((string state) =>
        {
            Debug.Log("state: " + state);
            if (state != "poweredOn")
            {
                return;
            }
            UnityCoreBluetooth.Shared.StartScan();
        });

        UnityCoreBluetooth.Shared.OnDiscoverPeripheral((UnityCBPeripheral peripheral) =>
        {
            if (peripheral.name != "")
            {
                Debug.Log("discover peripheral name: " + peripheral.name);
            }
            if (peripheral.name != "Daydream controller")
            {
                return;
            }

            UnityCoreBluetooth.Shared.StopScan();
            UnityCoreBluetooth.Shared.Connect(peripheral);
        });

        UnityCoreBluetooth.Shared.OnConnectPeripheral((UnityCBPeripheral peripheral) =>
        {
            Debug.Log("connected peripheral name: " + peripheral.name);
            peripheral.discoverServices();
        });

        UnityCoreBluetooth.Shared.OnDiscoverService((UnityCBService service) =>
        {
            Debug.Log("discover service uuid: " + service.uuid);
            if (service.uuid != "FE55")
            {
                return;
            }
            service.discoverCharacteristics();
        });


        UnityCoreBluetooth.Shared.OnDiscoverCharacteristic((UnityCBCharacteristic 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);
        });

        UnityCoreBluetooth.Shared.OnUpdateValue((UnityCBCharacteristic characteristic, byte[] data) =>
        {
            this.value = data;
            this.flag  = true;
        });
        UnityCoreBluetooth.Shared.StartCoreBluetooth();
    }
示例#2
0
 void OnDestroy()
 {
     UnityCoreBluetooth.ReleaseSharedInstance();
 }
示例#3
0
    // Start is called before the first frame update
    void Start()
    {
        UnityCoreBluetooth.CreateSharedInstance();

        UnityCoreBluetooth.Shared.OnUpdateState((string state) => {
            Debug.Log("Bluetooth turned: " + state);
            if (state != "poweredOn")
            {
                return;
            }
            UnityCoreBluetooth.Shared.StartScan();
        });

        UnityCoreBluetooth.Shared.OnDiscoverPeripheral((UnityCBPeripheral peripheral) => {
            Debug.Log("Discovered peripheral: " + peripheral.name);
            if (peripheral.name == "Polar OH1 72838626" || peripheral.name == "Polar OH1 72852D2D")
            {
                UnityCoreBluetooth.Shared.StopScan();
                UnityCoreBluetooth.Shared.Connect(peripheral);
            }
        });

        UnityCoreBluetooth.Shared.OnConnectPeripheral((UnityCBPeripheral peripheral) => {
            Debug.Log("Connected to: " + peripheral.name);
            peripheral.discoverServices();
        });

        UnityCoreBluetooth.Shared.OnDiscoverService((UnityCBService service) => {
            if (service.uuid != "180D")
            {
                return;
            }
            Debug.Log("Discovered service: " + service.uuid);
            service.discoverCharacteristics();
        });

        UnityCoreBluetooth.Shared.OnDiscoverCharacteristic((UnityCBCharacteristic characteristic) => {
            string uuid  = characteristic.uuid;
            string usage = characteristic.propertis[0];
            Debug.Log("Discovered characteristic: " + uuid + ", usage: " + usage);
            if (usage != "notify")
            {
                return;
            }
            characteristic.setNotifyValue(true);
        });

        UnityCoreBluetooth.Shared.OnUpdateValue((UnityCBPeripheral peripheral, UnityCBCharacteristic characteristic, byte[] data) => {
            if (peripheral.name == "Polar OH1 72852D2D")
            {
                Debug.Log("Heart rate: " + data[1] + " from peripheral:" + peripheral.name);
                this.heartRate1 = data[1];
            }

            if (peripheral.name == "Polar OH1 72838626")
            {
                Debug.Log("Heart rate: " + data[1] + " from peripheral:" + peripheral.name);
                this.heartRate2 = data[1];
            }
        });
        UnityCoreBluetooth.Shared.StartCoreBluetooth();
    }