Beispiel #1
0
    void OnDeviceDisconnected(string address)
    {
        // Check that this isn't an error-triggered disconnect, if it is, skip sending messages to the die
        if (_dice.TryGetValue(address, out Die die))
        {
            System.Action <Die> finishDisconnect = (d) =>
            {
                // Notify the die!
                d.die.OnDisconnected();
                onDieDisconnected?.Invoke(d.die);

                _dice.Remove(address);
            };

            switch (die.state)
            {
            case Die.State.Disconnecting:
                // This is perfectly okay
                finishDisconnect(die);
                break;

            case Die.State.Advertising:
                Debug.LogError("Disconnecting " + die.die.name + " is in incorrect state " + die.state);
                break;

            case Die.State.Connecting:
            case Die.State.Connected:
                Debug.LogWarning("Die " + die.die.name + " disconnected before subscribing");
                finishDisconnect(die);
                break;

            case Die.State.Subscribing:
                Debug.LogWarning("Die " + die.die.name + " disconnected while subscribing");

                // Clear error handler etc...
                onBluetoothError -= OnCharacteristicSubscriptionError;
                finishDisconnect(die);

                // Only kick off the next subscription IF this was the 'current' subscription attempt
                // Otherwise there will still be a subscription success/fail event and we'll trigger
                // the next one then.
                StartNextSubscribeToCharacteristic();
                break;

            case Die.State.Ready:
            default:
                finishDisconnect(die);
                break;
            }
        }
        else
        {
            Debug.LogError("Unknown die " + address + " disconnected!");
        }
    }
Beispiel #2
0
    void CheckSubscriptionState(Die die)
    {
        if (Time.time - die.startTime > SubscribeCharacteristicsTimeout)
        {
            Debug.LogError("Timeout trying to subscribe to die " + die.name);
            onBluetoothError -= OnCharacteristicSubscriptionError;

            // Temporarily add the die to the connected list to avoid an error message during the disconnect
            // And force a disconnect
            die.state = Die.State.Disconnecting;
            BluetoothLEHardwareInterface.DisconnectPeripheral(die.address, null);

            StartNextSubscribeToCharacteristic();
        }
    }
Beispiel #3
0
    void OnCharacteristicSubscriptionError(string error)
    {
        Die sub = _dice.Values.FirstOrDefault(d => d.state == Die.State.Subscribing);

        if (sub != null)
        {
            Debug.LogError("Die " + sub.name + " couldn't subscribe to read characteristic!");
            onBluetoothError -= OnCharacteristicSubscriptionError;

            // Temporarily add the die to the connected list to avoid an error message during the disconnect
            // And force a disconnect
            sub.state = Die.State.Disconnecting;
            BluetoothLEHardwareInterface.DisconnectPeripheral(sub.address, null);

            StartNextSubscribeToCharacteristic();
        }
        else
        {
            Debug.LogError("Subscription error but no subscribing die");
        }
    }
Beispiel #4
0
    void OnCharacteristicSubscriptionChanged(string characteristic)
    {
        Die sub = _dice.Values.FirstOrDefault(d => d.state == Die.State.Subscribing);

        if (sub != null)
        {
            // Clean up error handler
            onBluetoothError -= OnCharacteristicSubscriptionError;

            sub.state = Die.State.Ready;
            sub.die.OnConnected();
            onDieConnected?.Invoke(sub.die);

            StartNextSubscribeToCharacteristic();
        }
        else
        {
            sub = _dice.Values.FirstOrDefault(d => d.state == Die.State.Disconnecting);
            if (sub == null)
            {
                Debug.LogError("Subscription success but no subscribing die");
            }
        }
    }
Beispiel #5
0
    void StartNextSubscribeToCharacteristic()
    {
        Die nextToSub = _dice.Values.FirstOrDefault(d => d.state == Die.State.Connected);

        if (nextToSub != null)
        {
            nextToSub.state = Die.State.Subscribing;

            // Hook error handler
            onBluetoothError += OnCharacteristicSubscriptionError;

            // Add timeout...
            nextToSub.startTime = Time.time;

            // And subscribe!
            BluetoothLEHardwareInterface.SubscribeCharacteristic(
                nextToSub.address,
                serviceGUID,
                subscribeCharacteristic,
                OnCharacteristicSubscriptionChanged,
                (charac, data) => OnCharacteristicData(nextToSub.address, data));
        }
        // Else no more subscription pending
    }