nms
nms

Reputation: 577

BluetoothAdapter.getDefaultAdapter() throws exception

I try to add Bluetooth to my libgdx android project. I added the following to the Android manifest:

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

I tried to run

   mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

in non activity class - exception . I have read that the class should be activity class. Okay. I have created

   public class BluetoothServer extends Activity {
    ...
       public void GetBluetoothAdapter () {
         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
      }
    }

I ran it as follows:

BluetoothServer.GetBluetoothAdapter()

Crashes again in the same place. I tried on two devices with Bluetooth onboard. Of course, Bluetooth is enabled on these devices. I have no idea what to do.

Thanks.

Upvotes: 0

Views: 3182

Answers (2)

Kishan Vaghela
Kishan Vaghela

Reputation: 7938

To get a BluetoothAdapter representing the local Bluetooth adapter, when running on JELLY_BEAN_MR1 and below, call the static getDefaultAdapter() method; when running on JELLY_BEAN_MR2 and higher, retrieve it through getSystemService(String) with BLUETOOTH_SERVICE. Source

Upvotes: 0

Caner
Caner

Reputation: 59258

It would help greatly if you could get the reason for the crash. For some reason you seem not able to access stacktrace, try at least this to see the crash reason:

...
try {
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
} catch (Exception e) {

    Toast toast = Toast.makeText(getApplicationContext(), e.getMessage(), 1000);
    toast.show();
}
...

Upvotes: 2

Related Questions