-
Notifications
You must be signed in to change notification settings - Fork 13
/
SampleUser.cs
112 lines (94 loc) · 3.24 KB
/
SampleUser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR_OSX || UNITY_IOS
using UnityCoreBluetooth;
public class SampleUser : MonoBehaviour
{
public Text text;
private CoreBluetoothManager manager;
private CoreBluetoothCharacteristic characteristic;
// 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 != "M5Stack") && (peripheral.name != "M5StickC")) 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();
}
private bool flag = false;
private byte[] value = new byte[20];
private float vy = 0.0f;
// Update is called once per frame
void Update()
{
if (this.transform.position.y < 0)
{
vy = 0.0f;
transform.position = new Vector3(0, 0, 0);
}
else
{
vy -= 0.006f;
transform.position += new Vector3(0, vy, 0);
}
this.transform.Rotate(2, -3, 4);
if (flag == false) return;
flag = false;
text.text = $"Notify: {BitConverter.ToInt32(value, 0)}";
vy += 0.1f;
transform.position += new Vector3(0, vy, 0);
}
void OnDestroy()
{
manager.Stop();
}
private int counter = 0;
public void Write()
{
characteristic.Write(System.Text.Encoding.UTF8.GetBytes($"{counter}"));
counter++;
}
}
#endif