private static void OnUpdateValueCallback(IntPtr characteristic, IntPtr data, long length)
        {
            var c = new CoreBluetoothCharacteristic(characteristic);

            byte[] result = new byte[length];
            Marshal.Copy(data, result, 0, (int)length);

            Shared.OnUpdateValueHandler(characteristic, result);
        }
Beispiel #2
0
    // Use this for initialization
    void Start()
    {
        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") && (peripheral.name != "M5StickC") && (peripheral.name != "M5Stack"))
            {
                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) =>
        {
            this.characteristic = characteristic;
            string uuid         = characteristic.Uuid;
            string[] usage      = characteristic.Propertis;
            Debug.Log("discover characteristic uuid: " + uuid + ", usage: " + usage);
            for (int i = 0; i < usage.Length; i++)
            {
                Debug.Log("discover characteristic uuid: " + uuid + ", usage: " + usage[i]);
                if (usage[i] == "notify")
                {
                    characteristic.SetNotifyValue(true);
                }
            }
        });

        manager.OnUpdateValue((CoreBluetoothCharacteristic characteristic, byte[] data) =>
        {
            this.value = data;
            this.flag  = true;
        });
        manager.Start();
    }