示例#1
0
        /// <summary>
        /// Shows the device.
        /// </summary>
        /// <param name="deviceMessage">The device message.</param>
        private void Connect(DeviceBt device)
        {
            this.dev = device;
            ElementDiffusion ed;

            while (this.elementsDiffusion.TryDequeue(out ed))
            {
                if (ed == null)
                {
                    break;
                }
            }

            ElementFTP eftp;

            while (this.elementsFTP.TryDequeue(out eftp))
            {
                if (eftp == null)
                {
                    break;
                }
            }
            this.mre.Reset();
            this.terminate = false;
            //this.ViderCache(fonctionInterne.ViderCache);
            //Messenger.Default.Send(fonctionInterne.ViderCache);
        }
        /// <summary>
        /// Sends the data to the Receiver.
        /// </summary>
        /// <param name="device">The device.</param>
        /// <param name="content">The content.</param>
        /// <returns>If was sent or not.</returns>
        public async Task <bool> Send(DeviceBt device, string content)
        {
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }

            if (string.IsNullOrEmpty(content))
            {
                throw new ArgumentNullException("content");
            }

            // for not block the UI it will run in a different threat
            var task = Task.Run(() =>
            {
                using (var bluetoothClient = new BluetoothClient())
                {
                    try
                    {
                        var ep = new BluetoothEndPoint(device.DeviceInfo.DeviceAddress, _serviceClassId);

                        // connecting
                        bluetoothClient.Connect(ep);

                        // get stream for send the data
                        var bluetoothStream = bluetoothClient.GetStream();

                        // if all is ok to send
                        if (bluetoothClient.Connected && bluetoothStream != null)
                        {
                            // write the data in the stream
                            var buffer = System.Text.Encoding.UTF8.GetBytes(content);
                            bluetoothStream.Write(buffer, 0, buffer.Length);
                            bluetoothStream.Flush();
                            bluetoothStream.Close();
                            return(true);
                        }
                        return(false);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.ToString());
                        // the error will be ignored and the send data will report as not sent
                        // for understood the type of the error, handle the exception
                    }
                }
                return(false);
            });

            return(await task);
        }
示例#3
0
        public void Configurer()
        {
            //if (this.dev == null && !this.connexionEnCours)
            //{
            //    this.connexionEnCours = true;
            SelectionBT w = new SelectionBT();
            bool?       r = w.ShowDialog();

            if (r.HasValue && r.Value)
            {
                this.dev = w.Liste.SelectedItem as DeviceBt;
                this.Connect(this.dev);
                ThreadStart ts = new ThreadStart(this.BackgroundThradProcess);
                this.BlueToothTheread = new Thread(ts);
                this.BlueToothTheread.Start();
            }
            //}
        }
示例#4
0
 void toto1(DeviceBt bd)
 {
     Devices.Add(bd);
 }