0

I am building a piece of code that imitates an activity when the phone receives an active Bluetooth connection. This is to run as a service so it can be picked up on in the moment.

Here is the code I am working with. Right now its not launching the intent but its not failing either. How do I get this to run properly?

import android.app.Service;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class detectService extends Service{

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

private BroadcastReceiver ConnectListener = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) 
        {
                intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            //Start Second Activity
                Intent secondIntent = new Intent(detectService.this, otherClass.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(secondIntent);
        }
     }
};
}
CodeMonkeyAlx
  • 813
  • 4
  • 16
  • 32

1 Answers1

2

You need to register the receiver you created using

registerReceiever(ConnectListener, intentFilter);

the intent filter in your case will be the bluetooth connnect filter, something like

IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);

For a more complete example look at this other post

Community
  • 1
  • 1
Michaeldcooney
  • 1,435
  • 10
  • 14
  • Sweet, just now it gives me an issue the likes of which I cant logcat since my phone is not hooked up to my computer. Thanks for the tip though! – CodeMonkeyAlx Oct 11 '12 at 20:48