Esempio n. 1
0
    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);
        }
    }