public override void Run() { Log.Info(TAG, "BEGIN mConnectThread"); Name = "ConnectThread"; // Always cancel discovery because it will slow down a connection _service._adapter.CancelDiscovery(); // Make a connection to the BluetoothSocket try { // This is a blocking call and will only return on a // successful connection or an exception mmSocket.Connect(); } catch (Java.IO.IOException e) { _service.ConnectionFailed(); // Close the socket try { mmSocket.Close(); } catch (Java.IO.IOException e2) { Log.Error(TAG, "unable to close() socket during connection failure", e2); } // Start the service over to restart listening mode _service.Start(); return; } // Reset the ConnectThread because we're done lock (this) { _service.connectThread = null; } // Start the connected thread _service.Connected(mmSocket, mmDevice); }
public override void Run() { Log.I(TAG, "BEGIN mConnectedThread"); var buffer = new sbyte[1024]; int bytes; // Keep listening to the InputStream while connected while (true) { try { // Read from the InputStream bytes = mmInStream.Read(buffer); // Send the obtained bytes to the UI Activity chatService.mHandler.ObtainMessage(global::BluetoothChat.BluetoothChat.MESSAGE_READ, bytes, -1, buffer).SendToTarget(); } catch (IOException e) { Log.E(TAG, "disconnected", e); chatService.ConnectionLost(); // Start the service over to restart listening mode chatService.Start(); break; } } }
protected override void OnResume() { base.OnResume(); // Performing this check in onResume() covers the case in which BT was // not enabled during onStart(), so we were paused to enable it... // onResume() will be called when ACTION_REQUEST_ENABLE activity returns. if (chatService != null) { // Only if the state is STATE_NONE, do we know that we haven't started already if (chatService.GetState() == BluetoothChatService.STATE_NONE) { // Start the Bluetooth chat services chatService.Start(); } } }