Check if bluetooth is enabled using an Android application












4














I want to check if bluetooth is enabled in a device using an Android application.
I used the .isEnabled method. But there is an error. I found out (by commenting lines) that the error is in .isEnabled method. Can you pls help me to figure this out?



final BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String status = "Bluetooth";

if(bluetooth != null) {
if (bluetooth.isEnabled()) {
String mydeviceaddress = bluetooth.getAddress();
String mydevicename = bluetooth.getName();
status = ("Address "+ mydeviceaddress + " Name" + mydevicename);
Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
} else {
status = ("Bluetooth not enabled");
Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
}
}
}









share|improve this question





























    4














    I want to check if bluetooth is enabled in a device using an Android application.
    I used the .isEnabled method. But there is an error. I found out (by commenting lines) that the error is in .isEnabled method. Can you pls help me to figure this out?



    final BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

    submitButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    String status = "Bluetooth";

    if(bluetooth != null) {
    if (bluetooth.isEnabled()) {
    String mydeviceaddress = bluetooth.getAddress();
    String mydevicename = bluetooth.getName();
    status = ("Address "+ mydeviceaddress + " Name" + mydevicename);
    Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
    } else {
    status = ("Bluetooth not enabled");
    Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
    }
    } else {
    Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
    }
    }
    }









    share|improve this question



























      4












      4








      4


      2





      I want to check if bluetooth is enabled in a device using an Android application.
      I used the .isEnabled method. But there is an error. I found out (by commenting lines) that the error is in .isEnabled method. Can you pls help me to figure this out?



      final BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

      submitButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      String status = "Bluetooth";

      if(bluetooth != null) {
      if (bluetooth.isEnabled()) {
      String mydeviceaddress = bluetooth.getAddress();
      String mydevicename = bluetooth.getName();
      status = ("Address "+ mydeviceaddress + " Name" + mydevicename);
      Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
      } else {
      status = ("Bluetooth not enabled");
      Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
      }
      } else {
      Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
      }
      }
      }









      share|improve this question















      I want to check if bluetooth is enabled in a device using an Android application.
      I used the .isEnabled method. But there is an error. I found out (by commenting lines) that the error is in .isEnabled method. Can you pls help me to figure this out?



      final BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

      submitButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      String status = "Bluetooth";

      if(bluetooth != null) {
      if (bluetooth.isEnabled()) {
      String mydeviceaddress = bluetooth.getAddress();
      String mydevicename = bluetooth.getName();
      status = ("Address "+ mydeviceaddress + " Name" + mydevicename);
      Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
      } else {
      status = ("Bluetooth not enabled");
      Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
      }
      } else {
      Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
      }
      }
      }






      android bluetooth






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 18 '15 at 12:55









      Ziem

      3,99574172




      3,99574172










      asked Jun 28 '12 at 10:20









      Saku

      36126




      36126
























          4 Answers
          4






          active

          oldest

          votes


















          20














          This has worked best for me:



          /**
          * Check for Bluetooth.
          *
          * @return true if Bluetooth is available.
          */
          public boolean isBluetoothAvailable() {
          final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

          return bluetoothAdapter != null
          && bluetoothAdapter.isEnabled()
          && bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON;
          }





          share|improve this answer























          • thanks for sharing it in optimal way... #makesense
            – CoDe
            May 1 '14 at 5:28






          • 2




            Just one thing, why is that method taking a Context object, when is no really using it at all?
            – hmartinezd
            Apr 16 '15 at 13:17






          • 1




            @hmartinezd Good eye. It is now fixed.
            – Jared Burrows
            Apr 16 '15 at 13:27



















          6














          Try this.



          BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
          if (bluetoothAdapter == null) {
          // Device does not support Bluetooth
          } else {
          if (!bluetoothAdapter.isEnabled()) {
          // Bluetooth is not enabled
          }
          }


          in your AndroidManifest.xml File add



          <uses-permission android:name="android.permission.BLUETOOTH" />





          share|improve this answer





















          • I just tried. But that does not work either :(
            – Saku
            Jun 28 '12 at 10:47










          • @Saku what are the errors ? update your post and put error details.
            – Bishan
            Jun 28 '12 at 10:51












          • A 'Force Close' appears. Since the emulator does not support Bluetooth i tried in an actual tab. So i can't give precise exceptions or error messages
            – Saku
            Jun 28 '12 at 11:24












          • @Saku Write Log File and track error massages. then you can sea errors occurred.
            – Bishan
            Jun 29 '12 at 3:13










          • If bluetooth permission is only needed for this detection, don't forget to set it NOT required: <uses-feature android:name="android.hardware.bluetooth" android:required="false" />
            – Murphy
            Jan 16 '13 at 1:20



















          3














          Jared Burrows answer seems like the correct one, however I had to add one addition before it started working. I had to check the Bluetooth state.



          public static boolean isBluetoothAvailable() {
          final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

          return (bluetoothAdapter != null &&
          bluetoothAdapter.isEnabled() &&
          bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON);
          }





          share|improve this answer





























            0














            why not just:



            ...
            return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled();





            share|improve this answer





















              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%2f11242519%2fcheck-if-bluetooth-is-enabled-using-an-android-application%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              20














              This has worked best for me:



              /**
              * Check for Bluetooth.
              *
              * @return true if Bluetooth is available.
              */
              public boolean isBluetoothAvailable() {
              final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

              return bluetoothAdapter != null
              && bluetoothAdapter.isEnabled()
              && bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON;
              }





              share|improve this answer























              • thanks for sharing it in optimal way... #makesense
                – CoDe
                May 1 '14 at 5:28






              • 2




                Just one thing, why is that method taking a Context object, when is no really using it at all?
                – hmartinezd
                Apr 16 '15 at 13:17






              • 1




                @hmartinezd Good eye. It is now fixed.
                – Jared Burrows
                Apr 16 '15 at 13:27
















              20














              This has worked best for me:



              /**
              * Check for Bluetooth.
              *
              * @return true if Bluetooth is available.
              */
              public boolean isBluetoothAvailable() {
              final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

              return bluetoothAdapter != null
              && bluetoothAdapter.isEnabled()
              && bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON;
              }





              share|improve this answer























              • thanks for sharing it in optimal way... #makesense
                – CoDe
                May 1 '14 at 5:28






              • 2




                Just one thing, why is that method taking a Context object, when is no really using it at all?
                – hmartinezd
                Apr 16 '15 at 13:17






              • 1




                @hmartinezd Good eye. It is now fixed.
                – Jared Burrows
                Apr 16 '15 at 13:27














              20












              20








              20






              This has worked best for me:



              /**
              * Check for Bluetooth.
              *
              * @return true if Bluetooth is available.
              */
              public boolean isBluetoothAvailable() {
              final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

              return bluetoothAdapter != null
              && bluetoothAdapter.isEnabled()
              && bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON;
              }





              share|improve this answer














              This has worked best for me:



              /**
              * Check for Bluetooth.
              *
              * @return true if Bluetooth is available.
              */
              public boolean isBluetoothAvailable() {
              final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

              return bluetoothAdapter != null
              && bluetoothAdapter.isEnabled()
              && bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON;
              }






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 22 at 18:24

























              answered Jan 9 '14 at 1:25









              Jared Burrows

              39.4k18121152




              39.4k18121152












              • thanks for sharing it in optimal way... #makesense
                – CoDe
                May 1 '14 at 5:28






              • 2




                Just one thing, why is that method taking a Context object, when is no really using it at all?
                – hmartinezd
                Apr 16 '15 at 13:17






              • 1




                @hmartinezd Good eye. It is now fixed.
                – Jared Burrows
                Apr 16 '15 at 13:27


















              • thanks for sharing it in optimal way... #makesense
                – CoDe
                May 1 '14 at 5:28






              • 2




                Just one thing, why is that method taking a Context object, when is no really using it at all?
                – hmartinezd
                Apr 16 '15 at 13:17






              • 1




                @hmartinezd Good eye. It is now fixed.
                – Jared Burrows
                Apr 16 '15 at 13:27
















              thanks for sharing it in optimal way... #makesense
              – CoDe
              May 1 '14 at 5:28




              thanks for sharing it in optimal way... #makesense
              – CoDe
              May 1 '14 at 5:28




              2




              2




              Just one thing, why is that method taking a Context object, when is no really using it at all?
              – hmartinezd
              Apr 16 '15 at 13:17




              Just one thing, why is that method taking a Context object, when is no really using it at all?
              – hmartinezd
              Apr 16 '15 at 13:17




              1




              1




              @hmartinezd Good eye. It is now fixed.
              – Jared Burrows
              Apr 16 '15 at 13:27




              @hmartinezd Good eye. It is now fixed.
              – Jared Burrows
              Apr 16 '15 at 13:27













              6














              Try this.



              BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
              if (bluetoothAdapter == null) {
              // Device does not support Bluetooth
              } else {
              if (!bluetoothAdapter.isEnabled()) {
              // Bluetooth is not enabled
              }
              }


              in your AndroidManifest.xml File add



              <uses-permission android:name="android.permission.BLUETOOTH" />





              share|improve this answer





















              • I just tried. But that does not work either :(
                – Saku
                Jun 28 '12 at 10:47










              • @Saku what are the errors ? update your post and put error details.
                – Bishan
                Jun 28 '12 at 10:51












              • A 'Force Close' appears. Since the emulator does not support Bluetooth i tried in an actual tab. So i can't give precise exceptions or error messages
                – Saku
                Jun 28 '12 at 11:24












              • @Saku Write Log File and track error massages. then you can sea errors occurred.
                – Bishan
                Jun 29 '12 at 3:13










              • If bluetooth permission is only needed for this detection, don't forget to set it NOT required: <uses-feature android:name="android.hardware.bluetooth" android:required="false" />
                – Murphy
                Jan 16 '13 at 1:20
















              6














              Try this.



              BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
              if (bluetoothAdapter == null) {
              // Device does not support Bluetooth
              } else {
              if (!bluetoothAdapter.isEnabled()) {
              // Bluetooth is not enabled
              }
              }


              in your AndroidManifest.xml File add



              <uses-permission android:name="android.permission.BLUETOOTH" />





              share|improve this answer





















              • I just tried. But that does not work either :(
                – Saku
                Jun 28 '12 at 10:47










              • @Saku what are the errors ? update your post and put error details.
                – Bishan
                Jun 28 '12 at 10:51












              • A 'Force Close' appears. Since the emulator does not support Bluetooth i tried in an actual tab. So i can't give precise exceptions or error messages
                – Saku
                Jun 28 '12 at 11:24












              • @Saku Write Log File and track error massages. then you can sea errors occurred.
                – Bishan
                Jun 29 '12 at 3:13










              • If bluetooth permission is only needed for this detection, don't forget to set it NOT required: <uses-feature android:name="android.hardware.bluetooth" android:required="false" />
                – Murphy
                Jan 16 '13 at 1:20














              6












              6








              6






              Try this.



              BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
              if (bluetoothAdapter == null) {
              // Device does not support Bluetooth
              } else {
              if (!bluetoothAdapter.isEnabled()) {
              // Bluetooth is not enabled
              }
              }


              in your AndroidManifest.xml File add



              <uses-permission android:name="android.permission.BLUETOOTH" />





              share|improve this answer












              Try this.



              BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
              if (bluetoothAdapter == null) {
              // Device does not support Bluetooth
              } else {
              if (!bluetoothAdapter.isEnabled()) {
              // Bluetooth is not enabled
              }
              }


              in your AndroidManifest.xml File add



              <uses-permission android:name="android.permission.BLUETOOTH" />






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jun 28 '12 at 10:25









              Bishan

              6,98241125204




              6,98241125204












              • I just tried. But that does not work either :(
                – Saku
                Jun 28 '12 at 10:47










              • @Saku what are the errors ? update your post and put error details.
                – Bishan
                Jun 28 '12 at 10:51












              • A 'Force Close' appears. Since the emulator does not support Bluetooth i tried in an actual tab. So i can't give precise exceptions or error messages
                – Saku
                Jun 28 '12 at 11:24












              • @Saku Write Log File and track error massages. then you can sea errors occurred.
                – Bishan
                Jun 29 '12 at 3:13










              • If bluetooth permission is only needed for this detection, don't forget to set it NOT required: <uses-feature android:name="android.hardware.bluetooth" android:required="false" />
                – Murphy
                Jan 16 '13 at 1:20


















              • I just tried. But that does not work either :(
                – Saku
                Jun 28 '12 at 10:47










              • @Saku what are the errors ? update your post and put error details.
                – Bishan
                Jun 28 '12 at 10:51












              • A 'Force Close' appears. Since the emulator does not support Bluetooth i tried in an actual tab. So i can't give precise exceptions or error messages
                – Saku
                Jun 28 '12 at 11:24












              • @Saku Write Log File and track error massages. then you can sea errors occurred.
                – Bishan
                Jun 29 '12 at 3:13










              • If bluetooth permission is only needed for this detection, don't forget to set it NOT required: <uses-feature android:name="android.hardware.bluetooth" android:required="false" />
                – Murphy
                Jan 16 '13 at 1:20
















              I just tried. But that does not work either :(
              – Saku
              Jun 28 '12 at 10:47




              I just tried. But that does not work either :(
              – Saku
              Jun 28 '12 at 10:47












              @Saku what are the errors ? update your post and put error details.
              – Bishan
              Jun 28 '12 at 10:51






              @Saku what are the errors ? update your post and put error details.
              – Bishan
              Jun 28 '12 at 10:51














              A 'Force Close' appears. Since the emulator does not support Bluetooth i tried in an actual tab. So i can't give precise exceptions or error messages
              – Saku
              Jun 28 '12 at 11:24






              A 'Force Close' appears. Since the emulator does not support Bluetooth i tried in an actual tab. So i can't give precise exceptions or error messages
              – Saku
              Jun 28 '12 at 11:24














              @Saku Write Log File and track error massages. then you can sea errors occurred.
              – Bishan
              Jun 29 '12 at 3:13




              @Saku Write Log File and track error massages. then you can sea errors occurred.
              – Bishan
              Jun 29 '12 at 3:13












              If bluetooth permission is only needed for this detection, don't forget to set it NOT required: <uses-feature android:name="android.hardware.bluetooth" android:required="false" />
              – Murphy
              Jan 16 '13 at 1:20




              If bluetooth permission is only needed for this detection, don't forget to set it NOT required: <uses-feature android:name="android.hardware.bluetooth" android:required="false" />
              – Murphy
              Jan 16 '13 at 1:20











              3














              Jared Burrows answer seems like the correct one, however I had to add one addition before it started working. I had to check the Bluetooth state.



              public static boolean isBluetoothAvailable() {
              final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

              return (bluetoothAdapter != null &&
              bluetoothAdapter.isEnabled() &&
              bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON);
              }





              share|improve this answer


























                3














                Jared Burrows answer seems like the correct one, however I had to add one addition before it started working. I had to check the Bluetooth state.



                public static boolean isBluetoothAvailable() {
                final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

                return (bluetoothAdapter != null &&
                bluetoothAdapter.isEnabled() &&
                bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON);
                }





                share|improve this answer
























                  3












                  3








                  3






                  Jared Burrows answer seems like the correct one, however I had to add one addition before it started working. I had to check the Bluetooth state.



                  public static boolean isBluetoothAvailable() {
                  final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

                  return (bluetoothAdapter != null &&
                  bluetoothAdapter.isEnabled() &&
                  bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON);
                  }





                  share|improve this answer












                  Jared Burrows answer seems like the correct one, however I had to add one addition before it started working. I had to check the Bluetooth state.



                  public static boolean isBluetoothAvailable() {
                  final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

                  return (bluetoothAdapter != null &&
                  bluetoothAdapter.isEnabled() &&
                  bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON);
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 17 '17 at 8:52









                  Anthony Cannon

                  695313




                  695313























                      0














                      why not just:



                      ...
                      return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled();





                      share|improve this answer


























                        0














                        why not just:



                        ...
                        return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled();





                        share|improve this answer
























                          0












                          0








                          0






                          why not just:



                          ...
                          return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled();





                          share|improve this answer












                          why not just:



                          ...
                          return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled();






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Sep 2 '14 at 14:34









                          ececilla

                          11




                          11






























                              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%2f11242519%2fcheck-if-bluetooth-is-enabled-using-an-android-application%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

                              What visual should I use to simply compare current year value vs last year in Power BI desktop

                              How to ignore python UserWarning in pytest?

                              Alexandru Averescu