Exception while activating bluetooth
I have written the following code with permissions included in the manifest file.
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
{
}
}
}
}
And I have been getting exception and the DDMs shows these errors:
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(Acti开发者_JAVA技巧vityThread.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
Hi please follows following code for blue-tooth activation:-
public class BluetoothDemo extends Activity {
TextView out;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
out = (TextView) findViewById(R.id.out);
// Getting the Bluetooth adapter
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
out.append("\nAdapter: " + adapter);
// Check for Bluetooth support in the first place
// Emulator doesn't support Bluetooth and will return null
if(adapter==null) {
out.append("\nBluetooth NOT supported. Aborting.");
return;
}
// Starting the device discovery
out.append("\nStarting discovery...");
adapter.startDiscovery();
out.append("\nDone with discovery...");
// Listing paired devices
out.append("\nDevices Pared:");
Set<BluetoothDevice> devices = adapter.getBondedDevices();
for (BluetoothDevice device : devices) {
out.append("\nFound device: " + device);
}
}
}
and don't forget to give this in manifest file...
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
if (mBluetoothAdapter == null)
{
.... // code does not use "return"
}
if (!mBluetoothAdapter.isEnabled()) // crash here if mBluetoothAdapter == null
{
It's your onclick
code that's not right. If the adapter is null, the second if will crash.
Try:
if (mBluetoothAdapter == null)
{
....
}
else if (!mBluetoothAdapter.isEnabled())
{
Also, judging be a quick google on this, mBluetoothAdapter
should be initialized inside onCreate
rather than at class scope.
I've had the exact same problem. It turned out you need both bluetooth and bluetooth-admin permissions. Admin alone, is not enough.
I had exactly the same issue. Every use of my "bluetoothAdapter" resulted in an application crash, until I added the "<uses-permission …>
" tag in the manifest file, like that :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="charles.software"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".VitaleReader"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
</manifest>
Note the two lines added at the end of the file, just before the "</manifest>
" tag.
Now it works.
精彩评论