I am trying since 4 days to get a small app running. I am using the code from BluetoothChat just for checking if BT is enabled or not. If not it should display the enable-dialog.
I try to run that code on a HTC Wildfire with Android 2.2
package com.example.testagain;
import android.app.Activity;
import android.os.Bundle;
import android.bluetooth.BluetoothAdapter;
import android.util.Log;
import android.widget.Toast;
import android.content.Intent;
public class testit extends Activity {
/** Called when the activity is first created. */
private static final String TAG = "BluetoothChat";
private static final boolean D = true;
private BluetoothAdapter mBluetoothAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(D) Log.e(TAG, "+++ ON CREATE +++");
// Set up the window layout
setContentView(R.layout.main);
// Get local Bluetooth adapter
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// If the adapter is null, then Bluetooth is not supported
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
finish();
return;
}
}
@Override
public void onStart() {
super.onStart();
if(D) Log.e(TAG, "++ ON START ++");
// If BT is not on, request that it be enabled.
// setupChat() will then be called during onActivityResult
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, 1);
// Otherwise, setup the chat session
} else {
Toast.makeText(this, "done!", Toast.LENGTH_LONG).show();
}
}
}
Where’s the error in this code?
Thanks for your help!
Christian
Add
to your AndroidManifest file.