public async UniTask <Cube> Connect(BLEPeripheralInterface peripheral)
            {
                try
                {
                    while (this.isConnecting)
                    {
                        await UniTask.Delay(100);
                    }

                    this.isConnecting = true;

                    bool success = await this.ConnectPeripheral(peripheral);

                    if (!success)
                    {
                        return(null);
                    }

                    var cube = new CubeUnity(peripheral as UnityPeripheral);
                    cube.Initialize();
                    this.isConnecting = false;
                    return(cube);
                }
                catch (System.Exception e)
                {
                    Debug.LogError(e);
                    this.isConnecting = false;
                    return(null);
                }
            }
Exemplo n.º 2
0
 void Update()
 {
     if (_timeout > 0f)
     {
         _timeout -= Time.deltaTime;
         if (_timeout <= 0f)
         {
             _timeout = 0f;
             if (state == State.Scan)
             {
                 // 端末のBLE機能変数を取得
                 BLEService.Instance.RequestDevice((_device) =>
                 {
                     device         = _device;
                     string[] uuids = { SERVICE_UUID };
                     // 検索対象デバイスをtoioのサービスIDに絞って検索
                     device.Scan(uuids, true, (_peripheral) =>
                     {
                         // 一台発見したら接続モードへ変更
                         peripheral = _peripheral;
                         Debug.LogFormat("peripheral.device_name: {0}, peripheral.device_address: {1}", peripheral.device_name, peripheral.device_address);
                         device.StopScan();
                         SetState(State.Connect, 1f);
                     });
                 }
                                                   );
             }
             if (state == State.Connect)
             {
                 // peripheral(デバイス)に接続して全てのcharacteristic(機能)を取得
                 peripheral.Connect((chara) =>
                 {
                     characteristicTable[chara.characteristicUUID] = chara;
                     // 全てのcharacteristicへの接続を確認
                     if (characteristicTable.Count == 8)
                     {
                         SetState(State.Subscribe, 1f);
                     }
                 });
             }
             if (state == State.Subscribe)
             {
                 // 座標や角度を定期受信出来るように購読開始
                 characteristicTable[CHARACTERISTIC_ID].StartNotifications(action: Recv_Id);
                 SetState(State.Control, 1f);
             }
             if (state == State.Control)
             {
                 // コアキューブ通信仕様に沿ってパケット送信
                 // https://toio.github.io/toio-spec/docs/2.0.0/ble_motor#時間指定付きモーター制御
                 byte[] buff = { 2, 1, 1, 100, 2, 1, 70, 100 };
                 // モーターcharacteristicに対してパケット送信
                 characteristicTable[CHARACTERISTIC_MOTOR].WriteValue(data: buff, withResponse: false);
                 // 送信処理を50ミリ秒間隔で実行
                 SetState(State.Control, 0.05f);
             }
         }
     }
 }
            public async UniTask <Cube> Connect(BLEPeripheralInterface peripheral)
            {
                try
                {
                    while (this.isConnecting)
                    {
                        await UniTask.Delay(100);
                    }

                    this.isConnecting = true;
                    var characteristicTable = await this.ConnectCharacteristics(peripheral);

                    var version = await this.GetProtocolVersion(characteristicTable[CubeReal.CHARACTERISTIC_CONFIG]);

                    CubeReal cube = null;
                    switch (version)
                    {
                    case "2.0.0":
                        cube = new CubeReal_ver2_0_0(peripheral);
                        break;

                    case "2.1.0":
                        cube = new CubeReal_ver2_1_0(peripheral);
                        break;

                    case "2.2.0":
                        cube = new CubeReal_ver2_2_0(peripheral);
                        break;

                    case "2.3.0":
                        cube = new CubeReal_ver2_3_0(peripheral);
                        break;

                    default:
                        // Basically, BLE protocol version has backward compatibility,
                        // so consider unknown version as the latest version.
                        //
                        // TODO:
                        // - patch(build) number can be ignored (should be?)
                        // - major number should be checked
                        cube = new CubeReal_ver2_3_0(peripheral);
                        break;
                    }
                    await cube.Initialize(characteristicTable);

                    this.isConnecting = false;
                    return(cube);
                }
                catch (System.Exception e)
                {
                    Debug.LogError(e);
                    this.isConnecting = false;
                    return(null);
                }
            }
Exemplo n.º 4
0
        public CubeReal(BLEPeripheralInterface peripheral)
        {
            this.peripheral          = peripheral;
            this.isPressed           = false; // 初期値:非押下
            this.isSloped            = false; // 初期値:水平
            this.isCollisionDetected = false; // 初期値:非衝突
            this.battery             = 100;

            // idが無効となったため、addrで代用
            this.id = addr;
        }
Exemplo n.º 5
0
        public CubeReal(BLEPeripheralInterface peripheral, Dictionary <string, BLECharacteristicInterface> characteristicTable)
        {
            this.peripheral          = peripheral;
            this.characteristicTable = characteristicTable;
            this.isPressed           = false; // 初期値:非押下
            this.isSloped            = false; // 初期値:水平
            this.isCollisionDetected = false; // 初期値:非衝突
            this.battery             = 100;

            // idが無効となったため、addrで代用
            this.id = addr;
        }
Exemplo n.º 6
0
            public async UniTask <Cube> Connect(BLEPeripheralInterface peripheral)
            {
                var uniperipheral = peripheral as UnityPeripheral;
                var cube          = new CubeUnity(uniperipheral.obj);

                while (!cube.isConnected)
                {
                    await UniTask.Delay(50);
                }
                cube.Init();
                return(cube as Cube);
            }
            protected virtual async UniTask <bool> ConnectPeripheral(BLEPeripheralInterface peripheral)
            {
                float startTime = Time.time;

                peripheral.Connect(null);

                while (true)
                {
                    if (peripheral.isConnected)
                    {
                        return(true);
                    }
                    if (startTime < Time.time - 10.0f)
                    {
                        return(false);
                    }
                    await UniTask.Delay(100);
                }
            }
Exemplo n.º 8
0
            public async UniTask ReConnect(Cube cube, BLEPeripheralInterface peripheral)
            {
                try
                {
                    while (this.isConnecting)
                    {
                        await UniTask.Delay(100);
                    }

                    this.isConnecting = true;
                    var characteristicTable = await this.ConnectCharacteristics(peripheral);

                    await(cube as CubeReal).StartNotifications();
                    this.isConnecting = false;
                }
                catch (System.Exception)
                {
                    this.isConnecting = false;
                }
            }
Exemplo n.º 9
0
 private void OnConnectionEvent(BLEPeripheralInterface peripheral)
 {
     if (peripheral.isConnected)
     {
         if (!this.connectedPeripheralTable.ContainsKey(peripheral.device_address))
         {
             this.connectedPeripheralTable.Add(peripheral.device_address, peripheral);
         }
     }
     else
     {
         if (this.connectedPeripheralTable.ContainsKey(peripheral.device_address))
         {
             this.connectedPeripheralTable.Remove(peripheral.device_address);
         }
         if (!this.isScanning && this.autoRunning)
         {
             this.NearScanAsync(this.satisfiedNumForAsync, this.coroutineObject, this.callback, this.autoRunning);
         }
     }
 }
Exemplo n.º 10
0
        // --- private methods ---
        protected async void OnPeripheralScanned(BLEPeripheralInterface peripheral)
        {
            if (null == this.connecter)
            {
                this.connecter = new CubeConnecter();
            }
            if (this.cubeTable.ContainsKey(peripheral.device_address))
            {
                var cube = this.cubeTable[peripheral.device_address];
                await this.connecter.ReConnect(cube, peripheral);

                this.connectedAction(cube, new CONNECTION_STATUS(CONNECTION_STATUS.RE_CONNECTED));
            }
            else
            {
                var cube = await this.connecter.Connect(peripheral);

                this.AddCube(cube);
                if (null != this.connectedAction)
                {
                    this.connectedAction(cube, new CONNECTION_STATUS(CONNECTION_STATUS.NEW_CONNECTED));
                }
            }
        }
Exemplo n.º 11
0
 public CubeReal_ver2_0_0(BLEPeripheralInterface peripheral) : base(peripheral)
 {
 }
Exemplo n.º 12
0
 public void ConnectionNotify(BLEPeripheralInterface peri)
 {
     this.callback.Notify(peri);
 }
Exemplo n.º 13
0
 public async UniTask ReConnect(Cube cube, BLEPeripheralInterface peripheral)
 {
     await this.impl.ReConnect(cube);
 }
Exemplo n.º 14
0
 public async UniTask <Cube> Connect(BLEPeripheralInterface peripheral)
 {
     return(await this.impl.Connect(peripheral));
 }
Exemplo n.º 15
0
            /// <summary>
            /// CoreCubeの操作に必要な全ての機能と接続.
            /// </summary>
            protected virtual async UniTask <Dictionary <string, BLECharacteristicInterface> > ConnectCharacteristics(BLEPeripheralInterface peripheral, float waitSeconds = 10.0f)
            {
                float startTime = Time.time;

                var characteristicTable = new Dictionary <string, BLECharacteristicInterface>();

                characteristicTable.Add(CubeReal.CHARACTERISTIC_CONFIG, null);
                characteristicTable.Add(CubeReal.CHARACTERISTIC_ID, null);
                characteristicTable.Add(CubeReal.CHARACTERISTIC_SENSOR, null);
                characteristicTable.Add(CubeReal.CHARACTERISTIC_BUTTON, null);
                characteristicTable.Add(CubeReal.CHARACTERISTIC_BATTERY, null);
                characteristicTable.Add(CubeReal.CHARACTERISTIC_MOTOR, null);
                characteristicTable.Add(CubeReal.CHARACTERISTIC_LIGHT, null);
                characteristicTable.Add(CubeReal.CHARACTERISTIC_SOUND, null);

                peripheral.Connect((chara) =>
                {
                    if (chara.serviceUUID == CubeReal.SERVICE_ID)
                    {
                        characteristicTable[chara.characteristicUUID] = chara;
                    }
                });

                bool isBreak = true;

                while (true)
                {
                    await UniTask.Delay(100);

                    isBreak = true;
                    foreach (var chara in characteristicTable.Values)
                    {
                        if (null == chara)
                        {
                            isBreak = false;
                            break;
                        }
                    }
                    if (isBreak)
                    {
                        break;
                    }

                    if (waitSeconds < (Time.time - startTime))
                    {
                        return(null);
                    }
                }

                return(characteristicTable);
            }
 public CubeReal_ver2_0_0(BLEPeripheralInterface peripheral, Dictionary <string, BLECharacteristicInterface> characteristicTable)
     : base(peripheral, characteristicTable)
 {
 }