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/02_ButtonInformation.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,126 @@
using CoreBluetooth;
using UnityEngine;
using UnityEngine.UI;

namespace CoreBluetoothSample
{
public class SampleButtonInformation_Central : MonoBehaviour, ICBCentralManagerDelegate, ICBPeripheralDelegate
{
[SerializeField] SampleButtonInformation_Log _log;
[SerializeField] SampleButtonInformation_Cube _cube;
[SerializeField] Text _stateLabel;

CBCentralManager _centralManager;
CBPeripheral _peripheral;

void Start()
{
var initOptions = new CBCentralManagerInitOptions() { ShowPowerAlert = true };
_centralManager = new CBCentralManager(this, initOptions);
}

void ICBCentralManagerDelegate.DidUpdateState(CBCentralManager central)
{
if (central.State == CBManagerState.PoweredOn)
{
_stateLabel.text = "Scanning...";
central.ScanForPeripherals(new string[] { SampleButtonInformation_Data.ServiceUUID });
}
}

void ICBCentralManagerDelegate.DidDiscoverPeripheral(CBCentralManager central, CBPeripheral peripheral, int rssi)
{
_stateLabel.text = "Connecting...";
_peripheral = peripheral;
peripheral.Delegate = this;
central.StopScan();
central.Connect(peripheral);
}

void ICBCentralManagerDelegate.DidConnectPeripheral(CBCentralManager central, CBPeripheral peripheral)
{
_stateLabel.text = "Connected";
peripheral.DiscoverServices();
}

void ICBCentralManagerDelegate.DidFailToConnectPeripheral(CBCentralManager central, CBPeripheral peripheral, CBError error)
{
_stateLabel.text = "Scanning...";
central.ScanForPeripherals(new string[] { SampleButtonInformation_Data.ServiceUUID });
}

void ICBCentralManagerDelegate.DidDisconnectPeripheral(CBCentralManager central, CBPeripheral peripheral, CBError error)
{
_stateLabel.text = "Scanning...";
central.ScanForPeripherals(new string[] { SampleButtonInformation_Data.ServiceUUID });
}

void ICBPeripheralDelegate.DidDiscoverServices(CBPeripheral peripheral, CBError error)
{
if (error != null)
{
Debug.LogError($"[DidDiscoverServices] error: {error}");
return;
}
foreach (var service in peripheral.Services)
{
if (service.UUID.Equals(SampleButtonInformation_Data.ServiceUUID))
{
peripheral.DiscoverCharacteristics(new string[] { SampleButtonInformation_Data.ButtonInformationCharacteristicUUID }, 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.Equals(SampleButtonInformation_Data.ButtonInformationCharacteristicUUID))
{
if (characteristic.Properties.HasFlag(CBCharacteristicProperties.Notify))
{
peripheral.SetNotifyValue(true, characteristic);
}
}
}
}

void ICBPeripheralDelegate.DidUpdateValueForCharacteristic(CBPeripheral peripheral, CBCharacteristic characteristic, CBError error)
{
if (error != null)
{
Debug.LogError($"[DidUpdateValue] error: {error}");
return;
}

if (SampleButtonInformation_Data.ParseButtonInformation(characteristic.Value, out int buttonId, out bool isPressed))
{
_log.AppendLog(buttonId, isPressed);
_cube.Action(buttonId, isPressed);
}
}

void ICBPeripheralDelegate.DidUpdateNotificationStateForCharacteristic(CBPeripheral peripheral, CBCharacteristic characteristic, CBError error)
{
if (error != null)
{
Debug.LogError($"[DidUpdateNotificationState] error: {error}");
return;
}
}

void OnDestroy()
{
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