我正在学习 android 并试图实现 android's Bluetooth guide中的代码,但这给我带来了很多麻烦,因为“教程”不是很清楚。过了一会儿,我确实找到了一个例子:
package edu.ius.rwisman.AndroidRemoteDiscovery;
import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import java.util.Set;
public class AndroidRemoteDiscoveryActivity extends ListActivity {
private static final int REQUEST_ENABLE_BT = 2;
private ArrayAdapter<String> mArrayAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
this.setListAdapter(mArrayAdapter);
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) mArrayAdapter.add("Device does not support Bluetooth");
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
mArrayAdapter.add("Paired devices");
for (BluetoothDevice device : pairedDevices) // Loop through paired devices
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
mArrayAdapter.add("Discovered devices");
final BroadcastReceiver mReceiver = new BroadcastReceiver() { // Create a BroadcastReceiver for ACTION_FOUND
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) { // When discovery finds a device
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
mBluetoothAdapter.startDiscovery();
}
}
当我尝试在真实设备和模拟器上运行它时,这个崩溃了。我还编写了自己的代码,结合了我所理解的所有内容,它看起来像这样:
package com.example.cellbotproxy;
import java.util.Set;
import android.app.Activity;
import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MainActivity extends ListActivity {
private TextView stateMessage;
private BroadcastReceiver mReceiver;
private BluetoothAdapter mBluetoothAdapter;
private ArrayAdapter<String> mArrayAdapter;
private boolean receiverRegisted = false;
private final static int REQUEST_ENABLE_BT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stateMessage = (TextView)findViewById(R.id.state_message);
stateMessage.setText(R.string.idle_message);
mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
this.setListAdapter(mArrayAdapter);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
stateMessage.setText(R.string.btnotsupported_message);
}
else{
if(!mBluetoothAdapter.isEnabled()) {
stateMessage.setText(R.string.btoff_message);
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_ENABLE_BT){
if(resultCode == RESULT_CANCELED){
stateMessage.setText(R.string.requestcanceled_message);
}
else if (resultCode == RESULT_OK){
stateMessage.setText(R.string.bton_message);
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
mReceiver = new BroadcastReceiver(){ // Create a BroadcastReceiver for ACTION_FOUND
public void onReceive(Context context, Intent intent){
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)){ // When discovery finds a device
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
receiverRegisted = true;
mBluetoothAdapter.startDiscovery();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
if(receiverRegisted){
unregisterReceiver(mReceiver);
}
}
}
这个也崩溃了。
经过几分钟的艰苦测试,我得出一个结论:如果我尝试使用 ListActivity 扩展 MainActivity,应用程序将在启动时崩溃。同时,如果我用 Activity 扩展 MainActivity,它会毫无问题地启动。但在这种情况下,我无法使用this.setListAdapter(mArrayAdapter);
,我看到的唯一解决方案是创建另一个活动,该活动将在启动时从 MainActivity 调用并执行此蓝牙代码。所以,我的问题是:1)应用程序在启动时崩溃是否正常,因为 MainActivity 正在扩展 ListActivity 2)除了我提到的解决方案之外,还有其他可行的解决方案吗?
谢谢。
日志猫:
07-14 01:38:47.957: E/Trace(2709): error opening trace file: No such file or directory (2)
07-14 01:38:48.899: D/dalvikvm(2709): GC_FOR_ALLOC freed 163K, 11% free 2531K/2824K, paused 60ms, total 67ms
07-14 01:38:48.906: I/dalvikvm-heap(2709): Grow heap (frag case) to 3.202MB for 635812-byte allocation
07-14 01:38:49.076: D/dalvikvm(2709): GC_FOR_ALLOC freed 2K, 9% free 3149K/3448K, paused 162ms, total 162ms
07-14 01:38:49.166: D/dalvikvm(2709): GC_CONCURRENT freed <1K, 9% free 3149K/3448K, paused 32ms+9ms, total 95ms
07-14 01:38:49.166: D/dalvikvm(2709): WAIT_FOR_CONCURRENT_GC blocked 35ms
07-14 01:38:49.187: I/dalvikvm-heap(2709): Grow heap (frag case) to 3.676MB for 500416-byte allocation
07-14 01:38:49.336: D/dalvikvm(2709): GC_FOR_ALLOC freed <1K, 8% free 3638K/3940K, paused 141ms, total 141ms
07-14 01:38:49.456: D/AndroidRuntime(2709): Shutting down VM
07-14 01:38:49.456: W/dalvikvm(2709): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-14 01:38:49.476: E/AndroidRuntime(2709): FATAL EXCEPTION: main
07-14 01:38:49.476: E/AndroidRuntime(2709): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cellbotproxy/com.example.cellbotproxy.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.os.Handler.dispatchMessage(Handler.java:99)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.os.Looper.loop(Looper.java:137)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-14 01:38:49.476: E/AndroidRuntime(2709): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 01:38:49.476: E/AndroidRuntime(2709): at java.lang.reflect.Method.invoke(Method.java:511)
07-14 01:38:49.476: E/AndroidRuntime(2709): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-14 01:38:49.476: E/AndroidRuntime(2709): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-14 01:38:49.476: E/AndroidRuntime(2709): at dalvik.system.NativeStart.main(Native Method)
07-14 01:38:49.476: E/AndroidRuntime(2709): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
07-14 01:38:49.476: E/AndroidRuntime(2709): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.Activity.setContentView(Activity.java:1881)
07-14 01:38:49.476: E/AndroidRuntime(2709): at com.example.cellbotproxy.MainActivity.onCreate(MainActivity.java:27)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.Activity.performCreate(Activity.java:5104)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-14 01:38:49.476: E/AndroidRuntime(2709): ... 11 more
07-14 01:38:53.066: I/Process(2709): Sending signal. PID: 2709 SIG: 9