@Override
  public void onDestroy() {
    if (audioRemoteScanController != null) {
      audioRemoteScanController.closeScanController();
      audioRemoteScanController = null;
    }
    if (remoteReleaseHandle != null) {
      remoteReleaseHandle.close();
      remoteReleaseHandle = null;
    }

    super.onDestroy();
  }
  /**
   * Requests access to the given search result.
   *
   * @param asyncScanResultDeviceInfo The search result to attempt to connect to.
   */
  protected void requestConnectToResult(
      final RemoteControlAsyncScanResultDeviceInfo asyncScanResultDeviceInfo) {
    // Inform the user we are connecting
    runOnUiThread(
        new Runnable() {
          public void run() {
            tv_status.setText(
                "Connecting to " + asyncScanResultDeviceInfo.resultInfo.getDeviceDisplayName());
          }
        });

    remoteReleaseHandle =
        audioRemoteScanController.requestDeviceAccess(
            asyncScanResultDeviceInfo.resultInfo,
            new IPluginAccessResultReceiver<AntPlusAudioRemoteControlPcc>() {
              @Override
              public void onResultReceived(
                  AntPlusAudioRemoteControlPcc result,
                  RequestAccessResult resultCode,
                  DeviceState initialDeviceState) {
                if (resultCode == RequestAccessResult.SEARCH_TIMEOUT) {
                  // On a connection timeout the scan automatically resumes, so we inform the user,
                  // and go back to scanning
                  runOnUiThread(
                      new Runnable() {
                        public void run() {
                          Toast.makeText(
                                  Activity_AudioRemoteControlSampler.this,
                                  "Timed out attempting to connect, try again",
                                  Toast.LENGTH_LONG)
                              .show();
                          tv_status.setText("Scanning for controllable devices asynchronously...");

                          // The attempted request access is finished so another request access is
                          // allowed
                          ((ListView) findViewById(R.id.listView_AlreadyConnectedDevices))
                              .setEnabled(true);
                          ((ListView) findViewById(R.id.listView_FoundDevices)).setEnabled(true);
                        }
                      });
                } else {
                  // Otherwise the results, including SUCCESS, behave the same as
                  IPluginAccessResultReceiver.onResultReceived(
                      result, resultCode, initialDeviceState);
                }
              }
            },
            IDeviceStateChangeReceiver);
  }
  private void handleReset() {
    // Release the old access if it exists
    if (remoteReleaseHandle != null) {
      remoteReleaseHandle.close();
      remoteReleaseHandle = null;
    }
    if (audioRemoteScanController != null) {
      audioRemoteScanController.closeScanController();
      audioRemoteScanController = null;
    }

    EnumSet<ControlsMode> requestModes = EnumSet.of(ControlsMode.AUDIO_MODE);
    // int desiredDeviceNumber = 4545;
    // AntPlusAudioRemoteControlPcc.requestAccessByDeviceNumber(requestModes, this,
    // desiredDeviceNumber, 0, IPluginAccessResultReceiver, IDeviceStateChangeReceiver);

    // Async scan
    audioRemoteScanController =
        AntPlusAudioRemoteControlPcc.requestRemoteControlAsyncScanController(
            requestModes,
            this,
            0,
            new IRemoteControlAsyncScanResultReceiver() {

              @Override
              public void onSearchStopped(RequestAccessResult reasonStopped) {
                // The triggers calling this function use the same codes and require the same
                // actions as those received by the standard access result receiver
                IPluginAccessResultReceiver.onResultReceived(null, reasonStopped, DeviceState.DEAD);
              }

              @Override
              public void onSearchResult(final RemoteControlAsyncScanResultDeviceInfo deviceFound) {
                for (RemoteControlAsyncScanResultDeviceInfo i : mScannedDeviceInfos) {
                  // The current implementation of the async scan will reset it's ignore list every
                  // 30s,
                  // So we have to handle checking for duplicates in our list if we run longer than
                  // that
                  if (i.resultInfo.getAntDeviceNumber()
                      == deviceFound.resultInfo.getAntDeviceNumber()) {
                    // Found already connected device, ignore
                    return;
                  }
                }

                // We split up devices already connected to the plugin from un-connected devices to
                // make this information more visible to the user,
                // since the user most likely wants to be aware of which device they are already
                // using in another app
                if (deviceFound.resultInfo.isAlreadyConnected()) {
                  mAlreadyConnectedDeviceInfos.add(deviceFound);
                  runOnUiThread(
                      new Runnable() {
                        @Override
                        public void run() {
                          if (adapter_connDevNameList
                              .isEmpty()) // connected device category is invisible unless there are
                          // some present
                          {
                            findViewById(R.id.listView_AlreadyConnectedDevices)
                                .setVisibility(View.VISIBLE);
                            findViewById(R.id.textView_AlreadyConnectedTitle)
                                .setVisibility(View.VISIBLE);
                          }
                          adapter_connDevNameList.add(
                              deviceFound.resultInfo.getDeviceDisplayName());
                          adapter_connDevNameList.notifyDataSetChanged();
                        }
                      });
                } else {
                  mScannedDeviceInfos.add(deviceFound);
                  runOnUiThread(
                      new Runnable() {
                        @Override
                        public void run() {
                          adapter_devNameList.add(deviceFound.resultInfo.getDeviceDisplayName());
                          adapter_devNameList.notifyDataSetChanged();
                        }
                      });
                }
              }
            });
    initScanDisplay();
  }