1

I'm following this tutorial to get started with Bluetooth: https://www.youtube.com/watch?v=y8R2C86BIUc

I want to outscource the bluetooth enable to an separated class and call it from the MainActivity.

I made the new Intent, but following the Video I'm not possible to start the Intent.

I tried importing:

android.support.v7.app.AppCompatActivity;
android.support.v4.content.ContextCompat;

But in both cases it didn't worked out.

Without any imports Android Studio is telling "Can't resolve method"

MAIN

package com.example.lenkzeitapplikation_01;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startBT.switch_BT_ON();
    }

}

STARTBT

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.content.ContextCompat;

import com.fleetboard.sdk.lib.android.log.Log;


public class startBT {

    private static final String Tag ="StartBT";
    static BluetoothAdapter mBluetoothAdapter;

    public static void switch_BT_ON(){
        if(mBluetoothAdapter == null){
            Log.d(Tag, "No BT adapter");
        }if(!mBluetoothAdapter.isEnabled()){
            Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
           startActivity(enableBTIntent);

             //mBluetoothAdapter.enable();
        }

    }

}

Using import android.support.v4.content.ContextCompat:

Error: method startActivity in class ContextCompat cannot be applied to given types; required: Context,Intent,Bundle found: Intent reason: actual and formal argument lists differ in length

Zoe
  • 27,060
  • 21
  • 118
  • 148
Alpha-Centauri
  • 316
  • 2
  • 13
  • you need a Context instance to call `startActivity` on. Easy fix: call it like `startBT.switch_BT_ON();` and change the method signature to `switch_BT_ON(Context context)` and change the call to `context.startActivity(enableBTIntent)` – Tim May 16 '19 at 15:03
  • @Tim Castelijns can you Tell me what an Context instance is absolut? – Alpha-Centauri May 16 '19 at 15:07

1 Answers1

1

Important

Ok, first things first: Activity is one of the different types of Context. AND: startActivity is a method that Context objects have.

Explanation

If you want to start an Activity, you must use a Context object. That's why it was working in the first place, in your MainActivity.

Now that you moved the code to another class, if you wan to use the method startActivity, you must have a reference to a Context object.

But... How?

public class startBT {

    public static void switch_BT_ON(Context context){
        //... Your logic
        context.startActivity(intent);
    }
}

In your activity:

startBT.switch_BT_ON(this);

The this parameter is the MainActivity itself, which is a Context by definition.

It means that:

switch_BT_ON requires a Context.

MainActivity is saying: "Here, use me".

Recommendations This is classic, basic OOP thinking. Study about Object-Oriented Programming, classes and inheritance to learn why the startActivity method worked on the Acivity and not outside of it, passing objects around and handling different scopes.

Read the quick answer about what is an Android Context. Or adventure yourself through the documentation.

Vitor Hugo Schwaab
  • 1,545
  • 20
  • 31