private void AndroidMessageFoundDevice(string message) { Debug.Log(message); string[] tokens = message.Split(new char[] { ',' }); CommDevice foundDevice = new CommDevice(); if (tokens[0].Length == 0) { foundDevice.name = tokens[1]; } else { foundDevice.name = tokens[0]; } foundDevice.address = tokens[1]; for (int i = 0; i < foundDevices.Count; i++) { if (foundDevices[i].Equals(foundDevice)) { return; } } foundDevices.Add(foundDevice); OnFoundDevice.Invoke(foundDevice); }
public override void StartSearch() { foundDevices.Clear(); OnStartSearch.Invoke(); #if UNITY_EDITOR #if UNITY_EDITOR_WIN string[] ports = SerialPort.GetPortNames(); foreach (string port in ports) { CommDevice foundDevice = new CommDevice(); foundDevice.name = port; foundDevice.address = "//./" + port; foundDevices.Add(foundDevice); OnFoundDevice.Invoke(foundDevice); } #elif UNITY_EDITOR_OSX string prefix = "/dev/"; string[] ports = Directory.GetFiles(prefix, "*.*"); foreach (string port in ports) { if (port.StartsWith("/dev/cu.")) { CommDevice foundDevice = new CommDevice(); foundDevice.name = port.Substring(prefix.Length); foundDevice.address = port; foundDevices.Add(foundDevice); OnFoundDevice.Invoke(foundDevice); } } #endif #else #if UNITY_STANDALONE_WIN string[] ports = SerialPort.GetPortNames(); foreach (string port in ports) { CommDevice foundDevice = new CommDevice(); foundDevice.name = port; foundDevice.address = "//./" + port; foundDevices.Add(foundDevice); OnFoundDevice.Invoke(foundDevice); } #elif UNITY_STANDALONE_OSX string prefix = "/dev/"; string[] ports = Directory.GetFiles(prefix, "*.*"); foreach (string port in ports) { if (port.StartsWith("/dev/cu.")) { CommDevice foundDevice = new CommDevice(); foundDevice.name = port.Substring(prefix.Length); foundDevice.address = port; foundDevices.Add(foundDevice); OnFoundDevice.Invoke(foundDevice); } } #endif #endif OnStopSearch.Invoke(); }
public override void StartSearch() { foundDevices.Clear(); _searchTimeout = searchTimeout; OnStartSearch.Invoke(); #if UNITY_ANDROID if (_android != null) { string[] devInfos = _android.Call <string[]>("GetBondedDevices"); for (int i = 0; i < devInfos.Length; i++) { string[] tokens = devInfos[i].Split(new char[] { ',' }); CommDevice foundDevice = new CommDevice(); foundDevice.name = tokens[0]; foundDevice.address = tokens[1]; foundDevices.Add(foundDevice); OnFoundDevice.Invoke(foundDevice); } _android.Call("StartSearch"); } #endif }
public bool Equals(CommDevice device) { if (device == null) { return(false); } if (!name.Equals(device.name)) { return(false); } if (!address.Equals(device.address)) { return(false); } if (args.Count != device.args.Count) { return(false); } for (int i = 0; i < args.Count; i++) { if (!args[i].Equals(device.args[i])) { return(false); } } return(true); }
public CommDevice(CommDevice device) { name = device.name; address = device.address; for (int i = 0; i < device.args.Count; i++) { args.Add(device.args[i]); } }
private void WindowPortSearch() { string[] ports = SerialPort.GetPortNames(); foreach (string port in ports) { CommDevice foundDevice = new CommDevice(); foundDevice.name = port; foundDevice.address = "//./" + port; foundDevices.Add(foundDevice); OnFoundDevice.Invoke(foundDevice); } }
private void OsxPortSearch() { string prefix = "/dev/"; string[] ports = Directory.GetFiles(prefix, "*.*"); foreach (string port in ports) { if (port.StartsWith("/dev/cu.")) { CommDevice foundDevice = new CommDevice(); foundDevice.name = port.Substring(prefix.Length); foundDevice.address = port; foundDevices.Add(foundDevice); OnFoundDevice.Invoke(foundDevice); } } }
private void OnFoundDevice(CommDevice device) { ListItem item = GameObject.Instantiate(deviceItem); item.gameObject.SetActive(true); item.textList[0].text = device.name; if (item.textList.Length > 1) { item.textList[1].text = device.address; } item.data = device; deviceList.AddItem(item); if (deviceList.selectedItem == null) { if (commBluetooth.device.Equals(device)) { deviceList.selectedItem = item; } } }
private void OnFoundDevice(CommDevice device) { Dropdown.OptionData item = new Dropdown.OptionData(); item.text = device.name; portList.options.Add(item); }
private void BleCallback(string message) { if (message == null) { return; } string[] tokens = message.Split(new char[] { '~' }); if (tokens.Length == 0) { return; } if (tokens[0].Equals("Initialized")) { Debug.Log("BLE Initialized"); #if (UNITY_STANDALONE_OSX || UNITY_IOS) _bleInitialized = true; #endif } else if (tokens[0].Equals("Deinitialized")) { Debug.Log("BLE Deinitialized"); #if (UNITY_STANDALONE_OSX || UNITY_IOS) _bleInitialized = false; #endif } else if (tokens[0].Equals("NotSupported")) { #if (UNITY_STANDALONE_OSX || UNITY_IOS) Debug.Log("BLE not supported"); _isSupport = false; #endif } else if (tokens[0].Equals("PoweredOff")) { #if (UNITY_STANDALONE_OSX || UNITY_IOS) Debug.Log("BLE Power Off"); #endif } else if (tokens[0].Equals("PoweredOn")) { #if (UNITY_STANDALONE_OSX || UNITY_IOS) Debug.Log("BLE Power On"); #endif } else if (tokens[0].Equals("Unauthorized")) { #if (UNITY_STANDALONE_OSX || UNITY_IOS) Debug.Log("BLE Unauthorized"); #endif } else if (tokens[0].Equals("StateUnknown")) { #if (UNITY_STANDALONE_OSX || UNITY_IOS) Debug.Log("BLE Unauthorized"); #endif } else if (tokens[0].Equals("StartScan")) { Debug.Log("HM10 Start Scanning"); foundDevices.Clear(); _threadOnStartSearch = true; } else if (tokens[0].Equals("StopScan")) { Debug.Log("HM10 Stop Scanning"); _threadOnStopSearch = true; } else if (tokens[0].Equals("ConnectFailed")) { Debug.Log("BLE GATT Connect Failed"); _threadOnOpenFailed = true; } else if (tokens[0].Equals("DiscoveredDevice")) { CommDevice foundDevice = new CommDevice(); foundDevice.name = tokens[1]; foundDevice.address = tokens[2]; for (int i = 0; i < foundDevices.Count; i++) { if (foundDevices[i].Equals(foundDevice)) { return; } } foundDevices.Add(foundDevice); _threadOnFoundDevice = true; } else if (tokens[0].Equals("Disconnected")) { Debug.Log("BLE GATT Disconnected"); if (_isBleOpen) { _threadOnErrorClosed = true; } else if (_bleOpenTry) { _threadOnOpenFailed = true; } } else if (tokens[0].Equals("Connected")) { Debug.Log("BLE GATT Connected"); #if UNITY_ANDROID if (_android != null) { _android.Call("DiscoverService", _serviceUUID); } #elif (UNITY_STANDALONE_OSX || UNITY_IOS) if (_bleInitialized) { bleDiscoverService(device.address, _serviceUUID); } #endif } else if (tokens[0].Equals("DiscoveredService")) { Debug.Log("HM10 Discovered Service"); #if UNITY_ANDROID if (_android != null) { _android.Call("DiscoverCharacteristic", _serviceUUID, _charUUID); } #elif (UNITY_STANDALONE_OSX || UNITY_IOS) if (_bleInitialized) { bleDiscoverCharacteristic(device.address, _serviceUUID, _charUUID); } #endif } else if (tokens[0].Equals("ErrorDiscoveredService")) { Debug.Log("HM10 Discovered Service Error: " + tokens[1]); _threadOnOpenFailed = true; } else if (tokens[0].Equals("DiscoveredCharacteristic")) { #if UNITY_ANDROID Debug.Log("HM10 Discovered Characteristic"); if (_android != null) { _android.Call("SubscribeCharacteristic2", _serviceUUID, _charUUID); } #elif (UNITY_STANDALONE_OSX || UNITY_IOS) if (tokens.Length > 1 && !_isBleOpen) { string[] tokens2 = message.Split(new char[] { ':' }); bool foundCharUUID = false; for (int i = 0; i < tokens2.Length; i++) { if (_charUUID.Equals(tokens2[i]) == true) { Debug.Log("HM10 Discovered Characteristic"); if (_bleInitialized) { bleSubscribe(device.address, _serviceUUID, _charUUID); } foundCharUUID = true; break; } } if (!foundCharUUID) { Debug.Log(string.Format("Can not find HM-10 Characteristic <{0}>", _charUUID)); _threadOnOpenFailed = true; } } #endif } else if (tokens[0].Equals("ErrorDiscoverCharacteristic")) { Debug.Log("HM10 Discover Characteristic Error: " + tokens[1]); _threadOnOpenFailed = true; } else if (tokens[0].Equals("ErrorWrite")) { Debug.Log("HM10 Write Error: " + tokens[1]); _threadOnErrorClosed = true; } else if (tokens[0].Equals("ErrorSubscribeCharacteristic")) { Debug.Log("HM10 Subscribe Characteristic Error: " + tokens[1]); _threadOnOpenFailed = true; } else if (tokens[0].Equals("SubscribedCharacteristic")) { #if UNITY_ANDROID Debug.Log("HM10 Subscribed Characteristic"); _isBleOpen = true; _bleOpenTry = false; _threadOnOpen = true; #elif (UNITY_STANDALONE_OSX || UNITY_IOS) if (tokens.Length > 1) { if (_charUUID.Equals(tokens[1]) == true) { Debug.Log("HM10 Subscribed Characteristic"); _isBleOpen = true; _bleOpenTry = false; _threadOnOpen = true; } } #endif } else if (tokens[0].Equals("UnSubscribedCharacteristic")) { #if UNITY_ANDROID Debug.Log("HM10 UnSubscribed Characteristic"); #elif (UNITY_STANDALONE_OSX || UNITY_IOS) if (tokens.Length > 1) { if (_charUUID.Equals(tokens[1]) == true) { Debug.Log("HM10 UnSubscribed Characteristic"); } } #endif } else if (tokens[0].Equals("Write")) { if (string.Compare(_charUUID, tokens[1], true) == 0) { txWrite(); } } else if (tokens[0].Equals("ErrorRead")) { Debug.Log("HM10 Read Error: " + tokens[1]); _threadOnErrorClosed = true; } else if (tokens[0].Equals("Read")) { byte[] base64Bytes = Convert.FromBase64String(tokens[2]); if (base64Bytes.Length > 0) { if (string.Compare(_charUUID, tokens[1], true) == 0) { _rxBuffer.AddRange(base64Bytes); } } } else { if (tokens.Length == 1) { Debug.Log(tokens[0]); } else if (tokens.Length == 2) { Debug.Log(tokens[0] + ":" + tokens[1]); } } }
private void OnChangedDevice(CommDevice device) { commBLE.Close(); }
private void BleCallback(string message) { if (message == null) { return; } string[] tokens = message.Split(new char[] { '~' }); if (tokens.Length == 0) { return; } if (tokens[0].Equals("Initialized")) { Debug.Log("BLE Initialized"); #if (UNITY_STANDALONE_OSX || UNITY_IOS) _bleInitialized = true; #endif } else if (tokens[0].Equals("Deinitialized")) { Debug.Log("BLE Deinitialized"); #if (UNITY_STANDALONE_OSX || UNITY_IOS) _bleInitialized = false; #endif } else if (tokens[0].Equals("NotSupported")) { #if (UNITY_STANDALONE_OSX || UNITY_IOS) Debug.Log("BLE not supported"); _isSupport = false; #endif } else if (tokens[0].Equals("PoweredOff")) { #if (UNITY_STANDALONE_OSX || UNITY_IOS) Debug.Log("BLE Power Off"); #endif } else if (tokens[0].Equals("PoweredOn")) { #if (UNITY_STANDALONE_OSX || UNITY_IOS) Debug.Log("BLE Power On"); #endif } else if (tokens[0].Equals("Unauthorized")) { #if (UNITY_STANDALONE_OSX || UNITY_IOS) Debug.Log("BLE Unauthorized"); #endif } else if (tokens[0].Equals("StateUnknown")) { #if (UNITY_STANDALONE_OSX || UNITY_IOS) Debug.Log("BLE Unauthorized"); #endif } else if (tokens[0].Equals("StartScan")) { Debug.Log("BLE Start Scanning"); foundDevices.Clear(); _threadOnStartSearch = true; } else if (tokens[0].Equals("StopScan")) { Debug.Log("BLE Stop Scanning"); _threadOnStopSearch = true; } else if (tokens[0].Equals("ConnectFailed")) { Debug.Log("BLE GATT Connect Failed"); _threadOnOpenFailed = true; } else if (tokens[0].Equals("DiscoveredDevice")) { CommDevice foundDevice = new CommDevice(); foundDevice.name = tokens[1]; foundDevice.address = tokens[2]; for (int i = 0; i < foundDevices.Count; i++) { if (foundDevices[i].Equals(foundDevice)) { return; } } foundDevices.Add(foundDevice); _threadOnFoundDevice = true; } else if (tokens[0].Equals("Disconnected")) { Debug.Log("BLE GATT Disconnected"); if (_isBleOpen) { _threadOnErrorClosed = true; } } else if (tokens[0].Equals("Connected")) { Debug.Log("BLE GATT Connected"); #if UNITY_ANDROID if (_android != null) { _android.Call("DiscoverService", ArdunityBLE.serviceUUID); } #elif (UNITY_STANDALONE_OSX || UNITY_IOS) if (_bleInitialized) { bleDiscoverService(device.address, ArdunityBLE.serviceUUID); } #endif } else if (tokens[0].Equals("DiscoveredService")) { Debug.Log("BLE Discovered Service"); #if UNITY_ANDROID if (_android != null) { _android.Call("DiscoverCharacteristic", ArdunityBLE.serviceUUID, ArdunityBLE.txUUID); } #elif (UNITY_STANDALONE_OSX || UNITY_IOS) if (_bleInitialized) { bleDiscoverCharacteristic(device.address, ArdunityBLE.serviceUUID, ArdunityBLE.txUUID); } #endif } else if (tokens[0].Equals("ErrorDiscoveredService")) { Debug.Log("BLE Discovered Service Error: " + tokens[1]); _threadOnOpenFailed = true; } else if (tokens[0].Equals("DiscoveredCharacteristic")) { Debug.Log("BLE Discovered Characteristic"); if (!_foundTX) { _foundTX = true; #if UNITY_ANDROID if (_android != null) { _android.Call("DiscoverCharacteristic", ArdunityBLE.serviceUUID, ArdunityBLE.rxUUID); } #elif (UNITY_STANDALONE_OSX || UNITY_IOS) if (_bleInitialized) { bleDiscoverCharacteristic(device.address, ArdunityBLE.serviceUUID, ArdunityBLE.rxUUID); } #endif } else if (!_foundRX) { _foundRX = true; #if UNITY_ANDROID if (_android != null) { _android.Call("SubscribeCharacteristic", ArdunityBLE.serviceUUID, ArdunityBLE.rxUUID); _android.Call("DiscoverCharacteristic", ArdunityBLE.serviceUUID, ArdunityBLE.nameUUID); } #elif (UNITY_STANDALONE_OSX || UNITY_IOS) if (_bleInitialized) { bleSubscribe(device.address, ArdunityBLE.serviceUUID, ArdunityBLE.rxUUID); bleDiscoverCharacteristic(device.address, ArdunityBLE.serviceUUID, ArdunityBLE.nameUUID); } #endif } else if (!_foundName) { _foundName = true; _isBleOpen = true; _threadOnOpen = true; } } else if (tokens[0].Equals("ErrorDiscoverCharacteristic")) { Debug.Log("BLE Discovered Characteristic Error: " + tokens[1]); _threadOnOpenFailed = true; } else if (tokens[0].Equals("ErrorWrite")) { Debug.Log("BLE Write Error: " + tokens[1]); _threadOnErrorClosed = true; } else if (tokens[0].Equals("Write")) { if (string.Compare(ArdunityBLE.txUUID, tokens[1], true) == 0) { txWrite(); } else if (string.Compare(ArdunityBLE.nameUUID, tokens[1], true) == 0) { device.name = _changedName; _threadOnChangedDevice = true; } } else if (tokens[0].Equals("ErrorRead")) { Debug.Log("BLE Read Error: " + tokens[1]); _threadOnErrorClosed = true; } else if (tokens[0].Equals("Read")) { byte[] base64Bytes = Convert.FromBase64String(tokens[2]); if (base64Bytes.Length > 0) { if (string.Compare(ArdunityBLE.rxUUID, tokens[1], true) == 0) { _rxBuffer.AddRange(base64Bytes); } } } }