IEnumerator SearchDeviceFlow() { //Bluetoothが有効か確認 bool isBluetoothActive = false; yield return(StartCoroutine(CheckBluetoothIsActive((bool isActive) => isBluetoothActive = isActive))); if (!isBluetoothActive) { //Bluetooth無効時 yield break; } //もし既にデバイスと接続済みであれば、検索結果が表示できるように切断を行ってから行う if (UserDataManager.State.isConnectingDevice()) { BluetoothManager.Instance.Disconnect(); } //初期化として検索停止しておく BluetoothManager.Instance.StopScanning(); //インジケーターをリストに追加する var indicatorObj = Instantiate(IndicatorListElementPrehab); Adapter.SetElementToList(indicatorObj); /// <summary> /// デバイスを検索する /// /// TODO: 複数デバイス検索は、仕様ではなく、試験的に実施しているため、お客様からの指示があり次第処理を修正する /// NOTE: whileループをやめれば、1台だけの検索になります /// </summary> List <string> listDeviceAdress = new List <string>(); //リストに追加したデバイスのアドレスリスト //For iOS Dictionary <string, string[]> deviceList = new Dictionary <string, string[]>(); //Dict (deviceAddress -> (deviceName, identifierUuid)) bool? isContinue = null; string receiveData = ""; while (true) { isContinue = null; // BLEデバイス検索結果判定に使用する BluetoothManager.Instance.ScanBleDevice( (string data) => { isContinue = false; // デバイスが存在しない receiveData = data; }, (string data) => { //デバイス発見時 //発見したデバイス情報読み出し var json = Json.Deserialize(data) as Dictionary <string, object>; string deviceName = (string)json["KEY1"]; string deviceAdress = (string)json["KEY2"]; //iOSの場合、deviceAdressはiOS_UUID(未接続時)、もしくは、アドレス(接続後) string identifierUuid = ""; //iOSのみで使用するデバイス識別番号 #if UNITY_IOS //どのデバイスと接続するか決定するデバイス識別番号を取得(iOSのみでしか取得できない) identifierUuid = (string)json["KEY3"]; #endif //既にリストに追加されてるデバイスは弾く bool isExistList = listDeviceAdress.Where(adress => adress == deviceAdress).Count() > 0; //インジケーターがなければ、表示がおかしくなるため追加を行わない bool isExistIndicator = indicatorObj != null; if (!isExistList && indicatorObj) { //リストに追加する listDeviceAdress.Add(deviceAdress); // TODO: 試験的に、デバイス名とともにデバイスアドレスを表示する。仕様ではないため、お客様からの指示で下の一行を削除する // iOSはデバイスアドレスが取得できない場合(未接続時)、表示しない if (deviceAdress != "" && !deviceAdress.Contains("iOS_")) { deviceName += " [" + deviceAdress + "]"; } #if UNITY_IOS //iOSなら、以前に接続したデバイス(アドレスが取得できたデバイス)を優先に表示 deviceList[deviceAdress] = new String[] { deviceName, identifierUuid }; Adapter.HideAllElement(); var orderDeviceList = Adapter.GetDeviceListByOrderForIOS(deviceList); foreach (KeyValuePair <string, string[]> entry in orderDeviceList) { var sleeimDevice = CreateListElement( entry.Value[0], () => { //接続ボタンを押した際のコールバック //スキャンを停止する BluetoothManager.Instance.StopScanning(); //インジケーターを削除 if (indicatorObj != null) { DestroyImmediate(indicatorObj); } //ペアリング処理開始 StartCoroutine(Parering(entry.Value[0], entry.Key.Contains("iOS_") ? "" : entry.Key, entry.Value[1])); isContinue = false; // 検索処理終了フラグ }); Adapter.SetElementToList(sleeimDevice); } #else var sleeimDevice = CreateListElement( deviceName, () => { //接続ボタンを押した際のコールバック //スキャンを停止する BluetoothManager.Instance.StopScanning(); //インジケーターを削除 if (indicatorObj != null) { DestroyImmediate(indicatorObj); } //ペアリング処理開始 StartCoroutine(Parering(deviceName, deviceAdress.Contains("iOS_") ? "" : deviceAdress, identifierUuid)); isContinue = false; // 検索処理終了フラグ }); Adapter.SetElementToList(sleeimDevice); #endif } if (isContinue == null) { isContinue = true; } }); yield return(new WaitUntil(() => isContinue != null)); // 検索失敗or接続完了で終了 if (isContinue == false) { break; } } //エラー時 //インジケーター削除 DestroyImmediate(indicatorObj); BluetoothManager.Instance.StopScanning(); //スキャン停止 //エラー情報読み出し var j = Json.Deserialize(receiveData) as Dictionary <string, object>; int error1 = Convert.ToInt32(j["KEY1"]); //不要?説明なし int error2 = Convert.ToInt32(j["KEY2"]); //タイムアウトエラー(スキャンエラー含む) if (error2 == -4) { //接続切れのエラーであればリトライ yield return(StartCoroutine(SearchDeviceFlow())); } else { if (listDeviceAdress.Count() == 0) { //機器が見つからなかった旨のダイアログを表示する yield return(StartCoroutine(TellNotFoundDevice())); } } }