Ejemplo n.º 1
0
        private void ConnectToBrain()
        {
            try
            {
                if (!Mindwave.Current.IsConnected)
                {
                    PairedDeviceInfo pdi = Discovered_BT_List.SelectedItem as PairedDeviceInfo;
                    Windows.Networking.Proximity.PeerInformation peer = pdi.PeerInfo;

                    Mindwave.Current.Start(peer);

                    isBrainConnected = true;

                    //Mindwave.Current.allowBlinkSec = 2;
                    //Mindwave.Current.allowBlinkInterval = 2;
                    Mindwave.Current.CurrentValueChanged += Current_CurrentValueChanged;
                    Mindwave.Current.StateChanged        += Current_StateChanged;
                    //Mindwave.Current.Blinking += Current_Blinking;
                    BTDeviceCon_t.Text = Mindwave.Current.PeerInformation.DisplayName + ":" + Mindwave.Current.PeerInformation.ServiceName + ":" + Mindwave.Current.IsConnected.ToString();
                }
                else
                {
                    MessageBox.Show("No Mindwave is find!!!");
                }
            }
            catch (Exception err)
            {
                MessageBox.Show("MindWave Connection Error: " + err.Message + "\r\n" + Mindwave.Current.Err_S);

                Mindwave.Current.Dispose();
            }
        }
Ejemplo n.º 2
0
        private void ConnectDevices_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
        {
            PairedDeviceInfo pairedDevice = (PairedDeviceInfo)ConnectDevices.SelectedItem;

            this.TxtBlock_SelectedID.Text = pairedDevice.ID;
            this.textBlockBTName.Text     = pairedDevice.Name;
            ConnectDevice_Click(sender, e);
        }
 private void PairedDevicesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     // Check whether the user has selected a device
     if (PairedDevicesList.SelectedItem != null)
     {
         PairedDeviceInfo pdi = PairedDevicesList.SelectedItem as PairedDeviceInfo;
         ConnectToDevice(pdi);
     }
 }
Ejemplo n.º 4
0
        async private void ConnectDevice_Click(object sender, RoutedEventArgs e)
        {
            //Revision: No need to requery for Device Information as we alraedy have it:
            DeviceInformation DeviceInfo; // = await DeviceInformation.CreateFromIdAsync(this.TxtBlock_SelectedID.Text);
            PairedDeviceInfo  pairedDevice = (PairedDeviceInfo)ConnectDevices.SelectedItem;

            DeviceInfo = pairedDevice.DeviceInfo;

            bool success = true;

            try
            {
                _service = await RfcommDeviceService.FromIdAsync(DeviceInfo.Id);

                if (_socket != null)
                {
                    // Disposing the socket with close it and release all resources associated with the socket
                    _socket.Dispose();
                }

                _socket = new StreamSocket();
                try
                {
                    // Note: If either parameter is null or empty, the call will throw an exception
                    await _socket.ConnectAsync(_service.ConnectionHostName, _service.ConnectionServiceName);
                }
                catch (Exception ex)
                {
                    success = false;
                    System.Diagnostics.Debug.WriteLine("Connect:" + ex.Message);
                }
                // If the connection was successful, the RemoteAddress field will be populated
                if (success)
                {
                    this.buttonDisconnect.IsEnabled   = true;
                    this.buttonSend.IsEnabled         = true;
                    this.buttonStartRecv.IsEnabled    = true;
                    this.buttonStartProcess.IsEnabled = true;
                    this.buttonStopRecv.IsEnabled     = false;
                    this.StartStopReceive.IsEnabled   = true;

                    string msg = String.Format("Connected to {0}!", _socket.Information.RemoteAddress.DisplayName);
                    //MessageDialog md = new MessageDialog(msg, Title);
                    System.Diagnostics.Debug.WriteLine(msg);
                    //await md.ShowAsync();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Overall Connect: " + ex.Message);
                _socket.Dispose();
                _socket = null;
            }
        }
Ejemplo n.º 5
0
        async private static void ConnectDevice()
        {
            //Revision: No need to requery for Device Information as we alraedy have it:
            DeviceInformation DeviceInfo; // = await DeviceInformation.CreateFromIdAsync(this.TxtBlock_SelectedID.Text);
            PairedDeviceInfo  pairedDevice = _pairedDevices[0];

            DeviceInfo = pairedDevice.DeviceInfo;
            try
            {
                while (_service == null)
                {
                    _service = await RfcommDeviceService.FromIdAsync(DeviceInfo.Id);
                }

                if (_socket != null)
                {
                    // Disposing the socket with close it and release all resources associated with the socket
                    _socket.Dispose();
                }

                _socket = new StreamSocket();
                try
                {
                    // Note: If either parameter is null or empty, the call will throw an exception
                    await _socket.ConnectAsync(_service.ConnectionHostName, _service.ConnectionServiceName);

                    success = true;
                    MainPage.SendReceiveStart();
                }
                catch (Exception ex)
                {
                    success = false;
                    ConnectDevice();
                    System.Diagnostics.Debug.WriteLine("Connect:" + ex.Message);
                }
                // If the connection was successful, the RemoteAddress field will be populated
                if (success)
                {
                    string msg = String.Format("Connected to {0}!", _socket.Information.RemoteAddress.DisplayName);
                    //MessageDialog md = new MessageDialog(msg, Title);
                    System.Diagnostics.Debug.WriteLine(msg);
                    Listen();
                    //await md.ShowAsync();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Overall Connect: " + ex.Message);
                _socket.Dispose();
                _socket = null;
            }
        }
        private async void ConnectToDevice(PairedDeviceInfo deviceInfo)
        {
            FindPairedDevices.IsEnabled    = false;
            ConnectingIndicator.Visibility = Windows.UI.Xaml.Visibility.Visible;

            await(App.Current as App).MainController.SetDevice(deviceInfo);

            if (!(App.Current as App).MainController.IsConnected)
            {
                await new MessageDialog("Connection Failed. Please make sure that your bluetooth device is plugged in properly.").ShowAsync();
            }

            PairedDevicesList.SelectedItem = null;
            FindPairedDevices.IsEnabled    = true;
            ConnectingIndicator.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        }
        async public Task <bool> ConnectDevice(PairedDeviceInfo pairedDevice)//object sender, RoutedEventArgs e)
        {
            DeviceInfo = pairedDevice.DeviceInfo;

            bool success = true;

            try
            {
                _service = await RfcommDeviceService.FromIdAsync(DeviceInfo.Id);

                if (_socket != null)
                {
                    // Disposing the socket with close it and release all resources associated with the socket
                    _socket.Dispose();
                }

                _socket = new StreamSocket();
                try
                {
                    // Note: If either parameter is null or empty, the call will throw an exception
                    await _socket.ConnectAsync(_service.ConnectionHostName, _service.ConnectionServiceName);
                }
                catch (Exception ex)
                {
                    success = false;
                    System.Diagnostics.Debug.WriteLine("Connect:" + ex.Message);
                }
                // If the connection was successful, the RemoteAddress field will be populated
                if (success)
                {
                    string msg = String.Format("Connected to {0}!", _socket.Information.RemoteAddress.DisplayName);
                    System.Diagnostics.Debug.WriteLine(msg);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Overall Connect: " + ex.Message);
                _socket.Dispose();
                _socket = null;
                success = false;
            }
            return(success);
        }
        async private void ConnectDevice_Click(object sender, RoutedEventArgs e)
        {
            DeviceInformation DeviceInfo;
            PairedDeviceInfo  pairedDevice = (PairedDeviceInfo)ConnectDevices.SelectedItem;

            DeviceInfo = pairedDevice.DeviceInfo;

            bool success = true;

            try
            {
                _service = await RfcommDeviceService.FromIdAsync(DeviceInfo.Id);

                if (_socket != null)
                {
                    // Disposing the socket with close it and release all resources associated with the socket
                    _socket.Dispose();
                }

                _socket = new StreamSocket();
                try {
                    // Note: If either parameter is null or empty, the call will throw an exception
                    await _socket.ConnectAsync(_service.ConnectionHostName, _service.ConnectionServiceName);
                }
                catch (Exception ex)
                {
                    success = false;
                    System.Diagnostics.Debug.WriteLine("Connect:" + ex.Message);
                }
                // If the connection was successful, the RemoteAddress field will be populated
                if (success)
                {
                    this.buttonDisconnect.IsEnabled = true;
                    Listen();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Overall Connect: " + ex.Message);
                _socket.Dispose();
                _socket = null;
            }
        }
Ejemplo n.º 9
0
        private async void ConnectDevices_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
        {
            PairedDeviceInfo pairedDevice = (PairedDeviceInfo)ConnectDevices.SelectedItem;

            this.TxtBlock_SelectedID.Text = pairedDevice.ID;
            this.textBlockBTName.Text     = pairedDevice.Name;

            isConnected = await BluetoothSerial.Instance.ConnectDevice(pairedDevice);

            if (isConnected)
            {
                this.buttonDisconnect.IsEnabled = true;
                this.buttonSend.IsEnabled       = true;
                this.buttonStartRecv.IsEnabled  = true;
                this.buttonStopRecv.IsEnabled   = false;

                string msg = String.Format("Connected to {0}!", pairedDevice.DeviceInfo.Name);
                System.Diagnostics.Debug.WriteLine(msg);
            }
        }
Ejemplo n.º 10
0
        async void InitializeRfcommDeviceService()
        {
            try
            {
                DeviceInformationCollection DeviceInfoCollection = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));


                var numDevices = DeviceInfoCollection.Count();

                // By clearing the backing data, we are effectively clearing the ListBox
                _pairedDevices = new ObservableCollection <PairedDeviceInfo>();
                _pairedDevices.Clear();

                if (numDevices == 0)
                {
                    System.Diagnostics.Debug.WriteLine("InitializeRfcommDeviceService: No paired devices found.");
                }
                else
                {
                    // Found paired devices.
                    foreach (var deviceInfo in DeviceInfoCollection)
                    {
                        PairedDeviceInfo pdi = new PairedDeviceInfo(deviceInfo);
                        _pairedDevices.Add(pdi);
                        System.Diagnostics.Debug.WriteLine(pdi.ToString());
                        //find the correct OBD Sensor
                        if (pdi.ToString().Contains("OBDLink MX"))
                        {
                            ConnectDevice(pdi);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("InitializeRfcommDeviceService: " + ex.Message);
            }
        }
Ejemplo n.º 11
0
        async void ConnectDevice(PairedDeviceInfo pairedDevice)
        {
            DeviceInformation DeviceInfo;

            DeviceInfo = pairedDevice.DeviceInfo;

            bool success = true;

            try
            {
                //_service = await RfcommDeviceService.FromIdAsync(DeviceInfo.Id);
                _service = Task.Run <RfcommDeviceService>(async() =>
                {
                    return(await RfcommDeviceService.FromIdAsync(DeviceInfo.Id));
                }).Result;



                if (_socket != null)
                {
                    // Disposing the socket with close it and release all resources associated with the socket
                    _socket.Dispose();
                }

                _socket = new StreamSocket();
                try
                {
                    // Note: If either parameter is null or empty, the call will throw an exception
                    Task.Run(async() => {
                        await _socket.ConnectAsync(_service.ConnectionHostName, _service.ConnectionServiceName);
                    }).Wait();
                    //await _socket.ConnectAsync(_service.ConnectionHostName, _service.ConnectionServiceName);
                }
                catch (Exception ex)
                {
                    success = false;
                    System.Diagnostics.Debug.WriteLine("Connect:" + ex.Message);
                }
                // If the connection was successful, the RemoteAddress field will be populated
                if (success)
                {
                    string msg = String.Format("Connected to {0}!", _socket.Information.RemoteAddress.DisplayName);
                    System.Diagnostics.Debug.WriteLine(msg);

                    //send a message
                    Send("ATZ");
                    Listen();
                    //Send("ATE0");
                    //Send("ATL0");
                    //Send("ATH1");
                    //Send("ATSP 5");
                    //Send(GetMessage());
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Overall Connect: " + ex.Message);
                _socket.Dispose();
                _socket = null;
            }
        }
Ejemplo n.º 12
0
        private void ConnectDevices_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
        {
            PairedDeviceInfo pairedDevice = (PairedDeviceInfo)ConnectDevices.SelectedItem;

            ConnectDevice_Click(sender, e);
        }