public void Run() { BluetoothSocket socket; while (sendData.state != StateEnum.Connected) { try { socket = mmServerSocket.Accept(); sendData.state = StateEnum.Connecting; } catch (IOException e) { Console.WriteLine(e.ToString() + "!!!"); throw; } if (socket != null) { if (sendData.GetState() == StateEnum.Connecting) { sendData.Connected(socket, socket.RemoteDevice); } else if (sendData.GetState() == StateEnum.Connected) { mmServerSocket.Close(); } } } }
public override void Run() { Logger.Log("AcceptThread Run"); BluetoothSocket socket = null; while (true) { try { socket = serverSocket.Accept(); } catch (IOException e) { Logger.Log("Socket's accept() method failed " + e.Message); break; } if (socket == null) { continue; } // connection accepted service.ManageConnectedSocket(socket); serverSocket.Close(); break; } }
public bool CloseConnection() { try { if (_Mode == e_BtMode.LowEnergy) { SERVER("close server ble"); _GattServer?.Close(); } else if (_Mode == e_BtMode.Socket) { SERVER("close server socket"); _ServerSocket?.Close(); _BluetoothSocket?.Close(); } else { SERVER("no mode selected"); } return(true); } catch (Exception ex) { SERVER_ERR("close server error", ex); return(false); } }
/// <summary> /// try to close the server socket . /// </summary> internal void Cancel() { try { _serverSocket.Close(); } catch { } }
private void DoStopAdvertising() { if (_socket != null) { _socket.Close(); _socket = null; } }
public void WaitForConnection() { _bluetoothSocket = _serverSocket.Accept(); _serverSocket.Close(); _inStream = _bluetoothSocket.InputStream; _inStreamReader = new InputStreamReader(_inStream); _readerBuffer = new BufferedReader(_inStreamReader); }
public void Cancel() { try { serverSocket.Close(); } catch (Java.IO.IOException e) { Log.Error(BluetoothConnectionManager.TAG, "close() of server failed", e); } }
public void Cancel() { try { _serverSocket.Close(); } catch (IOException e) { Log.Error(Constants.TAG, "close() of server failed", e); } }
public void Cancel() { try { serverSocket.Close(); } catch (Java.IO.IOException e) { Log.Error(TAG, "close() of server failed", e); } }
// Closes the connect socket and causes the thread to finish. public void Cancel() { try { mmServerSocket.Close(); } catch (Java.IO.IOException e) { System.Console.WriteLine("Errore: " + e); } }
public void Cancel() { try { serverSocket.Close(); } catch (Exception e) { Toast.MakeText(Application.Context, "Server Close Failed due to " + e.Message, ToastLength.Long).Show(); } }
public void Cancel() { // Log.d(TAG, "Socket Type" + mSocketType + "cancel " + this); try { mmServerSocket.Close(); } catch (Exception) { //Log.e(TAG, "Socket Type" + mSocketType + "close() of server failed", e); } }
public void Cancel() { Logger.Log("Cancel: Cancelling AcceptThread"); try { serverSocket.Close(); } catch (IOException e) { Logger.Log("Cancel: " + e.Message); } }
public void Cancel() { if (Debug) { Log.Debug(TAG, "cancel " + this.ToString()); } try { mmServerSocket.Close(); } catch (Java.IO.IOException e) { Log.Error(TAG, "close() of server failed", e); } }
public override void Run() { BluetoothSocket btSocket; while (!done) { try { btSocket = btServerSocket.Accept(); } catch (Java.IO.IOException e) { e.PrintStackTrace(); done = true; break; } if (btSocket != null) { try { StartConnectedThread(btSocket); btServerSocket.Close(); done = true; break; } catch (Java.IO.IOException e) { e.PrintStackTrace(); done = true; break; } } } btServerSocket.Close(); }
public void Cancel() { if (D) { Log.D(TAG, "Socket Type" + mSocketType + "cancel " + this); } try { mmServerSocket.Close(); } catch (IOException e) { Log.E(TAG, "Socket Type" + mSocketType + "close() of server failed", e); } }
private void CancelAccept() { try { _isAcceptRunning = false; if (_acceptServerSocket != null) { _acceptServerSocket.Close(); _acceptServerSocket = null; } } catch (IOException) { } }
public void Cancel() { if (_stopped) { return; } try { mmServerSocket.Close(); } catch (Java.IO.IOException e) { SAMLog.Error("ABTA::Thread3_Cancel", e); } }
private async void StartAccepting() { UUID uuid = UUID.FromString("6c460207-cba2-43ac-acb9-daf481d10f95"); if (server != null) { server.Close(); } server = bluetoothAdapter.ListenUsingRfcommWithServiceRecord("LakesideLounge", uuid); if (socket != null && socket.IsConnected) { socket.Close(); } while (true) { await Task.Run(() => socket = server.Accept()); StartRecieving(); } }
//private void BtnStatus_Clicked(object sender, EventArgs e) //{ // var state = ble.State; // this.DisplayAlert("Notice", state.ToString(), "OK !"); // if (state == BluetoothState.Off) // { // btnStatus.TextColor = Color.Red; // } // else // { // btnStatus.TextColor = Color.Green; // } //} #region HETWound Connection private async void BtnConnect_Clicked(object sender, EventArgs e) { if (device != null) { switch (device.Name) { case "HET Wound v2.1-001": //LOGIC await adapter.StopScanningForDevicesAsync(); string notif = ("You've connected to " + device.Name); await adapter.ConnectToDeviceAsync(device); Service = await device.GetServiceAsync(het); var char4 = await Service.GetCharacteristicAsync(char4_uuid); if (MainPage.SetDeviceName != null) { MainPage.SetDeviceName(device.Name, "Connected"); } char1 = await Service.GetCharacteristicAsync(char1_uuid); try { await char1.WriteAsync(cmd_string); } catch { } char4.ValueUpdated += CharacteristicsChanged; btnConnect.TextColor = Color.Green; await char4.StartUpdatesAsync(); await DisplayAlert("Notice", notif, "OK"); break; case "Ticwatch E C36B": //LOGIC await adapter.StopScanningForDevicesAsync(); string notif_w = ("You've connected to " + device.Name); //await adapter.ConnectToDeviceAsync(device); try { // set up the server, start and stop listening for new connections BluetoothServerSocket serverSocket = null; // start listening for a new connection if (!isClosed) { try { bluetooth = BluetoothAdapter.DefaultAdapter; if (bluetooth == null) { Console.WriteLine("NO ADAPTER"); } // MY_UUID is the app's UUID string, also used by the client code Console.WriteLine("Starting the server listener"); serverSocket = bluetooth.ListenUsingRfcommWithServiceRecord(ServiceName, UUID.FromString(Uuid)); // keep trying to find a client serverClientSocket = await serverSocket.AcceptAsync(); } catch (IOException ex) { ExceptionErrorLogger.writeFileOnInternalStorage(ex.ToString()); await DisplayAlert("Notice", "Cannot connect to device. Please try again.", "OK"); return; } // If a connection was accepted if (serverClientSocket != null) { var device = serverClientSocket.RemoteDevice; bluetooth = null; serverSocket.Close(); // Do work to manage the connection (in a separate thread) try { await DisplayAlert("Notice", notif_w, "OK"); btnConnect.TextColor = Color.Green; await StartListening(serverClientSocket); } catch (Exception ex) { ExceptionErrorLogger.writeFileOnInternalStorage(ex.ToString()); await DisplayAlert("Notice", "Error connecting to Ticwatch. Please try again.", "OK"); } } } } catch (Exception ex) { ExceptionErrorLogger.writeFileOnInternalStorage(ex.ToString()); } break; default: await DisplayAlert("Notice", "Device not supported !", "OK"); btnConnect.TextColor = Color.Red; break; } } else { await DisplayAlert("Notice", "No Device Selected!", "OK"); btnConnect.TextColor = Color.Red; } //try //{ // if (Xamarin.Forms.Device.RuntimePlatform == Xamarin.Forms.Device.Android) // { // await adapter.StopScanningForDevicesAsync(); // } // if (device != null && device.Name == "HET Wound v2.1-001") // { // string notif = ("You've connected to " + device.Name); // await adapter.ConnectToDeviceAsync(device); // Service = await device.GetServiceAsync(het); // var char4 = await Service.GetCharacteristicAsync(char4_uuid); // if (MainPage.SetDeviceName != null) // { // MainPage.SetDeviceName(device.Name, "Connected"); // } // char1 = await Service.GetCharacteristicAsync(char1_uuid); // try // { // await char1.WriteAsync(cmd_string); // } // catch // { // } // char4.ValueUpdated += CharacteristicsChanged; // btnConnect.TextColor = Color.Green; // await char4.StartUpdatesAsync(); // await DisplayAlert("Notice", notif, "OK"); // } // else // { // await DisplayAlert("Notice", "Device not supported !", "OK"); // btnConnect.TextColor = Color.Red; // } //} //catch(DeviceConnectionException ex) //{ // await DisplayAlert("Notice", ex.Message.ToString(), "OK"); // btnConnect.TextColor = Color.Red; //} }
public void Listen() { List <ServerThread> serverThreads = new List <ServerThread>(); bluetoothAdapter = BluetoothAdapter.DefaultAdapter; if (bluetoothAdapter == null) { Log.Debug(_tag, "No Bluetooth adapter found."); } else if (!bluetoothAdapter.IsEnabled) { Log.Debug(_tag, "Bluetooth adapter is not enabled."); } else { Log.Info(_tag, "Bluetooth adapter ready."); } Task.Run(async() => { try { while (true) // !_cts.IsCancellationRequested { bluetoothAdapter.CancelDiscovery(); // Always do this before connecting. await Task.Delay(100); serverSocket = bluetoothAdapter.ListenUsingRfcommWithServiceRecord("AtroposBluetooth", ServiceUUID); await Task.Delay(100); Log.Debug(_tag, "Server socket ready and waiting."); // Grab the next incoming connection BluetoothSocket socket = serverSocket.Accept(); //socket.Connect(); await Task.Delay(100); serverSocket.Close(); Log.Debug(_tag, $"Server accepted a connection from {socket.RemoteDevice.Address}."); lock (_lock) { // Cache the socket, filed under its address connections[socket.RemoteDevice.Address] = socket; // Create and cache a DataOutputStream for sending data over it outputStreams.Add(socket, new DataOutputStream(socket.OutputStream)); inputStreams.Add(socket, new DataInputStream(socket.InputStream)); } // Create a new thread for this connection await Task.Delay(150); serverThreads.Add(new ServerThread(this, socket).Start()); //AddressBook.Add(new CommsContact() { Name = socket.RemoteDevice.Name, IPaddress = socket.RemoteDevice.Address }); // Ack back to the connection to let it know you're hearing it (and to pass it the address you'll know it by) //ForwardTo(socket.RemoteDevice.Address, SERVER, ACK + CONNECTED_AS + socket.RemoteDevice.Address); } } catch (Exception e) { Log.Debug(_tag, $"Exception in server socket listening: \n{e}\n"); } finally { Log.Debug(_tag, $"Releasing all connections ({serverThreads.Count} of them)."); serverSocket.Close(); foreach (var sThread in serverThreads) { sThread.Stop(); } } }); }
public async void ConnectToWatchAsync() { isClosed = false; system_ids = new Guid[] { watch_uuid }; foreach (IDevice dev in adapter.GetSystemConnectedOrPairedDevices(system_ids)) { if (dev.Name == "Ticwatch E C36B") { try { // set up the server, start and stop listening for new connections serverSocket = null; // start listening for a new connection if (!isClosed) { try { bluetooth = BluetoothAdapter.DefaultAdapter; if (bluetooth == null) { var message = new NotifyMessage { Message = "Bluetooth turned off or not available" }; Device.BeginInvokeOnMainThread(() => MessagingCenter.Send(message, "NotifyMessage")); } // MY_UUID is the app's UUID string, also used by the client code Console.WriteLine("Starting the server listener"); serverSocket = bluetooth.ListenUsingRfcommWithServiceRecord(ServiceName, UUID.FromString(Uuid)); // keep trying to find a client while (serverClientSocket == null) { serverClientSocket = await serverSocket.AcceptAsync(); } } catch (IOException ex) { ExceptionErrorLogger.writeFileOnInternalStorage(ex.ToString()); //await DisplayAlert("Notice", "Cannot connect to device. Please try again.", "OK"); return; } // If a connection was accepted if (serverClientSocket != null) { var device = serverClientSocket.RemoteDevice; bluetooth = null; serverSocket.Close(); // Do work to manage the connection (in a separate thread) try { //await DisplayAlert("Notice", notif_w, "OK"); isClosed = true; StartListeningAsync(serverClientSocket, cancellationToken); } catch (Exception ex) { ExceptionErrorLogger.writeFileOnInternalStorage(ex.ToString()); //await DisplayAlert("Notice", "Error connecting to Ticwatch. Please try again.", "OK"); } } } else { if (serverClientSocket != null) { serverSocket = bluetooth.ListenUsingRfcommWithServiceRecord(ServiceName, UUID.FromString(Uuid)); var device = serverClientSocket.RemoteDevice; bluetooth = null; serverSocket.Close(); // Do work to manage the connection (in a separate thread) try { //await DisplayAlert("Notice", notif_w, "OK"); StartListeningAsync(serverClientSocket, cancellationToken); } catch (Exception ex) { ExceptionErrorLogger.writeFileOnInternalStorage(ex.ToString()); //await DisplayAlert("Notice", "Error connecting to Ticwatch. Please try again.", "OK"); } } } } catch (Exception ex) { ExceptionErrorLogger.writeFileOnInternalStorage(ex.ToString()); } } } }