Пример #1
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);
             }
         }
     }
 }
            /// <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);
            }
            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);
                }
            }