0

Hello i have a service to Beacon Scanner in kontak sdk, but i need begin scan from broadcast receive when user turning on and off bluetooth my broadcast

public class Bluetooth_Reciver extends BroadcastReceiver {
private static final String TAG = "Bluetooth_Reciver";
@Override
public void onReceive(Context context, Intent intent) {
    try {
        final String action = intent.getAction();
        if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                    BluetoothAdapter.ERROR);
            switch (state) {
                case BluetoothAdapter.STATE_TURNING_OFF:
                    Intent intent1 = new Intent();
                    intent1.setAction("com.mypackage.name.Beacons.BeaconService");
                    intent1.setPackage("com.mypackage.name.Beacons");
                    context.stopService(intent1);
                    break;
                case BluetoothAdapter.STATE_ON:
                    Intent i = new Intent();
                    i.setAction("com.mypackage.name.Beacons.BeaconService");
                    i.setPackage("com.mypackage.name.Beacons");
                    context.startService(i);

                    break;
            }
        }

    }catch (Exception e){
        e.printStackTrace();
    }
}

}

Manifest services

        <service android:name=".Beacons.BeaconService" ></service>

When user turning on bluetooth enter in broadcast and work but service dont init..

pedroooo
  • 563
  • 1
  • 4
  • 17
  • Possible duplicate of [Starting Service from BroadcastReceiver](http://stackoverflow.com/questions/4641712/starting-service-from-broadcastreceiver) – Mahendran Sakkarai Oct 12 '16 at 21:28

1 Answers1

1

That is how i starting my service from receiver:

@Override
public void onReceive(Context context, Intent intent) {
    // TODO: This method is called when the BroadcastReceiver is receiving
    // an Intent broadcast.
    context.startService(new Intent(context, MyService.class));
}

And it works perfect.

Rahim Rahimov
  • 1,347
  • 15
  • 24