void BGW_DeviceFinder(object CustomData, UnityBackgroundWorkerArguments e) { try { if (!isBTDevicesSearching) { isBTDevicesSearching = true; DeviceFinderHelper temp = (DeviceFinderHelper)CustomData; do { if (temp.performFastFind) { temp.transferedText = Marshal.PtrToStringAnsi(BTM_GetDevicesNamesFast()); } else { temp.transferedText = Marshal.PtrToStringAnsi(BTM_GetDevicesNames()); } e.Progress++; } while (isBTDevicesSearching && temp.performLoopFind); } } catch (Exception error) { e.HasError = true; e.ErrorMessage = error.Message; } }
/** * Cherche les devices et tente un connexion * @param CustomData information lié au Bluetooth * @param e indique au thread qu'il y a eu une modification */ void FindDevicesBegin(object CustomData, UnityBackgroundWorkerArguments e) { try { DeviceFinderHelper temp = (DeviceFinderHelper)CustomData; temp.transferedText = Marshal.PtrToStringAnsi(BTM_GetDevicesNamesFast()); string[] devicesList = temp.transferedText.Split('\n'); foreach (var device in devicesList) { if (device.Length != 0 && !(listDeviceChecked.Contains(device))) { if (device.Contains(BEGIN_NAME_DEVICE)) { if (checkBLSDevice(device, temp)) { AddDeviceBLS(device, temp.surnameDevice); } } else { listDeviceChecked.AddFirst(device); } } } e.Progress++; } catch (Exception error) { e.HasError = true; e.ErrorMessage = error.Message; } }
/** * Initie le protocole avec un device compatible * @param name nom du device * @param CustomData Permet de recupérer le surnom du device si le protocole est terminé * @return True si le protocole est effectué jusqu'au bout */ bool checkBLSDevice(string nameDevice, object CustomData) { hm.MonitorIn(); try { string status = Marshal.PtrToStringAnsi(BTM_ConnectToDevice(nameDevice)); if (status.Contains("Connected")) { string available = Marshal.PtrToStringAnsi(BTM_ReceiveDataFast(nameDevice)); if (available.Contains("I am available")) { string response = ""; do { Marshal.PtrToStringAnsi(BTM_SendDataFast("Hello, I search BLS device")); Thread.Sleep(2000); response = Marshal.PtrToStringAnsi(BTM_ReceiveDataFast(nameDevice)); } while(!response.Contains(" Terminate.")); response = response.Substring(0, response.Length - 2); if (response.Contains("I Am BLS Device. My Name Is ") && response.EndsWith(" Terminate.")) { DeviceFinderHelper temp = (DeviceFinderHelper)CustomData; string[] splitReponse = response.Split(' '); temp.surnameDevice = splitReponse[splitReponse.Length - 2]; Marshal.PtrToStringAnsi(BTM_SendDataFast("Ok, my name is " + NAME_GAME)); Thread.Sleep(2000); response = Marshal.PtrToStringAnsi(BTM_ReceiveDataFast(nameDevice)); Marshal.PtrToStringAnsi(BTM_DisconnectFromDevice()); if (response.Contains("I am connected with " + NAME_GAME)) { return(true); } else { return(false); } } else { Marshal.PtrToStringAnsi(BTM_SendDataFast("Abort")); } } } } catch (Exception e) { Debug.Log(e.Message); } finally{ hm.MonitorOut(); } return(false); }
void BGW_DeviceFinder_Progress(object CustomData, int Progress) { txtAnswer.text += "Devices search completed!\n"; DeviceFinderHelper temp = (DeviceFinderHelper)CustomData; Debug.Log(temp.transferedText); //temp.transferedText = temp.transferedText.Replace(" ", ""); string[] strDeviceFormattedList = temp.transferedText.Split('\n'); ddDevices.ClearOptions(); foreach (var item in strDeviceFormattedList) { if (item.Length != 0) { ddDevices.options.Add(new Dropdown.OptionData(item)); } } ddDevices.RefreshShownValue(); }
void Start() { deviceFinderHelper = new DeviceFinderHelper() { performFastFind = true, performLoopFind = false }; deviceConnectionHelper = new DeviceConnectionHelper(); dataSenderAndReceiverHelper = new DataCommunicationHelper(); dataReceiverHelper = new DataCommunicationHelper(); byteDataSenderHelper = new ByteDataCommunicationHelper(); byteDataReceiverHelper = new ByteDataCommunicationHelper() { dataToSend = new byte[maxByteLength], receivedData = new byte[maxByteLength], maxLength = maxByteLength }; ubwDeviceFinder = new UnityBackgroundWorker(this, BGW_DeviceFinder, BGW_DeviceFinder_Progress, BGW_DeviceFinder_Done, deviceFinderHelper); ubwDeviceConnector = new UnityBackgroundWorker(this, BGW_DeviceConnector, BGW_DeviceConnector_Progress, BGW_DeviceConnector_Done, deviceConnectionHelper); ubwDataSenderAndReceiver = new UnityBackgroundWorker(this, BGW_SendAndReceiveData, BGW_SendAndReceiveData_Progress, BGW_SendAndReceiveData_Done, dataSenderAndReceiverHelper); ubwDataReceiver = new UnityBackgroundWorker(this, BGW_ReceiveData, BGW_ReceiveData_Progress, BGW_ReceiveData_Done, dataReceiverHelper); ubwByteDataReceiver = new UnityBackgroundWorker(this, BGW_ReceiveByteData, BGW_ReceiveByteData_Progress, BGW_ReceiveByteData_Done, byteDataReceiverHelper); FindDevices(true); StartCoroutine(StatusCoroutine()); }
/** * Constructeur privé */ private FinderDevicesBLS() { listDeviceBLS = new LinkedList <CommunicationDeviceBLS>(); listDeviceChecked = new LinkedList <String>(); dfh = new DeviceFinderHelper(); }