public CubeUnity(UnityPeripheral peripheral) { this.peripheral = peripheral; this.gameObject = peripheral.obj; id = gameObject.GetInstanceID().ToString(); simulator = gameObject.GetComponent <CubeSimulator>(); }
public async UniTask <BLEPeripheralInterface> NearestScan() { List <BLEPeripheralInterface> peripheralList = new List <BLEPeripheralInterface>(); List <GameObject> foundObjs = new List <GameObject>(); Action <GameObject> foundCallback = obj => { var peripheral = new UnityPeripheral(obj) as BLEPeripheralInterface; if (!this.connectedPeripheralTable.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.SimImpl", this.OnConnectionEvent); } peripheralList.Add(peripheral); } }; isScanning = true; // Scanning Loop while (true) { // Search for new cube object var objs = Array.ConvertAll <CubeSimulator, GameObject>(GameObject.FindObjectsOfType <CubeSimulator>(), sim => sim.gameObject); foreach (var obj in objs) { if (!obj.GetComponent <CubeSimulator>().isRunning) { continue; } if (!foundObjs.Contains(obj)) { foundObjs.Add(obj); foundCallback.Invoke(obj); } } if (0 < peripheralList.Count) { break; } // Searching Period await UniTask.Delay(200); } peripheralList.Sort((a, b) => b.rssi > a.rssi ? 1 : -1); isScanning = false; return(peripheralList[0]); }
// --- private methods --- private IEnumerator ScanCoroutine(int satisfiedNum, Action <BLEPeripheralInterface> callback) { List <GameObject> foundObjs = new List <GameObject>(); Action <GameObject> foundCallback = obj => { var peripheral = new UnityPeripheral(obj) as BLEPeripheralInterface; if (this.isScanning && foundObjs.Count < satisfiedNum && !this.connectedPeripheralTable.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.SimImpl", this.OnConnectionEvent); } callback?.Invoke(peripheral); } }; isScanning = true; // Scanning Loop while (true) { // Search for new cube object var objs = Array.ConvertAll <CubeSimulator, GameObject>(GameObject.FindObjectsOfType <CubeSimulator>(), sim => sim.gameObject); foreach (var obj in objs) { if (!obj.GetComponent <CubeSimulator>().isRunning) { continue; } if (!foundObjs.Contains(obj)) { foundObjs.Add(obj); foundCallback.Invoke(obj); } } // 必要数に達したらスキャン終了 if (satisfiedNum <= foundObjs.Count) { break; } // Searching Period yield return(new WaitForSeconds(0.2f)); } isScanning = false; }
public async UniTask <BLEPeripheralInterface[]> NearScan(int satisfiedNum, float waitSeconds) { var start_time = Time.time; List <BLEPeripheralInterface> peripheralList = new List <BLEPeripheralInterface>(); List <GameObject> foundObjs = new List <GameObject>(); Action <GameObject> foundCallback = obj => { var peripheral = new UnityPeripheral(obj) as BLEPeripheralInterface; if (this.isScanning && peripheralList.Count < satisfiedNum && !this.connectedPeripheralTable.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.SimImpl", this.OnConnectionEvent); } peripheralList.Add(peripheral); } }; isScanning = true; // Scanning Loop while (true) { // Search for new cube object var objs = Array.ConvertAll <CubeSimulator, GameObject>(GameObject.FindObjectsOfType <CubeSimulator>(), sim => sim.gameObject); foreach (var obj in objs) { if (!obj.GetComponent <CubeSimulator>().isRunning) { continue; } if (!foundObjs.Contains(obj)) { foundObjs.Add(obj); foundCallback.Invoke(obj); } } // 必要数に達したらスキャン終了 if (satisfiedNum <= peripheralList.Count) { break; } // Searching Period await UniTask.Delay(200); // 待機時間を超えた場合は一旦関数を終了する var elapsed = Time.time - start_time; if (waitSeconds <= elapsed) { break; } } peripheralList.Sort((a, b) => b.rssi > a.rssi ? 1 : -1); var nearPeripherals = peripheralList.GetRange(0, Mathf.Min(satisfiedNum, peripheralList.Count)).ToArray(); this.isScanning = false; return(nearPeripherals); }