Я написал следующий код с разрешениями, включенными в файл манифеста.
package com.cognism.device;
import java.io.IOException;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Trial extends Activity {
// Declare our Views, so we can access them later
private Button activate_buletooth;
static final int REQUEST_ENABLE_BT = 0;
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
/** Called when the activity is first created. */
@Override public void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
// Set Activity Layout
setContentView(R.layout.main);
activate_buletooth = (Button)findViewById(R.id.activate_buletooth);
// Set Click Listener
activate_buletooth.setOnClickListener(new OnClickListener()
{ @Override
public void onClick(View v)
{
if (mBluetoothAdapter == null)
{ // Device does not support Bluetooth
Context context = getApplicationContext();
CharSequence text = "BT not suported";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
if (!mBluetoothAdapter.isEnabled())
{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
// startActivityForResult(discoverableIntent, REQUEST_ENABLE_BT);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{ if(requestCode == REQUEST_ENABLE_BT)
{ if(resultCode == RESULT_CANCELED)
{ Context context = getApplicationContext();
CharSequence text = "bt not available";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
else
{
}
}
}
}
И я получаю исключение, и DDM показывает эти ошибки:
04-18 11:48:03.587: ERROR/AndroidRuntime(616): FATAL EXCEPTION: main
04-18 11:48:03.587: ERROR/AndroidRuntime(616): java.lang.NullPointerException
04-18 11:48:03.587: ERROR/AndroidRuntime(616): at com.cognism.device.Trial$1.onClick(Trial.java:47)
04-18 11:48:03.587: ERROR/AndroidRuntime(616): at android.view.View.performClick(View.java:2485)
04-18 11:48:03.587: ERROR/AndroidRuntime(616): at android.view.View$PerformClick.run(View.java:9080)
04-18 11:48:03.587: ERROR/AndroidRuntime(616): at android.os.Handler.handleCallback(Handler.java:587)
04-18 11:48:03.587: ERROR/AndroidRuntime(616): at android.os.Handler.dispatchMessage(Handler.java:92)
04-18 11:48:03.587: ERROR/AndroidRuntime(616): at android.os.Looper.loop(Looper.java:123)
04-18 11:48:03.587: ERROR/AndroidRuntime(616): at android.app.ActivityThread.main(ActivityThread.java:3647)
04-18 11:48:03.587: ERROR/AndroidRuntime(616): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 11:48:03.587: ERROR/AndroidRuntime(616): at java.lang.reflect.Method.invoke(Method.java:507)
04-18 11:48:03.587: ERROR/AndroidRuntime(616): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-18 11:48:03.587: ERROR/AndroidRuntime(616): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-18 11:48:03.587: ERROR/AndroidRuntime(616): at dalvik.system.NativeStart.main(Native Method)
04-18 11:48:03.608: WARN/ActivityManager(61): Force finishing activity com.cognism.device/.Trial
04-18 11:48:04.127: WARN/ActivityManager(61): Activity pause timeout for HistoryRecord{40683988 com.cognism.device/.Trial}
04-18 11:48:06.248: INFO/Process(616): Sending signal. PID: 616 SIG: 9
04-18 11:48:06.267: INFO/ActivityManager(61): Process com.cognism.device (pid 616) has died.
04-18 11:48:06.278: INFO/WindowManager(61): WIN DEATH: Window{4074aa58 com.cognism.device/com.cognism.device.Trial paused=false}
04-18 11:48:10.397: DEBUG/dalvikvm(429): GC_EXPLICIT freed 7K, 55% free 2519K/5511K, external 716K/1038K, paused 59ms
04-18 11:48:15.290: WARN/ActivityManager(61): Activity destroy timeout for HistoryRecord{40683988 com.cognism.device/.Trial}
04-18 11:48:15.457: DEBUG/dalvikvm(446): GC_EXPLICIT freed 20K, 55% free 2577K/5703K, external 716K/1038K, paused 98ms
04-18 11:48:20.458: DEBUG/dalvikvm(536): GC_EXPLICIT freed 55K, 49% free 3054K/5959K, external 716K/1038K, paused 57ms
04-18 11:48:25.508: DEBUG/dalvikvm(488): GC_EXPLICIT freed 6K, 55% free 2527K/5511K, external 716K/1038K, paused 82ms
04-18 11:51:59.923: DEBUG/SntpClient(61): request time failed: java.net.SocketException: Address family not supported by protocol
04-18 11:55:00.207: DEBUG/dalvikvm(61): GC_CONCURRENT freed 834K, 46% free 4391K/8007K, external 2050K/2558K, paused 7ms+8ms
04-18 11:56:59.967: DEBUG/SntpClient(61): request time failed: java.net.SocketException: Address family not supported by protocol