Code example for BluetoothSocket

Methods: close, connect, getInputStream, getOutputStream, isConnected

0
				 * from google developer) 
				 */ 
				if(btAdapter.isDiscovering()){
					stopDiscovery(); 
				} 
				btSocket.connect();
				isConnected = true;
				setConnected(); 
				/* 
				 * We are changing state to connected since we have a 
				 * BroadcastReceiver for it and it's more reliable. 
				 */ 
			} catch (Exception e) {
				Log.v(TAG, "Socket connect Exception: ", e);
				// Close the socket upon error 
				try { 
					if (D)
						if(D)Log.e(TAG, "Connection Exception: ", e);
					setDisconnected(); 
					btSocket.close();
				} catch (Exception e2) {
					if (D)
						if(D)Log.v(TAG, "Socket Close Exception: " + e2);
				} 
			} 
		} catch (Exception e) {
			if (D)
				if(D)Log.v(TAG, "Socket init Exception: " + e);
			setDisconnected(); 
		} 
 
		try { 
			if (isConnected()) { 
				if (D)
					if(D)Log.d(TAG, "--> Init btSocket I/O Streams");
				btInStream = btSocket.getInputStream();
				btOutStream = btSocket.getOutputStream();
			} 
		} catch (Exception e) {
			if (D)
				if(D)Log.w(TAG, "Socket I/O Streams Exception" + e);
			setDisconnected(); 
		} 
		return isConnected;
	} 
 
	public static int sendBytes(byte[] data) {
		if (D)
			if(D)Log.e(TAG, "++ sendString ++");
		try { 
			if (btSocket.isConnected()) {
				btOutStream.write(data);
				return RESULT_OK;
			} 
		} catch (IOException e1) {
			if (D)
				if(D)Log.w(TAG, "Sending of data with bt failed" + e1);
			setDisconnected(); 
			return RESULT_IO_EXCEPTION;
		} catch (Exception e1) {
			if(D)Log.w(TAG, "Exception: " + e1);
			return RESULT_EXCEPTION;
		} 
		return RESULT_IO_EXCEPTION;
	} 
 
	// ================================================================ 
 
	public String receiveString() {
		if (D)
			if(D)Log.e(TAG, "++ receiveString ++");
		if (btInStream != null) {
			if (D)
				if(D)Log.d(TAG, "iStream good");
			String inData = null;
			try { 
				if (bufferedReader == null) {
					if (D)
						if(D)Log.e(TAG, "bufferedReader was = null");
					bufferedReader = new BufferedReader(new InputStreamReader(
							btInStream));
				} 
				if (bufferedReader.ready()) {
					if (D)
						if(D)Log.d(TAG, "reader ready");
					inData = bufferedReader.readLine();
					if (D)
						if(D)Log.d(TAG, "DATA= " + inData);
					return inData;
				} 
			} catch (Exception e1) {
				setDisconnected(); 
				if (D)
					if(D)Log.w(TAG, "Exception:", e1);
			} 
		} else { 
			setDisconnected(); 
		} 
		return null; 
	} 
 
	// ================================================================ 
 
	public static boolean isDiscovering() { 
		if (D)
			if(D)Log.e(TAG, "++ isDiscovering ++");
		return btAdapter.isDiscovering();
	} 
 
	// ================================================================ 
 
	public static int closeConnection() { 
		if (D)
			if(D)Log.e(TAG, "++ closeConnection ++");
		try { 
			/* 
			 * The use of && prohibits isConnected() to be invoked if btSocket 
			 * is null. 
			 */ 
			if (btSocket != null) {
				btSocket.close();
			} 
			return RESULT_OK;
		} catch (Exception e) {
			if (D)
				if(D)Log.w(TAG, "Error closing btSocket: ", e);
			return RESULT_IO_EXCEPTION;
		} 
	} 
 
	public static boolean isBluetoothAdapterAvailable() { 
		return btAdapter != null;
	} 
 
	public static void stopDiscovery() { 
		if (D)
			if(D)Log.e(TAG, "++ stopDiscovery ++");
 
		// cancel any prior BT device discovery 
		if (btAdapter.isDiscovering()) {
			btAdapter.cancelDiscovery();
		} 
	} 
 
	public static void enableAdapterNoUserInteraction() { 
		if (D)
			if(D)Log.e(TAG, "++ enableAdapterNoUserInteraction ++");
		btAdapter.enable();
	} 
 
	public static boolean enableAdapter(Activity invokerActivity) {
		if (D)
			if(D)Log.e(TAG, "++ enableAdapter ++");
		if (btAdapter != null)
			if(D)Log.e(TAG, "btAdapter != null");
		if (!btAdapter.isEnabled()) {
			if (D)
				if(D)Log.d(TAG, "enabling adapter");
			Intent enableBtIntent = new Intent(
					BluetoothAdapter.ACTION_REQUEST_ENABLE);
			invokerActivity.startActivityForResult(enableBtIntent,
					REQUEST_ENABLE_BT);
		} 
		int state = btAdapter.getState();
		return state == BluetoothAdapter.STATE_TURNING_ON
				|| state == BluetoothAdapter.STATE_ON;
	} 
 
	public static boolean isEnabled() { 
		return btAdapter.isEnabled();
	} 
 
	public static boolean disableAdapter() { 
		if (D)
			if(D)Log.e(TAG, "++ disableAdapter ++");
		if (btAdapter.getState() == BluetoothAdapter.STATE_OFF) {
			return true; 
		} else { 
			btAdapter.disable();
		} 
		int state = btAdapter.getState();
		return state == BluetoothAdapter.STATE_TURNING_OFF
				|| state == BluetoothAdapter.STATE_OFF;
	} 
 
	public static void makeDiscoverable(MainActivity invokerActivity) {
		if (D)
			if(D)Log.e(TAG, "++ makeDiscoverable ++");
		Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
		intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
		invokerActivity.startActivityForResult(intent, REQUEST_DISCOVERABLE_BT);
	} 
 
	/** 
	 * The BroadcastREceiver will invoke this method to put in more devices in 
	 * the list. 
	 *  
	 * @param device 
	 */ 
	public static void addFoundDeviceTolist(BluetoothDevice device) {
		if (!foundDevices.contains(device)) {
			foundDevices.add(device);
		} 
	} 
 
	// CONNECTION STATE SETTERS AND GETTERS 
 
	public static boolean isConnected() { 
		try { 
			if(D)Log.i(TAG, " btSocket state: " + btSocket.isConnected() + " boolean: " + (connectionState == STATE_CONNECTED));
		} catch (Exception e) {
			Log.w(TAG, "Exception: ", e);
		} 
		return connectionState == STATE_CONNECTED && btSocket.isConnected(); 
	} 
 
	public static boolean isConnecting() { 
		return connectionState == STATE_CONNECTING;
	}