How to receive click event in activity from a recycler-view adapter












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.










share|improve this question





























    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.










    share|improve this question



























      0












      0








      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.










      share|improve this question















      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.







      android android-fragments bluetooth-lowenergy recycler-adapter android-service-binding






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 16:57







      tikiss

















      asked Nov 12 '18 at 21:50









      tikisstikiss

      3617




      3617
























          1 Answer
          1






          active

          oldest

          votes


















          -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);
          }






          share|improve this answer





















          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270621%2fhow-to-receive-click-event-in-activity-from-a-recycler-view-adapter%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          -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);
          }






          share|improve this answer





















          • 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
















          -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);
          }






          share|improve this answer





















          • 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














          -1












          -1








          -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);
          }






          share|improve this answer












          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);
          }







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 17:50









          Ukesh ShresthaUkesh Shrestha

          73




          73












          • 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


















          • 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
















          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




          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


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270621%2fhow-to-receive-click-event-in-activity-from-a-recycler-view-adapter%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma