jgm
jgm

Reputation: 1270

onServiceConnected in bindservice getting called only once

I am trying to develop an application which connects android and RedBear BLE shield.

Everything works fine the first time. But when I try to connect to the device the second time it doesn't work. OnServiceConnected() get called the first time, but in the second time it doesn't get called.

If I kill the application and start it again everything works fine for the first time, but the connection get failed in the second time.

public TransreceiverInterface registerIntent(Intent intent) {
    Log.d(TAG, "[registerIntent] Trying BluetoothTransreceiver");
    String action = intent.getAction();
    if ("BLUETOOTHLE".equals(action)) {
        Log.d(TAG,
                "[registerIntent] Discovered BLUETOOTHLE ... registering intent");
        device = intent.getParcelableExtra("EXTRAS_DEVICE");
        Log.d(TAG,
                "[registerIntent] Register intent fordevice ["
                        + device.getName() + "] [" + device.getAddress()
                        + "]");

        Log.d(TAG, "[registerIntent] Creating GATT service intent");
        Intent gattServiceIntent = new Intent(context, RBLService.class);
        Log.d(TAG, "[registerIntent] Binding service");

        boolean status = context.getApplicationContext().bindService(
                gattServiceIntent, mServiceConnection,
                Context.BIND_AUTO_CREATE);

        if (status) {
            Log.i(TAG, "Starting Service (just in case)");
            context.startService(gattServiceIntent);

            Log.d(TAG, "[registerIntent] Registering receiver");

            context.registerReceiver(mGattUpdateReceiver,
                    makeGattUpdateIntentFilter());
            Log.d(TAG, "[registerIntent] Registering receiver done");

        } else {
            Log.d(TAG, "[registerIntent] Could not bind service");
            return null;
        }
        return this;
    }
    return null;
}

private final ServiceConnection mServiceConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName componentName,
            IBinder service) {
        Log.d(TAG, "onServiceConnected");

        mBluetoothLeService = ((RBLService.LocalBinder) service)
                .getService();
        if (!mBluetoothLeService.initialize()) {
            Log.e(TAG, "Unable to initialize Bluetooth");
        }
        // Automatically connects to the device upon successful start-up
        // initialization.
        Log.d(TAG, "Connecting Service to device [" + device.getName()
                + "]");
        mBluetoothLeService.connect(device.getAddress());
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        Log.d(TAG, "onServiceDisconnect called");
        mBluetoothLeService = null;
    }
};

Upvotes: 1

Views: 1445

Answers (1)

jgm
jgm

Reputation: 1270

This did the trick.

@Override
public void disconnect() {

    serviceReady = false;
    Log.d(TAG, "unregister gatt update receiver");
    context.unregisterReceiver(mGattUpdateReceiver);
    Log.d(TAG, "diconnect BT service");
    mBluetoothLeService.disconnect();
    Log.d(TAG, "closing service connection");
    mBluetoothLeService.close();
    Log.d(TAG, "closing service connection");
    context.getApplicationContext().unbindService(mServiceConnection);

}

Upvotes: 2

Related Questions