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 services mBtManager.start(); } } }
/** * *************************************************** * * <p>Private methods * * <p>**************************************************** */ private void initialize() { Logs.d(TAG, "# Service : initialize ---"); // Get content manager instance mContentManager = ContentManager.getInstance(mContext, this); // Get connection info instance mConnectionInfo = ConnectionInfo.getInstance(mContext); // Set notification broadcast receiver mReceiver = new NotificationReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(Constants.NOTIFICATION_LISTENER); registerReceiver(mReceiver, filter); // Set battery broadcast receiver IntentFilter iFilter = new IntentFilter(); iFilter.addAction(Intent.ACTION_BATTERY_CHANGED); registerReceiver(mBatteryInfoReceiver, iFilter); // Set SMS listener IntentFilter smsFilter = new IntentFilter(); smsFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); registerReceiver(mSmsListener, smsFilter); // Set telephony listener TelephonyStateListener telephonyListener = new TelephonyStateListener(); TelephonyManager telephony = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); telephony.listen(telephonyListener, PhoneStateListener.LISTEN_SERVICE_STATE); telephony.listen(telephonyListener, PhoneStateListener.LISTEN_CALL_STATE); // 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(); } } }
/** Get connected device name */ public String getDeviceName() { return mConnectionInfo.getDeviceName(); }