Code example for BluetoothSocket

Methods: close, connect, getInputStream, getOutputStream

0
 
			try { 
				// Connect the device through the socket. This will block 
				// until it succeeds or throws an exception 
				Log.d("BT", "Connecting" + "\n");
				mmSocket.connect();
				Log.d("BT", "Connected " + "\n");
			} catch (IOException connectException) {
				// Unable to connect; close the socket and get out 
				try { 
					Log.d("BT", "Could not connect... " + "\n");
					mmSocket.close();
				} catch (IOException closeException) { }
				return; 
			} 
 
			connected = new ConnectedThread(mmSocket);
			connected.start();
		} 
 
		/** Will cancel an in-progress connection, and close the socket */ 
		public void cancel() { 
			try { 
				mmSocket.close();
			} catch (IOException e) { }
		} 
	} 
 
	private class ConnectedThread extends Thread {
		private final BluetoothSocket mmSocket;
		private final InputStream mmInStream;
		private final OutputStream mmOutStream;
 
		public ConnectedThread(BluetoothSocket socket) {
			mmSocket = socket;
			InputStream tmpIn = null;
			OutputStream tmpOut = null;
 
			// Get the input and output streams, using temp objects because 
			// member streams are final 
			try { 
				tmpIn = socket.getInputStream();
				tmpOut = socket.getOutputStream();
			} catch (IOException e) { }
 
			mmInStream = tmpIn;
			mmOutStream = tmpOut;
		}