Code example for BluetoothDevice

Methods: createInsecureRfcommSocketToServiceRecord, createRfcommSocketToServiceRecord, getAddress, getName

0
	} 
	 
	public Bundle getStateBundle() {
		Bundle states = new Bundle();
		states.putInt(KEY_OLD_STATE, mState);
		states.putString(BluetoothRunnable.KEY_DEVICE_ADDRESS, mConnectedDevice.getAddress());
		states.putString(BluetoothRunnable.KEY_DEVICE_NAME, mConnectedDevice.getName());
		return states;
	} 
	 
	protected BluetoothSocket listenForConnection() throws IOException{
		BluetoothSocket tempSocket;
		 
		/* 
		 * Prepare server socket using UUID and SDP service name 
		 */ 
		BluetoothServerSocket serverSocket = 
				mBluetoothAdapter.listenUsingRfcommWithServiceRecord(SERVICE_NAME, mUUID);
		 
		/* 
		 * Socket is ready to listen for connections, 
		 * set state before calling .accept() because 
		 * it will block until a device connects or  
		 * it times out. 
		 */ 
		setState(BluetoothState.LISTENING);
		 
		//Listen for connections 
		tempSocket = serverSocket.accept();
		 
		/* 
		 * If temp socket is null, the ServerSocket 
		 * didn't receive a connection and timed out 
		 * or it failed to connect for some reason. 
		 *  
		 * Otherwise, it successfully connected and  
		 * we set the state accordingly and return 
		 * the connected socket. 
		 */ 
		if(tempSocket != null){
			connectSuccessful(tempSocket.getRemoteDevice());
			return tempSocket;
		} 
		else{ 
			listenFailed(); 
			return null; 
		} 
	} 
 
	protected BluetoothSocket connectRemoteDevice() throws IOException{
		 
		BluetoothSocket tempSocket = null;
		 
		/* 
		 * Prepare the BluetoothSocket that will be used 
		 * to connect to the remote device. 
		 *  
		 * Depending on the socket security type specified 
		 * at initialization, we'll use different methods  
		 * to create the socket. 
		 *  
		 * The else case should never happen, but it's 
		 * better to include it in case additional 
		 * functionality is added later.  
		 *  
		 * See Android documentation for more information 
		 * about socket types. 
		 */ 
		if(mSocketType == BluetoothSocketType.INSECURE){
			if(D) Log.d(TAG,"Using insecure socket");
			tempSocket = mConnectedDevice.createInsecureRfcommSocketToServiceRecord(mUUID);
		} 
		else if(mSocketType == BluetoothSocketType.SECURE){
			tempSocket = mConnectedDevice.createRfcommSocketToServiceRecord(mUUID);
		} 
		else	return tempSocket;
		 
		/* 
		 * Set to state to connecting