I am attempting to get my android to launch an activity when it registers a Bluetooth connection, I have all the proper permissions and this code runs fine up to the point that it needs to launch the intent. After that it crashes out. Right now I do not have a logcat for this as its on a live test on my phone.
So, lets play another game of “How do I fix this?”
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;
}
}
Thanks to the help of someone on here I now have logcat on my phone! Here is a copy of the errors:
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
In your
onReceive()method you do this:But there is no extra with that name in this broadcast
Intent. If you want to get the device from theIntentthen you need this instead:However, if I look at your code you seem to only want to check if the Intent
ACTIONisACTION_ACL_CONNECTED. If that is all you want to do, then use this code instead: