Reputation: 1
I am building an Android App that requires the Bluetooth connection(including the BluetoothSocket) to persist over the whole lifetime of the App. So, I am trying to establish Bluetooth connection through the Application class. But I have met with a problem in the very first step.
Here, I am trying to enable Bluetooth by asking the user's permission. This is the code I have tried:
import android.app.Application;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.widget.Toast;
public class BluetoothApplication extends Application {
BluetoothAdapter bluetoothAdapter;
@Override
public void onCreate() {
super.onCreate();
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(getApplicationContext(), "Device doesn't Support Bluetooth", Toast.LENGTH_SHORT).show();
} else {
if (!bluetoothAdapter.isEnabled()) {
Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableAdapter, 0);
}
}
}
}
But the startActivityForResult
method displays an error saying Cannot resolve method startActivityForResult(android.content.Intent, int)
Is there a way out of this?
Or do I have to explicitly enable Bluetooth without asking the user's permission? (However, this is the situation which I would like to avoid)
Thank you
Upvotes: 0
Views: 695
Reputation: 626
Replace your extends Application
with extends Activity
The android.app.Application class is an optional facility for extending and storing application-global state. There are other ways of doing this, so most apps don't customize this class.
Activities however are what defines every major stage of your application. It wouldn't be possible to build an application without Activities. You will have a main Activity class and this will indeed be defined with 'extends Activity'.
THE ABOVE STATEMENTS WERE TAKEN FROM THE LINK BELOW
For more information, What is the difference between Extends Application and Extends Activity in Android?
Upvotes: 1