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);
        }
     }
};
}
4

1 回答 1

2

您需要注册使用创建的接收器

registerReceiever(ConnectListener, intentFilter);

在您的情况下,意图过滤器将是蓝牙连接过滤器,例如

IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);

有关更完整的示例,请查看其他帖子

于 2012-10-11T20:33:32.970 回答