0

我试图让我的android在注册蓝牙连接时启动一个活动,我拥有所有适当的权限,并且这段代码运行良好,直到它需要启动意图。之后它崩溃了。现在我没有一个logcat,因为它在我的手机上进行实时测试。

那么,让我们再玩一个“我该如何解决这个问题?”的游戏。

public class detectService extends Service {

public void onCreate() {
    IntentFilter filter1 = new IntentFilter(
            BluetoothDevice.ACTION_ACL_CONNECTED);
    this.registerReceiver(mReceiver, filter1);
    Toast.makeText(this, "Starting Service!", Toast.LENGTH_LONG).show();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

// The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        BluetoothAdapter device = intent
                .getParcelableExtra(BluetoothDevice.ACTION_ACL_CONNECTED);

        if (device.equals(action)) {
            // Device is now connected
            // Start Second Activity
            Intent otherIntent = new Intent(detectService.this,
                    otherClass.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(otherIntent);
        }
    };
};
@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
}

感谢这里有人的帮助,我的手机上现在有了 logcat!这是错误的副本:

E/AndroidRuntime(  567): FATAL EXCEPTION: main
E/AndroidRuntime(  567): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.device.action.ACL_CONNECTED (has extras) } in lionsimages.com.bluetoothalert.detectService$1@4051ad68
E/AndroidRuntime(  567):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
E/AndroidRuntime(  567):    at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(  567):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(  567):    at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime(  567):    at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(  567):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  567):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(  567):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
E/AndroidRuntime(  567):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
E/AndroidRuntime(  567):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  567): Caused by: java.lang.NullPointerException
E/AndroidRuntime(  567):    at lionsimages.com.bluetoothalert.detectService$1.onReceive(detectService.java:39)
E/AndroidRuntime(  567):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
E/AndroidRuntime(  567):    ... 9 more
4

3 回答 3

2

试试这个:

 if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            // Device is now connected
            // Do your stuff here
        }
于 2012-10-12T18:49:17.860 回答
1

在你的onReceive()方法中,你这样做:

   BluetoothAdapter device = intent
            .getParcelableExtra(BluetoothDevice.ACTION_ACL_CONNECTED);

但是在这个广播中没有那个名字的额外内容Intent。如果你想从那里获取设备,Intent那么你需要这个:

   BluetoothDevice device = intent
            .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

但是,如果我查看您的代码,您似乎只想检查 IntentACTION是否为ACTION_ACL_CONNECTED. 如果您只想这样做,请改用以下代码:

public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
        // Device is now connected
        // Start Second Activity
        Intent otherIntent = new Intent(detectService.this,
                otherClass.class);
        // NOTE: I changed "intent" to "otherIntent" in the following line as well!
        otherIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(otherIntent);
    }
};
于 2012-10-12T19:01:30.697 回答
0

使用此代码,在某些情况下,广播接收器代码的成功完成和运行也需要它。

    if( "android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) 
      { 
                  //You broad cast activity code ...
       }
于 2012-10-12T18:00:21.123 回答