title: Get Bluetooth device information from the Android bluetooth device picker
body: <p>I am developing an Android application which involves showing the user a list of nearby Bluetooth devices and connecting to the device selected by them. I'm trying to use the system bluetooth device picker as shown in these posts: </p>
  
  <p><a href="https://stackoverflow.com/questions/8712365/how-to-retrieve-bluetooth-device-info-with-android-bluetooth-device-picker">How to retrieve Bluetooth device info with Android Bluetooth device picker?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/12894945/android-bluetooth-device-picker-usage">Android Bluetooth Device Picker Usage</a></p>
  
  <p>The device picker does show, but I can't find out which device was selected in my code. The toast inside the <code>onReceive</code> does not show, which suggests that no broadcast is being received.</p>
  
  <p>Another problem I faced is that if I try to start the device picker activity inside the <code>onRequestPermissionsResult</code>, the device picker does not show up at all, despite clicking 'allow' in the request permission dialog. The toast inside doesn't get displayed either.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>//Code inside Fragment
  BroadcastReceiver mReceiver;
  BluetoothAdapter bluetoothAdapter;
  BluetoothSocket bsock;
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      myView = inflater.inflate(R.layout.controller_mode_layout,container,false);
  
       bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
  //Get location access permission.
      if (bluetoothAdapter != null) {
          if (bluetoothAdapter.isEnabled()) {
              ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, reqCode);
          }
      }
  //Receiver to get the selected device information 
      mReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
                  context.unregisterReceiver(this);
                  BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
              Toast.makeText(context,"Device"+device.getAddress(), Toast.LENGTH_SHORT).show();
              try {
                  bsock=device.createRfcommSocketToServiceRecord(UUID.fromString("00002415-0000-1000-8000-00805F9B34FB"));
                  bsock.connect();
                  //Send and receive data logic follows
  
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      };
      getActivity().registerReceiver(mReceiver, new IntentFilter("android.bluetooth.devicepicker.action.DEVICE_SELECTED"));
  
      showDevicePicker();
  
      return myView;
  }
  
  
  
  @Override
  public void onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResults)
  {
      Toast.makeText(getActivity(),"Permission result", Toast.LENGTH_SHORT).show();
      if((requestCode == reqCode) &amp;&amp; (grantResults[0] == PackageManager.PERMISSION_GRANTED))
      {
          //Not working
         // showDevicePicker();
      }
  }
  
  public void showDevicePicker()
  {
      //Launch built in bluetooth device picker activity
      startActivity( new Intent("android.bluetooth.devicepicker.action.LAUNCH")
              .putExtra("android.bluetooth.devicepicker.extra.NEED_AUTH", false)
              .putExtra("android.bluetooth.devicepicker.extra.FILTER_TYPE", 0)
              .putExtra("android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE","com.example.ankit2.controllerapp1")
              .putExtra("android.bluetooth.devicepicker.extra.DEVICE_PICKER_LAUNCH_CLASS","com.example.ankit2.controllerapp1.Fragment1")
              .setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS));
  
  }
  </code></pre>
  
  <p>Any help will be appreciated.</p>
  
  <p>Note: I tested the code on a Lenovo K3 note running Marshmallow.</p>