Example #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);
             }
         }
     }
 }
Example #2
0
            public async UniTask <BLEPeripheralInterface> NearestScan()
            {
                Dictionary <string, BLEPeripheralInterface> peripheralTable = new Dictionary <string, BLEPeripheralInterface>();
                List <BLEPeripheralInterface> peripheralList = new List <BLEPeripheralInterface>();

                bool   errorflg      = false;
                Action foundCallback = (() =>
                {
                    string[] uuids = { CubeReal.SERVICE_ID };
                    this.device.Scan(uuids, true, (peripheral) =>
                    {
                        if (null == peripheral)
                        {
                            errorflg = true;
                            return;
                        }
                        if (!this.connectedPeripheralTable.ContainsKey(peripheral.device_address) && !peripheralTable.ContainsKey(peripheral.device_address))
                        {
                            if (this.peripheralDatabase.ContainsKey(peripheral.device_address))
                            {
                                peripheral = this.peripheralDatabase[peripheral.device_address];
                            }
                            else
                            {
                                this.peripheralDatabase.Add(peripheral.device_address, peripheral);
                                peripheral.AddConnectionListener("CubeScanner.RealImpl", this.OnConnectionEvent);
                            }
                            peripheralList.Add(peripheral);
                            peripheralTable.Add(peripheral.device_address, peripheral);
                        }
                    });
                });

                this.StartScanning(foundCallback);

                await UniTask.Delay(1000);

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

                    if (errorflg)
                    {
                        this.isScanning = false;
                        if (null != device)
                        {
                            device.StopScan();
                        }
                        return(null);
                    }
                    if (0 < peripheralList.Count)
                    {
                        break;
                    }
                }
                peripheralList.Sort((a, b) => 0 < (b.rssi - a.rssi) ? 1 : -1);
                this.isScanning = false;
                if (null != device)
                {
                    device.StopScan();
                }

                return(peripheralList[0]);
            }