/
SampleLightControl_Central.cs
154 lines (131 loc) · 5.26 KB
/
SampleLightControl_Central.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using CoreBluetooth;
using UnityEngine;
using UnityEngine.UI;
namespace CoreBluetoothSample
{
public class SampleLightControl_Central : MonoBehaviour, ICBCentralManagerDelegate, ICBPeripheralDelegate
{
[SerializeField] SampleLightControl_RGBSliders _rgbSliders;
[SerializeField] Image _colorImage;
[SerializeField] SampleLightControl_Header _header;
CBCentralManager _centralManager;
CBPeripheral _peripheral;
CBCharacteristic _lightControlCharacteristic;
Color32 _writeValue;
bool _waitingForWrite = false;
float _writeInterval = 0.1f;
float _latestWriteTime = 0;
void Start()
{
var initOptions = new CBCentralManagerInitOptions() { ShowPowerAlert = true };
_centralManager = new CBCentralManager(this, initOptions);
_rgbSliders.OnColorChanged.AddListener(OnColorChanged);
}
void Update()
{
if (_waitingForWrite)
{
if (Time.time - _latestWriteTime < _writeInterval) return;
_latestWriteTime = Time.time;
TurnLedOn(_writeValue);
_waitingForWrite = false;
}
}
void ICBCentralManagerDelegate.DidUpdateState(CBCentralManager central)
{
if (central.State == CBManagerState.PoweredOn)
{
_header.SetStateText("Scanning...");
central.ScanForPeripherals(new string[] { SampleLightControl_Data.ServiceUUID });
}
}
void ICBCentralManagerDelegate.DidDiscoverPeripheral(CBCentralManager central, CBPeripheral peripheral, int rssi)
{
_header.SetStateText("Connecting...");
_peripheral = peripheral;
peripheral.Delegate = this;
central.StopScan();
central.Connect(peripheral);
}
void ICBCentralManagerDelegate.DidConnectPeripheral(CBCentralManager central, CBPeripheral peripheral)
{
_header.SetStateText("Connected");
peripheral.DiscoverServices(new string[] { SampleLightControl_Data.ServiceUUID });
}
void ICBCentralManagerDelegate.DidFailToConnectPeripheral(CBCentralManager central, CBPeripheral peripheral, CBError error)
{
_header.SetStateText("Scanning...");
_lightControlCharacteristic = null;
central.ScanForPeripherals(new string[] { SampleLightControl_Data.ServiceUUID });
}
void ICBCentralManagerDelegate.DidDisconnectPeripheral(CBCentralManager central, CBPeripheral peripheral, CBError error)
{
_header.SetStateText("Scanning...");
_lightControlCharacteristic = null;
central.ScanForPeripherals(new string[] { SampleLightControl_Data.ServiceUUID });
}
void ICBPeripheralDelegate.DidDiscoverServices(CBPeripheral peripheral, CBError error)
{
if (error != null)
{
Debug.LogError($"[DidDiscoverServices] error: {error}");
return;
}
foreach (var service in peripheral.Services)
{
peripheral.DiscoverCharacteristics(new string[] { SampleLightControl_Data.LightControlCharacteristicUUID }, service);
}
}
void ICBPeripheralDelegate.DidDiscoverCharacteristics(CBPeripheral peripheral, CBService service, CBError error)
{
if (error != null)
{
Debug.LogError($"[DidDiscoverCharacteristics] error: {error}");
return;
}
foreach (var characteristic in service.Characteristics)
{
if (characteristic.UUID == SampleLightControl_Data.LightControlCharacteristicUUID)
{
_lightControlCharacteristic = characteristic;
_header.SetStateText("Ready");
return;
}
}
}
void ICBPeripheralDelegate.DidWriteValueForCharacteristic(CBPeripheral peripheral, CBCharacteristic characteristic, CBError error)
{
if (error != null)
{
Debug.LogError($"[DidWriteValueForCharacteristic] error: {error}");
return;
}
}
void TurnLedOn(Color32 color)
{
if (_lightControlCharacteristic == null) return;
var data = SampleLightControl_Data.GetLedOnData(color);
_peripheral.WriteValue(data, _lightControlCharacteristic, CBCharacteristicWriteType.WithResponse);
}
void OnColorChanged(Color32 color)
{
_colorImage.color = color;
_writeValue = color;
_waitingForWrite = true;
}
public void OnClickRandomColor()
{
if (_lightControlCharacteristic == null) return;
var color = new Color32((byte)UnityEngine.Random.Range(0, 255), (byte)UnityEngine.Random.Range(0, 255), (byte)UnityEngine.Random.Range(0, 255), 255);
_rgbSliders.SetColor(color);
}
void OnDestroy()
{
if (_centralManager != null)
{
_centralManager.Dispose();
_centralManager = null;
}
}
}
}