Android Open Source - BleSensorTag Ble Service Binding Activity

From Project

Back to project page BleSensorTag.


The source code is released under:

MIT License

If you think the Android project BleSensorTag listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package sample.ble.sensortag;
/*w w w  . j  av  a  2  s. c om*/
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import sample.ble.sensortag.config.AppConfig;
import sample.ble.sensortag.ble.BleActionsReceiver;
import sample.ble.sensortag.ble.BleServiceListener;

public class BleServiceBindingActivity extends Activity
                                       implements BleServiceListener,
                                                  ServiceConnection {
    private final static String TAG = BleServiceBindingActivity.class.getSimpleName();

    public static final String EXTRAS_DEVICE_NAME = "DEVICE_NAME";
    public static final String EXTRAS_DEVICE_ADDRESS = "DEVICE_ADDRESS";

    private String deviceName;
    private String deviceAddress;
    private BleService bleService;
    private BroadcastReceiver bleActionsReceiver =
            AppConfig.REMOTE_BLE_SERVICE ? new BleActionsReceiver(this) : null;

    public void onCreate(Bundle savedInstanceState) {

        final Intent intent = getIntent();
        deviceName = intent.getStringExtra(EXTRAS_DEVICE_NAME);
        deviceAddress = intent.getStringExtra(EXTRAS_DEVICE_ADDRESS);

    protected void onStart() {

        if (AppConfig.REMOTE_BLE_SERVICE)
            registerReceiver(bleActionsReceiver, BleActionsReceiver.createIntentFilter());
        final Intent gattServiceIntent = new Intent(this, BleService.class);
        bindService(gattServiceIntent, this, BIND_AUTO_CREATE);

    protected void onStop() {
        if (bleService != null)
        if (AppConfig.REMOTE_BLE_SERVICE)

    public String getDeviceName() {
        return deviceName;

    public String getDeviceAddress() {
        return deviceAddress;

    public BleService getBleService() {
        return bleService;

    public void onConnected() {

    public void onDisconnected() {

    public void onServiceDiscovered() {

    public void onDataAvailable(String serviceUuid, String characteristicUUid, String text, byte[] data) {

    public void onServiceConnected(ComponentName name, IBinder service) {
        bleService = ((BleService.LocalBinder) service).getService();
        //noinspection PointlessBooleanExpression,ConstantConditions
        if (!AppConfig.REMOTE_BLE_SERVICE)
        if (!bleService.getBleManager().initialize(getBaseContext())) {
            Log.e(TAG, "Unable to initialize Bluetooth");

        // Automatically connects to the device upon successful start-up initialization.
        bleService.getBleManager().connect(getBaseContext(), deviceAddress);

    public void onServiceDisconnected(ComponentName name) {
        bleService = null;
        //TODO: show toast

Java Source Code List