void Update() { if (_state == CentralState.Connecting) { if (Time.time > connectingStartTime + connectingTimeout) { _state = CentralState.Idle; } } }
public void StopScanForDice() { if (_state == CentralState.Scanning) { BluetoothLEHardwareInterface.StopScan(); if (virtualBluetooth != null) { virtualBluetooth.StopScan(); } _state = CentralState.Idle; } else { Debug.LogError("Central is not currently scanning for devices, current state is " + _state); } }
public void BeginScanForDice(System.Action <Die> foundDieCallback) { if (_state == CentralState.Idle) { // Destroy any die that is not currently connected! foreach (var die in GetComponentsInChildren <Die>()) { if (!die.connected) { DestroyDie(die); } } System.Action <string, string> dieDiscovered = (address, name) => { if (!diceList.Any(dc => dc.address == address)) { var die = CreateDie(name, address); if (foundDieCallback != null) { foundDieCallback(die); } if (onDieDiscovered != null) { onDieDiscovered(die); } } }; _state = CentralState.Scanning; var services = new string[] { serviceGUID }; BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(services, dieDiscovered, null, false, false); // Also notify virtual dice that we're trying to connect if (virtualBluetooth != null) { virtualBluetooth.ScanForPeripheralsWithServices(services, dieDiscovered, null, false, false); } } else { Debug.LogError("Central is not ready to scan, current state is " + _state); } }
void Start() { _state = CentralState.Initializing; #if !UNITY_EDITOR_OSX BluetoothLEHardwareInterface.Initialize(true, false, () => { _state = CentralState.Idle; }, (err) => { _state = CentralState.Error; Debug.LogError("Error initializing Bluetooth Central: " + err); }); #else _state = CentralState.Idle; #endif }
public void ConnectToDie(Die die, System.Action <Die> dieConnectedCallback, System.Action <Die> dieDisconnectedCallback) { if (!die.connected && _state == CentralState.Idle) { _state = CentralState.Connecting; connectingStartTime = Time.time; bool readCharacDiscovered = false; bool writeCharacDiscovered = false; System.Action gotReadOrWriteCharacteristic = () => { // Do we have both read and write access? If so we're good to go! if (readCharacDiscovered && writeCharacDiscovered) { // If somehow we've timed out, skip this. if (_state == CentralState.Connecting) { // We're ready to go die.Connect(this); _state = CentralState.Idle; if (dieConnectedCallback != null) { dieConnectedCallback(die); } if (onDieConnected != null) { onDieConnected(die); } foreach (var client in clients) { client.OnNewDie(die); } } } }; System.Action <string, string, string> onCharacteristicDiscovered = (ad, serv, charac) => { // Check for the service guid to match that for our dice (it's the Simblee one) if (ad == die.address && serv.ToLower() == serviceGUID.ToLower()) { // Check the discovered characteristic if (charac.ToLower() == subscribeCharacteristic.ToLower()) { // It's the read characteristic, subscribe to it! System.Action <string, byte[]> onDataReceived = (dev, data) => { die.DataReceived(data); }; if (virtualBluetooth == null || !virtualBluetooth.IsVirtualDie(die.address)) { BluetoothLEHardwareInterface.SubscribeCharacteristic(die.address, serviceGUID, subscribeCharacteristic, null, onDataReceived); } else { virtualBluetooth.SubscribeCharacteristic(die.address, serviceGUID, subscribeCharacteristic, null, onDataReceived); } readCharacDiscovered = true; gotReadOrWriteCharacteristic(); } else if (charac.ToLower() == writeCharacteristic.ToLower()) { // It's the write characteristic, remember that writeCharacDiscovered = true; gotReadOrWriteCharacteristic(); } // Else we don't care about this characteristic } }; System.Action <string> dieDisconnected = (ad) => { if (ad == die.address) { if (dieDisconnectedCallback != null) { dieDisconnectedCallback(die); } if (onDieDisconnected != null) { onDieDisconnected(die); } die.Disconnect(); } }; if (virtualBluetooth == null || !virtualBluetooth.IsVirtualDie(die.address)) { BluetoothLEHardwareInterface.ConnectToPeripheral(die.address, null, null, onCharacteristicDiscovered, dieDisconnected); } else { virtualBluetooth.ConnectToPeripheral(die.address, null, null, onCharacteristicDiscovered, dieDisconnected); } } else { Debug.LogError("Central is not ready to connect, current state is " + _state); } }