public override void Run() { BluetoothSocket socket = null; while (service.GetState() != ConnectionState.Connected) { try { socket = serverSocket.Accept(); } catch (Exception e) { Toast.MakeText(Application.Context, "Accept Failed due to " + e.Message, ToastLength.Long).Show(); break; } if (socket != null) { lock (this) { switch (service.GetState()) { case ConnectionState.Listen: case ConnectionState.Connecting: service.Connected(socket, socket.RemoteDevice); break; case ConnectionState.None: case ConnectionState.Connected: try { socket.Close(); } catch (Exception e) { Toast.MakeText(Application.Context, "Could not close socket due to " + e.Message, ToastLength.Long).Show(); } break; } } } } }
public override void Run() { byte[] buffer = new byte[1024]; int bytes; while (service.GetState() == ConnectionState.Connected) { try { bytes = inStream.Read(buffer, 0, buffer.Length); service.handler .ObtainMessage((int)BluetoothState.Read, bytes, -1, buffer) .SendToTarget(); } catch (Exception e) { Toast.MakeText(Application.Context, "Disconnected due to " + e.Message, ToastLength.Long).Show(); service.ConnectionLost(); break; } } }