title: Unable to Initialize Bluetooth adapter inside Broadcast receiver
body: <p>In my application inside broadcast adapter at bluetooth on event, I am getting pair devices List  and then checking every Device's name separately if any device contain specific sub String then try to connect with that using this lib <a href="https://github.com/OmarAflak/Bluetooth-Library" rel="nofollow noreferrer">https://github.com/OmarAflak/Bluetooth-Library</a>. at that getting error. </p>
  
  <p><strong>Code of broadcast:</strong></p>
  
  <pre><code>public class BrodcastBlueTooth extends BroadcastReceiver {
  
      public BrodcastBlueTooth() {
  
      }
  
      @Override
      public void onReceive(Context context, Intent intent) {
          Bluetooth bluetooth;
          bluetooth = new Bluetooth(context);
          String DeviceName=null;
          String action = intent.getAction();
  //        Log.d("BroadcastActions", "Action "+action+"received");
          int state;
          BluetoothDevice bluetoothDevice;
          ArrayList&lt;String&gt; pairedDevice;
          pairedDevice=new ArrayList&lt;&gt;();
          final BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
          Set&lt;BluetoothDevice&gt; pairedDevices = mBtAdapter.getBondedDevices();
  
          switch(action)
          {
              case BluetoothAdapter.ACTION_STATE_CHANGED:
                  state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
                  if (state == BluetoothAdapter.STATE_OFF)
                  {
  //                    Toast.makeText(context, "Bluetooth is off", Toast.LENGTH_SHORT).show();
                      Log.d("BroadcastActions", "Bluetooth is off");
                  }
                  else if (state == BluetoothAdapter.STATE_TURNING_OFF)
                  {
  //                    Toast.makeText(context, "Bluetooth is turning off", Toast.LENGTH_SHORT).show();
                      Log.d("BroadcastActions", "Bluetooth is turning off");
                  }
                  else if(state == BluetoothAdapter.STATE_ON)
                  {
                      Toast.makeText(context, "Bluetooth is On", Toast.LENGTH_SHORT).show();
  
                      if (pairedDevices.size() &gt; 0) {
                          for (BluetoothDevice device : pairedDevices) {
                              pairedDevice.add(device.getName());
                          }
                          for(int i=0;i&lt;pairedDevice.size();i++){
                              String name = pairedDevice.get(i);
                              if (name.contains("OBD")){
                              bluetooth.connectToName(name);
                              }else if (name.contains("obd")){
                                  String Tname=name;
                                  Log.d("Tname", Tname);
                                  bluetooth.connectToName(name);
                              }else{
                                  Toast.makeText(context,"No device which has obd name",Toast.LENGTH_SHORT).show();
                              }
                          }
                      }
                      Log.d("DevicePaired", String.valueOf(pairedDevice));
  
                  }
                  break;
  
  
          }
  
      }
  }
  </code></pre>
  
  <p><strong>Log :</strong></p>
  
  <pre><code>2019-03-17 10:16:35.457 17502-17502/com.example.bluetoothlibraryexample E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.bluetoothlibraryexample, PID: 17502
      java.lang.RuntimeException: Unable to start receiver com.example.bluetoothlibraryexample.BrodcastBlueTooth: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Set android.bluetooth.BluetoothAdapter.getBondedDevices()' on a null object reference
          at android.app.ActivityThread.handleReceiver(ActivityThread.java:3047)
          at android.app.ActivityThread.-wrap18(ActivityThread.java)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1561)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:154)
          at android.app.ActivityThread.main(ActivityThread.java:6126)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Set android.bluetooth.BluetoothAdapter.getBondedDevices()' on a null object reference
          at me.aflak.bluetooth.Bluetooth.connectToName(Bluetooth.java:141)
          at me.aflak.bluetooth.Bluetooth.connectToName(Bluetooth.java:150)
          at com.example.bluetoothlibraryexample.BrodcastBlueTooth.onReceive(BrodcastBlueTooth.java:67)
          at android.app.ActivityThread.handleReceiver(ActivityThread.java:3040)
          at android.app.ActivityThread.-wrap18(ActivityThread.java) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1561) 
          at android.os.Handler.dispatchMessage(Handler.java:102) 
          at android.os.Looper.loop(Looper.java:154) 
          at android.app.ActivityThread.main(ActivityThread.java:6126) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
  </code></pre>
  
  <p>if you are wondering might be library's fault then it's not the case I try piece of code inside activity and it's running fine.   </p>