Esempio n. 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);
             }
         }
     }
 }
Esempio n. 2
0
 private void StartScanning(Action foundCallback)
 {
     this.isScanning = true;
     if (null == this.device)
     {
         BLEService.Instance.RequestDevice((device) =>
         {
             this.device = device;
             foundCallback?.Invoke();
         }
                                           );
     }
     else
     {
         foundCallback?.Invoke();
     }
 }