Example #1
0
 public CommDevice(CommDevice device)
 {
     name = device.name;
     type = device.type;
     address = device.address;
     for (int i = 0; i < device.args.Count; i++)
         args.Add(device.args[i]);
 }
Example #2
0
    public bool Equals(CommDevice device)
    {
        if (!name.Equals(device.name))
            return false;

        if (type != device.type)
            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;
    }
Example #3
0
    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 && hm10.device != null)
        {
            if (hm10.device.Equals(device))
            {
                deviceList.selectedItem = item;
                ok.interactable         = true;
            }
        }
    }
    public override void OnInspectorGUI()
    {
        this.serializedObject.Update();

        GUI.enabled = false;
        EditorGUILayout.PropertyField(script, true, new GUILayoutOption[0]);
        GUI.enabled = true;

#if UNITY_STANDALONE
#if UNITY_5_6_OR_NEWER
#if NET_2_0
        CommSerial socket = (CommSerial)target;
        GUI.enabled = !socket.IsOpen;

#if UNITY_EDITOR_WIN
        EditorGUILayout.LabelField(string.Format("Port Name: {0}", socket.device.name));
#elif UNITY_EDITOR_OSX
        EditorGUILayout.LabelField(string.Format("Port Name: {0}", socket.device.address));
#endif
        EditorGUILayout.BeginHorizontal();
        int      index = -1;
        string[] list  = new string[socket.foundDevices.Count];
        for (int i = 0; i < list.Length; i++)
        {
            list[i] = socket.foundDevices[i].name;
            if (deviceName.stringValue.Equals(socket.foundDevices[i].name))
            {
                index = i;
            }
        }
        int newIndex = EditorGUILayout.Popup(" ", index, list);
        if (newIndex != index)
        {
            CommDevice newDevice = socket.foundDevices[newIndex];
            deviceName.stringValue    = newDevice.name;
            deviceAddress.stringValue = newDevice.address;
            deviceArgs.ClearArray();
            deviceArgs.arraySize = newDevice.args.Count;
            for (int i = 0; i < newDevice.args.Count; i++)
            {
                SerializedProperty arg = deviceArgs.GetArrayElementAtIndex(i);
                arg.stringValue = newDevice.args[i];
            }
        }

        if (GUILayout.Button("Search", GUILayout.Width(60f)) == true)
        {
            socket.StartSearch();
        }
        EditorGUILayout.EndHorizontal();

        GUI.enabled = true;
#else
        EditorGUILayout.HelpBox("You must set as '.Net 2.0' for API Compatibility Level in PlayerSetting.", MessageType.Error);
#endif
#else
        CommSerial socket = (CommSerial)target;
        GUI.enabled = !socket.IsOpen;

#if UNITY_EDITOR_WIN
        EditorGUILayout.LabelField(string.Format("Port Name: {0}", socket.device.name));
#elif UNITY_EDITOR_OSX
        EditorGUILayout.LabelField(string.Format("Port Name: {0}", socket.device.address));
#endif
        EditorGUILayout.BeginHorizontal();
        int      index = -1;
        string[] list  = new string[socket.foundDevices.Count];
        for (int i = 0; i < list.Length; i++)
        {
            list[i] = socket.foundDevices[i].name;
            if (deviceName.stringValue.Equals(socket.foundDevices[i].name))
            {
                index = i;
            }
        }
        int newIndex = EditorGUILayout.Popup(" ", index, list);
        if (newIndex != index)
        {
            CommDevice newDevice = socket.foundDevices[newIndex];
            deviceName.stringValue    = newDevice.name;
            deviceAddress.stringValue = newDevice.address;
            deviceArgs.ClearArray();
            deviceArgs.arraySize = newDevice.args.Count;
            for (int i = 0; i < newDevice.args.Count; i++)
            {
                SerializedProperty arg = deviceArgs.GetArrayElementAtIndex(i);
                arg.stringValue = newDevice.args[i];
            }
        }

        if (GUILayout.Button("Search", GUILayout.Width(60f)) == true)
        {
            socket.StartSearch();
        }
        EditorGUILayout.EndHorizontal();

        GUI.enabled = true;
#endif
#else
        EditorGUILayout.HelpBox("This component only can work on standalone platform(windows, osx, linux..)", MessageType.Error);
#endif

        EditorGUILayout.PropertyField(baudrate, new GUIContent("Baudrate"));
        EditorGUILayout.PropertyField(dtrReset, new GUIContent("DTR Reset"));

        foldout = EditorGUILayout.Foldout(foldout, "Events");
        if (foldout)
        {
            EditorGUILayout.PropertyField(OnOpen, new GUIContent("OnOpen"));
            EditorGUILayout.PropertyField(OnClose, new GUIContent("OnClose"));
            EditorGUILayout.PropertyField(OnOpenFailed, new GUIContent("OnOpenFailed"));
            EditorGUILayout.PropertyField(OnErrorClosed, new GUIContent("OnErrorClosed"));
            EditorGUILayout.PropertyField(OnStartSearch, new GUIContent("OnStartSearch"));
            EditorGUILayout.PropertyField(OnStopSearch, new GUIContent("OnStopSearch"));
        }

        this.serializedObject.ApplyModifiedProperties();
    }
Example #5
0
 private void OnCodeStarSerialFoundDevice(CommDevice device)
 {
     Dropdown.OptionData item = new Dropdown.OptionData();
     item.text = device.name;
     codeStarPort.options.Add(item);
 }
Example #6
0
 private void OnSerialFoundDevice(CommDevice device)
 {
     Dropdown.OptionData item = new Dropdown.OptionData();
     item.text = device.name;
     portList.options.Add(item);
 }
Example #7
0
	private void iOSBluetoothLEMessage(string message)
	{
		if (message != null)
		{
	//		Debug.Log(message);
			
			string[] parts = message.Split(new char[] { '~' });
			
			const string deviceBLESupportedString = "BLESupported";
			const string deviceBLENotSupportedString = "BLENotSupported";
			const string deviceInitializedString = "Initialized";
			const string deviceDeInitializedString = "DeInitialized";
			const string deviceErrorString = "Error";
			const string deviceServiceAdded = "ServiceAdded";
			const string deviceStartedAdvertising = "StartedAdvertising";
			const string deviceStoppedAdvertising = "StoppedAdvertising";
			const string deviceDiscoveredPeripheral = "DiscoveredPeripheral";
			const string deviceRetrievedConnectedPeripheral = "RetrievedConnectedPeripheral";
			const string deviceConnectedPeripheral = "ConnectedPeripheral";
			const string deviceDisconnectedPeripheral = "DisconnectedPeripheral";
			const string deviceDiscoveredService = "DiscoveredService";
			const string deviceDiscoveredCharacteristic = "DiscoveredCharacteristic";
			const string deviceDidWriteCharacteristic = "DidWriteCharacteristic";
            const string deviceDidUpdateNotificationStateForCharacteristic = "DidUpdateNotificationStateForCharacteristic";
            const string deviceDidUpdateValueForCharacteristic = "DidUpdateValueForCharacteristic";

            if (message.Length >= deviceBLESupportedString.Length && message.Substring(0, deviceBLESupportedString.Length) == deviceBLESupportedString)
            {
                _isSupportBLE = true;
            }
            else if (message.Length >= deviceBLENotSupportedString.Length && message.Substring(0, deviceBLENotSupportedString.Length) == deviceBLENotSupportedString)
            {
                _isSupportBLE = false;
            }
            else if (message.Length >= deviceInitializedString.Length && message.Substring(0, deviceInitializedString.Length) == deviceInitializedString)
            {
                _bleInitialized = true;
            }
            else if (message.Length >= deviceDeInitializedString.Length && message.Substring(0, deviceDeInitializedString.Length) == deviceDeInitializedString)
            {
                _bleInitialized = false;
            }
            else if (message.Length >= deviceErrorString.Length && message.Substring(0, deviceErrorString.Length) == deviceErrorString)
            {
                Debug.Log("BLE Error: " + parts[1]);
            }
            else if (message.Length >= deviceServiceAdded.Length && message.Substring(0, deviceServiceAdded.Length) == deviceServiceAdded)
            {
            }
            else if (message.Length >= deviceStartedAdvertising.Length && message.Substring(0, deviceStartedAdvertising.Length) == deviceStartedAdvertising)
            {
            }
            else if (message.Length >= deviceStoppedAdvertising.Length && message.Substring(0, deviceStoppedAdvertising.Length) == deviceStoppedAdvertising)
            {
            }
            else if (message.Length >= deviceDiscoveredPeripheral.Length && message.Substring(0, deviceDiscoveredPeripheral.Length) == deviceDiscoveredPeripheral)
            {
                if (parts.Length >= 4)
                {
                    CommDevice foundDevice = new CommDevice();
                    foundDevice.type = CommDevice.Type.BLE;
                    foundDevice.name = parts[2];
                    foundDevice.address = parts[3];
                    foundDevice.args.Add(parts[1]);

                    for (int i = 0; i < foundDevices.Count; i++)
                    {
                        if (foundDevices[i].Equals(foundDevice))
                            return;
                    }

                    foundDevices.Add(foundDevice);
                    _threadOnFoundDevice = true;
                }
            }
            else if (message.Length >= deviceRetrievedConnectedPeripheral.Length && message.Substring(0, deviceRetrievedConnectedPeripheral.Length) == deviceRetrievedConnectedPeripheral)
            {
            }
            else if (message.Length >= deviceConnectedPeripheral.Length && message.Substring(0, deviceConnectedPeripheral.Length) == deviceConnectedPeripheral)
            {
				_isBleOpen = true;
				_rcvBuffer.Clear();
				OnOpen.Invoke();
			}
			else if (message.Length >= deviceDisconnectedPeripheral.Length && message.Substring(0, deviceDisconnectedPeripheral.Length) == deviceDisconnectedPeripheral)
            {
				if(_isBleOpen)
				{
					close();
					OnErrorClosed.Invoke();
				}                
            }
            else if (message.Length >= deviceDiscoveredService.Length && message.Substring(0, deviceDiscoveredService.Length) == deviceDiscoveredService)
            {
            }
            else if (message.Length >= deviceDiscoveredCharacteristic.Length && message.Substring(0, deviceDiscoveredCharacteristic.Length) == deviceDiscoveredCharacteristic)
            {
                string uuid = _device.args[0];
                if(uuid.Equals(parts[1]))
                {
                    if(_bleInitialized)
                    {
						_iOSBluetoothLESubscribeCharacteristic(uuid, BleUUID.service, BleUUID.rxCharacteristic);
                        return;
                    }                    
                }

                OnOpenFailed.Invoke();
            }
            else if (message.Length >= deviceDidWriteCharacteristic.Length && message.Substring(0, deviceDidWriteCharacteristic.Length) == deviceDidWriteCharacteristic)
            {
            }
            else if (message.Length >= deviceDidUpdateNotificationStateForCharacteristic.Length && message.Substring(0, deviceDidUpdateNotificationStateForCharacteristic.Length) == deviceDidUpdateNotificationStateForCharacteristic)
            {
            }
            else if (message.Length >= deviceDidUpdateValueForCharacteristic.Length && message.Substring(0, deviceDidUpdateValueForCharacteristic.Length) == deviceDidUpdateValueForCharacteristic)
            {
                if (parts.Length >= 3)
                    iOSDataDecoding(parts[1], parts[2]);
            }
        }
    }
Example #8
0
    private void AndroidMessageFoundDevice(string message)
    {
        Debug.Log(message);

        string[] tokens = message.Split(new char[] { ',' });
        CommDevice foundDevice = new CommDevice();
        if(tokens[0].Equals("BT"))
            foundDevice.type = CommDevice.Type.BT;
        else if (tokens[0].Equals("BLE"))
            foundDevice.type = CommDevice.Type.BLE;
        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;
    }
Example #9
0
    private void close()
    {
        if (_openThread != null)
        {
            if (_openThread.IsAlive)
                _openThread.Abort();
        }

        if(_device != null)
        {
            if(_device.type == CommDevice.Type.Serial)
            {
#if UNITY_STANDALONE
                _serialPort.Close();
#endif
            }
            else if(_device.type == CommDevice.Type.BT)
            {
#if UNITY_ANDROID
                if (_androidBluetooth != null)
                    _androidBluetooth.Call("Close");
#elif (UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX)
                _serialPort.Close();
#endif
            }
            else if(_device.type == CommDevice.Type.BLE)
            {
#if UNITY_ANDROID
                if (_androidBluetooth != null)
                    _androidBluetooth.Call("Close");
#elif (UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX || UNITY_IOS)
				_isBleOpen = false;
				_rcvBuffer.Clear();

                if (_bleInitialized)
                {
                    string uuid = _device.args[0];
                    _iOSBluetoothLEUnSubscribeCharacteristic(uuid, BleUUID.service, BleUUID.rxCharacteristic);
                    _iOSBluetoothLEDisconnectPeripheral(uuid);
                }
#endif
            }
        }

        _device = null;
    }
Example #10
0
    public void Open()
    {
        CancelSearch();

        if (isOpen)
        {
            if (_device.Equals(device))
                return;

            close();                
        }

        _device = new CommDevice(device);
        _openThread = new Thread(openThread);
        _openThread.Start();
    }
Example #11
0
    private void searchThread(object parameter)
    {
#if UNITY_ANDROID
        AndroidJNI.AttachCurrentThread();
#endif

        CommDevice.Type[] types = (CommDevice.Type[])parameter;

        foundDevices.Clear();

        bool serial = false;
        bool bt = false;
        bool ble = false;
        if (types.Length == 0)
        {
            serial = true;
            bt = true;
            ble = true;
        }
        else
        {
            foreach (CommDevice.Type t in types)
            {
                if (t == CommDevice.Type.Serial)
                    serial = true;
                else if (t == CommDevice.Type.BT)
                    bt = true;
                else if (t == CommDevice.Type.BLE)
                    ble = true;
            }
        }

        ble &= isSupportBLE;
        bool searchStart = false;

        if (serial)
        {
#if UNITY_STANDALONE
#if (UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN)
            string[] devInfos = SerialPort.GetPortNames();
            for (int i = 0; i < devInfos.Length; i++)
            {
                CommDevice foundDevice = new CommDevice();
                foundDevice.name = devInfos[i];
                foundDevice.type = CommDevice.Type.Serial;
                foundDevice.address = "//./" + devInfos[i];
                foundDevice.args.Add("57600");
                foundDevices.Add(foundDevice);
            }
            if (devInfos.Length > 0)
                _threadOnFoundDevice = true;
#elif (UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX)
            string prefix = "/dev/";
            string[] devInfos = Directory.GetFiles(prefix, "*.*");
			for(int i=0; i<devInfos.Length; i++)
            {
				if(devInfos[i].StartsWith ("/dev/cu."))
                {
					if(devInfos[i].Contains("usb") || devInfos[i].Contains("BT-110") || devInfos[i].Contains("BT-210"))
					{
						CommDevice foundDevice = new CommDevice();
						foundDevice.name = devInfos[i].Substring(prefix.Length);            
						foundDevice.address = devInfos[i];
						foundDevice.type = CommDevice.Type.Serial;
						foundDevice.args.Add("57600");
						foundDevices.Add(foundDevice);
					}					    
				}

			//	CommDevice foundDevice = new CommDevice();
			//	foundDevice.name = devInfos[i].Substring(prefix.Length);            
			//	foundDevice.address = devInfos[i];
			//	foundDevice.type = CommDevice.Type.Serial;
			//	foundDevice.args.Add("57600");
			//	foundDevices.Add(foundDevice);
            }
            if (devInfos.Length > 0)
                _threadOnFoundDevice = true;
#endif
#endif
        }

        if (bt)
        {
#if UNITY_ANDROID
            if (_androidBluetooth != null)
            {
                string[] devInfos = _androidBluetooth.Call<string[]>("GetBondedDevices");
                for(int i=0; i<devInfos.Length; i++)
                {
                    string[] tokens = devInfos[i].Split(new char[] { ',' });
                    CommDevice foundDevice = new CommDevice();
                    foundDevice.type = CommDevice.Type.BT;
                    foundDevice.name = tokens[0];
                    foundDevice.address = tokens[1];
                    foundDevices.Add(foundDevice);
                }
                if (devInfos.Length > 0)
                    _threadOnFoundDevice = true;

                if(!ble)
                {
                    _androidBluetooth.Call("StartSearchBT");
                    _btSearchTimeout = searchTimeout;
                    Debug.Log(string.Format("BT Search time: {0:f1}", _btSearchTimeout));
                    searchStart = true;
                }                
            }
#endif
        }

        if (ble)
        {
#if UNITY_ANDROID
            if (_androidBluetooth != null)
            {
                _androidBluetooth.Call("StartSearchBLE");
                if (bt)
                {
                    _bleSearchTimeout = searchTimeout * 0.3f;                    
                    _btSearchContinue = true;
                }
                else
                    _bleSearchTimeout = searchTimeout;

                Debug.Log(string.Format("BLE Search time: {0:f1}", _bleSearchTimeout));
                searchStart = true;
            }
#elif (UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX || UNITY_IOS)
            if (_bleInitialized)
            {
                _iOSBluetoothLEScanForPeripheralsWithServices(BleUUID.service);
                _bleSearchTimeout = searchTimeout;
                searchStart = true;
            }                
#endif
        }

        if(!searchStart)
            _threadOnSearchCompleted = true;

#if UNITY_ANDROID
        AndroidJNI.DetachCurrentThread();
#endif
        _searchThread.Abort();
        return;
    }