My mainactivity
package com.example.vivek.trackblue;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
BluetoothAdapter bt = BluetoothAdapter.getDefaultAdapter();
Context context = getApplicationContext();
CharSequence text = "Bluetooth is not supported";
int duration = Toast.LENGTH_SHORT;
Toast toast1 = Toast.makeText(context, text, duration);
Button b1 = (Button)findViewById(R.id.button);
private final static int REQUEST_ENABLE_BT=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
if(bt == null){
toast1.show();
}
if(bt.isEnabled())
{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
});
}
}
and this is my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vivek.trackblue">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<permission android:name="android.permission.BLUETOOTH" android:label="BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I am Trying to create an application where the user would press a button to start Bluetooth.
My code shows no errors on the ide but when it gets installed in the device, it crashes with an error "Unfortunately TrackBlue stopped working". Here trackblue is my application's name.