我正在尝试让我的机器人在注册蓝牙连接时启动一个活动,我拥有所有适当的权限,并且此代码运行良好,直到它需要启动意图。之后它崩溃了。现在我没有一个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
答案 0 :(得分:2)
试试这个:
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
// Device is now connected
// Do your stuff here
}
答案 1 :(得分:1)
在onReceive()
方法中执行此操作:
BluetoothAdapter device = intent
.getParcelableExtra(BluetoothDevice.ACTION_ACL_CONNECTED);
但是在这个广播Intent
中没有额外的名字。如果您想从Intent
获取设备,那么您需要这样做:
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
但是,如果我查看您的代码,您似乎只想检查Intent ACTION
是否为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);
}
};
答案 2 :(得分:0)
使用此代码,在某些情况下,还需要成功完成并运行广播接收器代码。
if( "android.intent.action.BOOT_COMPLETED".equals(intent.getAction()))
{
//You broad cast activity code ...
}