/**
  * Route a message to a bluetooth service. If the bluetooth server is connected, send to that
  * service otherwise, send to the client bluetooth to be sent
  */
 protected void sentToService() {
   try {
     expirePackets();
     if (mPackets.size() > 0 && isBluetoothEnabled()) {
       if (mServerService.isConnected()) {
         if (!mServerService.isSending()) {
           Log.d(TAG, "sentToService: serviceService running");
           mServerService.sendIntent(mPackets.poll());
         }
       } else {
         if (!mClientService.isRunning()) {
           if (lastClientError + 5000 < System.currentTimeMillis()) {
             Log.d(TAG, "sentToService: clientService not running");
             mClientService.startConnection();
           } else {
             Log.d(TAG, "sentToService: clientService error delay");
             mHandler.sendEmptyMessageDelayed(ROUTER_MESSAGE_SEND_QUEUED_MESSAGES, 1000);
           }
         } else if (!mServerService.isSending()) {
           Log.d(TAG, "sentToService: sending to clientService");
           mClientService.sendIntent(mPackets.poll());
         }
       }
     }
   } catch (Exception e) {
     Log.e(TAG, "Failed to route message", e);
   }
 }
  /** cleanup the router service */
  @Override
  public void onDestroy() {
    Log.d(TAG, "onDestroy()");

    getPreferences(this).unregisterOnSharedPreferenceChangeListener(mPreferenceHandler);
    mClientService.stop();
    mServerService.stop();
    service = null;
    super.onDestroy();
  }
 @Override
 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
   if ("bt.device.address".equals(key)) {
     mClientService.onConnectionLost();
   }
 }