Search code examples
androidbluetoothbroadcastreceiverandroid-broadcastreceiver

BroadCast Receiver onReceive not called


I am trying to send a broadcast from a service but it is not received in the receiver. Following is my code

Bluetooth Service

public class BluetoothService extends Service {

public final static String TAG = "com.example.linvor.BluetoothService";
Intent intent1 = new Intent(TAG);
static boolean isRunning = false;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
    android.os.Debug.waitForDebugger();

    super.onStartCommand(intent, flags, startId);
    //android.os.Debug.waitForDebugger();
    sendBroadcast(intent1);

    return START_NOT_STICKY;
}

}

My Broadcast receiver is as follows.

ServiceBroadCastReceiver

public class ServiceBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    MainActivity.myLabel.setText("BroadCAst received");
    if(intent.getAction().equals(BluetoothService.TAG)) {
    Log.i("Broadcast status","BroadCast received");
}
}
}

and my manifest declaration seems like this.

manifest

<receiver
        android:name=".ServiceBroadCastReceiver"
        android:label="@string/app_name"
        android:exported="true">
        <intent-filter>
            <action android:name="com.example.linvor.BluetoothService"></action>
        </intent-filter>
    </receiver>

One more thing:

when my service is running and sends broadcast to my activity(which is not received), my app shows app not responding.


Solution

  • Thanks to @KeLiuyue and @Mike M. for suggesting me solutions. But the problem was not in the broadcast code. The problem was in the service which was causing my app to not respond and hence I was getting problems with sending and receiving broadcasts in my app. And the reason for my service to not respond was this line.

    android.os.Debug.waitForDebugger();
    

    I just removed this line and everything is working fine.