0

EDITED on 14.11.2018:

I would like to simplify my question, how can I receive a click event in the activity from the Fragment which contains a recycler-view and it has an adapter which handels the click event?

The application looks the following: enter image description here


I am working on an application which is using BLE service (based on the Google BLE example). I have a DeviceScanActivity which is successfully able to search and connects to BLE periphery, this activity starts a new activity, DeviceControlActivity and I bind the BLE service to it:

@Override
protected void onStart() {
    super.onStart();
    Log.d(TAG, "onStart: bind Service. " + mServiceConnection);
    Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
    bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
}

In this activity I am able to send values/messages to the BLE periphery.

My application has another activity, ManualModeActivity. When I start this activity I unbind the BLE service first in case of DeviceControlActivity:

@Override
protected void onStop() {
    super.onStop();
    Log.d(TAG, "onStop: BLE Service is unbind " + mServiceConnection);
    unbindService(mServiceConnection);
    BLEServiceBonded = false;
}

Then in the newly started activity (ManualModeActivity) I bind the BLE service to it, like it is done in case of the DeviceControlActivity.

And now starts the problem, or basically I am new in android programming and I do not know what should I do next to use the BLE service in the ManualModeActivity, I am searching since two days, nothing useful is found.

So let see what I am not able to do, or what makes the challenge for me.

The ManualModeActivity has a ViewPage with two tabs, I have created a ManualTabPagerAdapter which extends FragmentStatePagerAdapter, in this adapter I am loading the fragments, e.g. the ManualDeviceFrament. This fragment has RecyclerView, every item in the RecyclerView has a button, to handle the list and button clicks I have created a ManualListAdapter. And now the challenge, if I click on the button I want to send a BLE message, but the BLE service, mBluetoothLeService is null. I want to use the following command to send message:

mBluetoothLeService.writeCustomCharacteristic("Hello");

In case of both activities I have the following code to handle the service connection:

    private final ServiceConnection mServiceConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder service) {
        mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
        if (!mBluetoothLeService.initialize()) {
            Log.e(TAG, "Unable to initialize Bluetooth");
            finish();
        }
        mBluetoothLeService.connect(mDeviceAddress);
        BLEServiceBonded = true;
    }
    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        BLEServiceBonded = false;
    }
}

So guys I would like to ask you to tell me some possible solutions, because I did not find any useful stuff, I am a newbee so maybe some basic knowledge is missing. If you need further details, just let me know and I will post it here.

tikiss
  • 48
  • 1
  • 8

1 Answers1

-1

did you bind and unbind the service on ManualModeActivity as well? and another thing on overriding onStop method it's better to write overridden code first than calling parent function first. @Override protected void onStop() { unbindService(mServiceConnection); BLEServiceBonded = false; super.onStop(); Log.d(TAG, "onStop: BLE Service is unbind " + mServiceConnection); }

  • Yes, bind and unbind is also done for that Activity, I have also found out the that Service is bind to the Activity, just I cannot call the BLE service because I call it in the recycler-view adapter and there the service is not available. And thanks for your hint regarding the Overriding. – tikiss Nov 14 '18 at 19:00