Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Assets/CoreBluetooth/Samples/01_LightControl.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}

public void DidUpdateState(CBCentralManager central)
{
if (central.State == CBManagerState.PoweredOn)
{
_header.SetStateText("Scanning...");
central.ScanForPeripherals(new string[] { SampleLightControl_Data.ServiceUUID });
}
}

public void DidDiscoverPeripheral(CBCentralManager central, CBPeripheral peripheral, int rssi)
{
_header.SetStateText("Connecting...");
_peripheral = peripheral;
peripheral.Delegate = this;
central.StopScan();
central.Connect(peripheral);
}

public void DidConnectPeripheral(CBCentralManager central, CBPeripheral peripheral)
{
_header.SetStateText("Connected");
peripheral.DiscoverServices(new string[] { SampleLightControl_Data.ServiceUUID });
}

public void DidFailToConnectPeripheral(CBCentralManager central, CBPeripheral peripheral, CBError error)
{
_header.SetStateText("Scanning...");
_lightControlCharacteristic = null;
central.ScanForPeripherals(new string[] { SampleLightControl_Data.ServiceUUID });
}

public void DidDisconnectPeripheral(CBCentralManager central, CBPeripheral peripheral, CBError error)
{
_header.SetStateText("Scanning...");
_lightControlCharacteristic = null;
central.ScanForPeripherals(new string[] { SampleLightControl_Data.ServiceUUID });
}

public void 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);
}
}

public void 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;
}
}
}

public void 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 OnDestory()
{
if (_centralManager != null)
{
_centralManager.Dispose();
_centralManager = null;
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading