gt: 			if(device != null && mBtManager != null) {
context: /*
   * Copyright (C) 2014 Bluetooth Connection Template
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package com.example.babycare.service;
  
  import java.util.ArrayList;
  import java.util.Timer;
  
  import com.example.babycare.bluetooth.*;
  import com.example.babycare.contents.CommandParser;
  import com.example.babycare.fragments.Crying_Show_Activity;
  import com.example.babycare.fragments.Humidity_Show_Activity;
  import com.example.babycare.fragments.Temper_Show_Activity;
  import com.example.babycare.fragments.TurnOver_Show_Activity;
  import com.example.babycare.utils.AppSettings;
  import com.example.babycare.utils.Constants;
  import com.example.babycare.utils.Logs;
  
  import android.app.Service;
  import android.bluetooth.BluetoothAdapter;
  import android.bluetooth.BluetoothDevice;
  import android.content.Context;
  import android.content.Intent;
  import android.content.res.Configuration;
  import android.os.Binder;
  import android.os.Handler;
  import android.os.IBinder;
  import android.os.Message;
  import android.util.Log;
  import android.widget.Toast;
  
  
  public class BTCTemplateService extends Service {
  	private static final String TAG = "LLService";
  	
  	// Context, System
  	private Context mContext = null;
  	private static Handler mActivityHandler = null;
  	private ServiceHandler mServiceHandler = new ServiceHandler();
  	private final IBinder mBinder = new ServiceBinder();
  	
  	// Bluetooth
  	private BluetoothAdapter mBluetoothAdapter = null;		// local Bluetooth adapter managed by Android Framework
  	private BluetoothManager mBtManager = null;
  	private ConnectionInfo mConnectionInfo = null;		// Remembers connection info when BT connection is made 
  	private CommandParser mCommandParser = null;
  	
  	private TransactionBuilder mTransactionBuilder = null;
  	private TransactionReceiver mTransactionReceiver = null;
  	
  	// Auto-refresh timer
  	private Timer mRefreshTimer = null;
  	private Timer mDeleteTimer = null;
      
  	
  	/*****************************************************
  	 *	Overrided methods
  	 ******************************************************/
  	@Override
  	public void onCreate() {
  		Logs.d(TAG, "# Service - onCreate() starts here");
  		
  		mContext = getApplicationContext();
  		initialize();
  	}
  	
  	@Override
  	public int onStartCommand(Intent intent, int flags, int startId) {
  		Logs.d(TAG, "# Service - onStartCommand() starts here");
  		
  		// If service returns START_STICKY, android restarts service automatically after forced close.
  		// At this time, onStartCommand() method in service must handle null intent.
  		return Service.START_STICKY;
  	}
  	
  	@Override
  	public void onConfigurationChanged(Configuration newConfig){
  		// This prevents reload after configuration changes
  		super.onConfigurationChanged(newConfig);
  	}
  
  	@Override
  	public IBinder onBind(Intent intent) {
  		Logs.d(TAG, "# Service - onBind()");
  		return mBinder;
  	}
  	
  	@Override
  	public boolean onUnbind(Intent intent) {
  		Logs.d(TAG, "# Service - onUnbind()");
  		return true;
  	}
  	
  	@Override
  	public void onDestroy() {
  		Logs.d(TAG, "# Service - onDestroy()");
  		finalizeService();
  	}
  	
  	@Override
  	public void onLowMemory (){
  		Logs.d(TAG, "# Service - onLowMemory()");
  		// onDestroy is not always called when applications are finished by Android system.
  		finalizeService();
  	}
  
  	
  	/*****************************************************
  	 *	Private methods
  	 ******************************************************/
  	private void initialize() {
  		Logs.d(TAG, "# Service : initialize ---");
  		
  		AppSettings.initializeAppSettings(mContext);
  		startServiceMonitoring();
  		
  		// Make instances
  		mConnectionInfo = ConnectionInfo.getInstance(mContext);
  		mCommandParser = new CommandParser();
  		
  		// Get local Bluetooth adapter
  		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  		
  		// If the adapter is null, then Bluetooth is not supported
  		if (mBluetoothAdapter == null) {
  			Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
  			return;
  		}
  		
  		if (!mBluetoothAdapter.isEnabled()) {
  			// BT is not on, need to turn on manually.
  			// Activity will do this.
  		} else {
  			if(mBtManager == null) {
  				setupBT();
  			}
  		}
  	}
  	
  	/**
  	 * Send message to device.
  	 * @param message		message to send
  	 */
  	
  	private void sendMessageToDevice(String message) {
  		if(message == null || message.length() < 1)
  			return;
  	
  		
  		TransactionBuilder.Transaction transaction = mTransactionBuilder.makeTransaction();
  		transaction.begin();
  		transaction.setMessage(message);
  		transaction.settingFinished();
  		transaction.sendTransaction();
  	}
  	
  	/**
  	 * 
  	 */
  	
  	
  	/*****************************************************
  	 *	Public methods
  	 ******************************************************/
  	public void finalizeService() {
  		Logs.d(TAG, "# Service : finalize ---");
  		
  		// Stop the bluetooth session
  		mBluetoothAdapter = null;
  		if (mBtManager != null)
  			mBtManager.stop();
  		mBtManager = null;
  		
  		// Stop the timer
  		if(mRefreshTimer != null) {
  			mRefreshTimer.cancel();
  			mRefreshTimer = null;
  		}
  		if(mDeleteTimer != null) {
  			mDeleteTimer.cancel();
  			mDeleteTimer = null;
  		}
  		
  	}
  	
  	/**
  	 * Setting up bluetooth connection
  	 * @param h
  	 */
  	public void setupService(Handler h) {
  		mActivityHandler = h;
  		
  		// Double check BT manager instance
  		if(mBtManager == null)
  			setupBT();
  		
  		// Initialize transaction builder & receiver
  		if(mTransactionBuilder == null)
  			mTransactionBuilder = new TransactionBuilder(mBtManager, mActivityHandler);
  		if(mTransactionReceiver == null)
  			mTransactionReceiver = new TransactionReceiver(mActivityHandler);
  		
  		// If ConnectionInfo holds previous connection info,
  		// try to connect using it.
  		if(mConnectionInfo.getDeviceAddress() != null && mConnectionInfo.getDeviceName() != null) {
  			connectDevice(mConnectionInfo.getDeviceAddress());
  		} 
  		// or wait in listening mode
  		else {
  			if (mBtManager.getState() == BluetoothManager.STATE_NONE) {
  				// Start the bluetooth service
  				mBtManager.start();
  			}
  		}
  	}
  	
      /**
       * Setup and initialize BT manager
       */
  	public void setupBT() {
          Logs.d(TAG, "Service - setupBT()");
  
          // Initialize the BluetoothManager to perform bluetooth connections
          if(mBtManager == null)
          	mBtManager = new BluetoothManager(this, mServiceHandler);
      }
  	
      /**
       * Check bluetooth is enabled or not.
       */
  	public boolean isBluetoothEnabled() {
  		if(mBluetoothAdapter==null) {
  			Log.e(TAG, "# Service - cannot find bluetooth adapter. Restart app.");
  			return false;
  		}
  		return mBluetoothAdapter.isEnabled();
  	}
  	
  	/**
  	 * Get scan mode
  	 */
  	public int getBluetoothScanMode() {
  		int scanMode = -1;
  		if(mBluetoothAdapter != null)
  			scanMode = mBluetoothAdapter.getScanMode();
  		
  		return scanMode;
  	}
  
      /**
       * Initiate a connection to a remote device.
       * @param address  Device's MAC address to connect
       */
  	public void connectDevice(String address) {
  		Logs.d(TAG, "Service - connect to " + address);
  		
  		// Get the BluetoothDevice object
  		if(mBluetoothAdapter != null) {
  			BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
  			
  