我编写了以下代码,其中包含清单文件中包含的权限。
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
答案 0 :(得分:2)
if (mBluetoothAdapter == null)
{
.... // code does not use "return"
}
if (!mBluetoothAdapter.isEnabled()) // crash here if mBluetoothAdapter == null
{
这是你的onclick
代码不正确。如果适配器为null,则第二个if将崩溃。
尝试:
if (mBluetoothAdapter == null)
{
....
}
else if (!mBluetoothAdapter.isEnabled())
{
另外,判断这是一个快速的谷歌,mBluetoothAdapter
应该在onCreate
内而不是在类范围内初始化。
答案 1 :(得分:2)
您好请遵循以下代码进行蓝牙激活: -
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);
}
}
}
并且不要忘记在清单文件中提供这个...
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
答案 2 :(得分:1)
我遇到了完全相同的问题。原来你需要蓝牙和蓝牙管理员权限。仅凭管理员是不够的。
答案 3 :(得分:1)
我有完全相同的问题。每次使用我的“bluetoothAdapter”都会导致应用程序崩溃,直到我在清单文件中添加了“<uses-permission …>
”标记,如下所示:
<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>
请注意文件末尾添加的两行,就在“</manifest>
”标记之前。
现在可行。