/// <summary> /// Asynchrones Verbinden zu dem Client. /// </summary> /// <param name="dev_name">Der Name des Geräts.</param> public void connect_to_be(string dev_name) { add = get_device_address(dev_name); BluetoothEndPoint BEP = new BluetoothEndPoint(add, BluetoothUUID); client = new BluetoothClient(); object connection = false; try { client.BeginConnect(BEP, new AsyncCallback(start_read), client); } catch (Exception excep) { Debug.WriteLine(excep.ToString()); } }
public void ThreadedSender() { BTClient = new BluetoothClient(); // Connect to a remote device. try { BTClient.BeginConnect(BTAddress, _N2FServiceGUID, new AsyncCallback(ConnectCallback), BTClient); connectDone.WaitOne(); // Send test data to the remote device. Send(BTClient, "TagValidationString!<EOF>"); sendDone.WaitOne(); } catch (Exception ex) { KillEmAll(); } finally { KillEmAll(); } }
/// <summary> /// If we were not provided with a btClient on creation we need to create one /// </summary> private void ConnectSocket() { try { if (NetworkComms.LoggingEnabled) NetworkComms.Logger.Trace("Connecting bluetooth client with " + ConnectionInfo); bool connectSuccess = true; //We now connect to our target btClient = new BluetoothClient(); //Start the connection using the async version //This allows us to choose our own connection establish timeout IAsyncResult ar = btClient.BeginConnect((ConnectionInfo.RemoteEndPoint as BluetoothEndPoint), null, null); WaitHandle connectionWait = ar.AsyncWaitHandle; try { if (!ar.AsyncWaitHandle.WaitOne(NetworkComms.ConnectionEstablishTimeoutMS, false)) { btClient.Close(); connectSuccess = false; } btClient.EndConnect(ar); } finally { connectionWait.Close(); } if (!connectSuccess) throw new ConnectionSetupException("Timeout waiting for remoteEndPoint to accept bluetooth connection."); } catch (Exception ex) { CloseConnection(true, 17); throw new ConnectionSetupException("Error during bluetooth connection establish with destination (" + ConnectionInfo + "). Destination may not be listening or connect timed out. " + ex.ToString()); } }
private void conectio() { BluetoothClient cliento = new BluetoothClient(); cliento.BeginConnect(dispositivomeromero.DeviceAddress,mUUID,this.callbacko,cliento); }
private void Discovery(Object sender, DoWorkEventArgs e) { InfoMessage("Starting Discovery Round"); var client = new BluetoothClient(); BluetoothDeviceInfo[] availableDevices = client.DiscoverDevicesInRange(); // I've found this to be SLOW! String deviceList = ""; BluetoothDeviceInfo tmpDI = null; foreach (BluetoothDeviceInfo device in availableDevices) { //ServiceRecord[] devServices = device.GetServiceRecords(OurServiceClassId); if (!device.Authenticated/* || devServices.Count() == 0*/) continue; if (tmpDI == null) tmpDI = device; deviceList += device.DeviceName + " : " + device.DeviceAddress + " : " + (device.Authenticated ? "Paired" : "Not Paired") + /*" : " + devServices.Count() + */"\n"; } UpdateDiscoveryDevices(deviceList); if (tmpDI != null) { var peerclient = new BluetoothClient(); peerclient.BeginConnect(tmpDI.DeviceAddress, OurServiceClassId, DiscoveryDeviceConnectCallback, peerclient); } }
private void bt_pair() { BluetoothAddress BtAdress = null; BluetoothClient _blueToothClient; bool _beginConnect; int c = 0; string selDev = null; try { _blueToothClient = new BluetoothClient(); } catch { MessageBox.Show("Bluetooth модуль не подключен!"); return; } button7.BeginInvoke((MethodInvoker)(() => { button7.Text = "Wait..."; button7.Enabled = false; })); comboBox6.BeginInvoke((MethodInvoker)(() => selDev = comboBox6.SelectedItem.ToString())); var devices = _blueToothClient.DiscoverDevices(); while (BtAdress == null) { foreach (var device in devices.Where(device => device.DeviceName == selDev)) { BtAdress = device.DeviceAddress; Console.WriteLine("Device found, Address:" + BtAdress.ToString()); } if (BtAdress != null) break; if (c > 2) { MessageBox.Show("Невозможно подключиться к устройству!"); button7.BeginInvoke((MethodInvoker)(() => { button7.Text = "Open"; button7.Enabled = true; })); return; } devices = _blueToothClient.DiscoverDevices(); c++; } BluetoothDeviceInfo _bluetoothDevice = null; try { _bluetoothDevice = new BluetoothDeviceInfo(BtAdress); } catch (System.ArgumentNullException) { } if (BluetoothSecurity.PairRequest(_bluetoothDevice.DeviceAddress, "1111")) { Console.WriteLine("Pair request result: :D"); if (_bluetoothDevice.Authenticated) { Console.WriteLine("Authenticated result: Cool :D"); _blueToothClient.SetPin("1111"); _blueToothClient.BeginConnect(_bluetoothDevice.DeviceAddress, BluetoothService.SerialPort, null, _bluetoothDevice); _beginConnect = true; bt_serial(BtAdress.ToString()); // Open Serial port for Bluetooth if (btSerialPort != null) { button7.BeginInvoke((MethodInvoker)(() => button7.Text = "Open")); button6.BeginInvoke((MethodInvoker)(() => button6.Enabled = true)); groupBox6.BeginInvoke((MethodInvoker)(() => groupBox6.Enabled = true)); } } else { Console.WriteLine("Authenticated: So sad :("); bt_pair(); } } else { Console.WriteLine("PairRequest: Sad :("); MessageBox.Show("Невозможно подключиться к устройству!"); button7.BeginInvoke((MethodInvoker)(() => { button7.Text = "Open"; button7.Enabled = true; })); return; } }
private void ClientconnectThread() { BluetoothClient client = new BluetoothClient(); updateUI("attempting connect"); client.BeginConnect(deviceInfo.DeviceAddress, mUUID,this.bluetoothClientConnectCallback,client); }