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:
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.