Android Studio, Chipmunk, Bluetooth, Java

Viewed 25

I am new to Stackoverflow.

I get the following error and do not understand why:

incompatible types: cannot be converted to Context

Code snippet:

           if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // Add the name and address to an array adapter to show in a ListView
                if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT)) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }

The error line did not show up! The error line is: if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT)) {

I have permissions set in my .xml file. I am very new to Android Studio!

Event Log output: C:\Users\jsla3009\AndroidStudioProjects\Test_Bluetooth\app\src\main\java\com\example\test_bluetooth\MainActivity.java:97: error: incompatible types: cannot be converted to Context if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT)) { ^ C:\Users\jsla3009\AndroidStudioProjects\Test_Bluetooth\app\src\main\java\com\example\test_bluetooth\MainActivity.java: uses or overrides a deprecated API. Recompile with -Xlint:deprecation for details. C:\Users\jsla3009\AndroidStudioProjects\Test_Bluetooth\app\src\main\java\com\example\test_bluetooth\MainActivity.java: uses unchecked or unsafe operations. Recompile with -Xlint:unchecked for details. Some messages have been simplified; recompile with -Xdiags:verbose to get full output

1 Answers

this always references the object you are currently in.

Your error means that you are trying to pass something other than a Context as your first argument in checkSelfPermission, which means that this is not a Context.

  1. Check that MainActivity extends Context or some subclass of it, e.g. AppCompatActivity.
  2. Check that your code is not inside of an anonymous class. If it is, use MainActivity.this instead of this to clarify you mean to pass the MainActivity instance and not the instance of your anonymous class.

If that doesn't help, please share your entire class!

Related