8

When Android Wear goes to sleep mode (screen dimmed), some parts of my code are not executed. I use Timer in background service to trigger some actions, such as sending data from wear to mobile, but the data is not sent. It is sent when I tap the screen to wake it up.

I also try to use Timer trigger a notification with vibration when the screen is off, but it doesn't appear until I tap the screen.

In debug mode (either Bluetooth or USB), data sending and notification work fine.

I suspect this is because when Android Wear is in sleep mode, its CPU works at minimum level because the Timer is still running, but not for GoogleApiClient, IntentService, or Notification.

I have tried many ways to wake CPU up such as AlarmManager, PowerManager, Wakelock, but it did not work for Android Wear.

Anyone has encountered this problem? What is the solution?

taingmeng
  • 119
  • 1
  • 6
  • Could you please give us more details about how exactly you tried to keep device awake? – Okas Sep 17 '14 at 09:38
  • Tried ACQUIRE_CAUSES_WAKEUP? – Umair Sep 21 '14 at 20:48
  • Any news, I am also looking for something similar, but cannot find any solution. – akmal Oct 05 '14 at 10:10
  • 2
    I found a solution using AlarmManager with AlarmManager.ELAPSED_REALTIME_WAKEUP to send a broadcast. Launch a notification or send data to phone inside onReceive. No need to use Wakelock. Note: AlarmManager.RTC_WAKEUP did not work for my case. – taingmeng Oct 07 '14 at 02:00
  • The solution with the Alarm manager and the WakefulBroadcastReceiver seems to be the correct one. You have a concrete example here: https://github.com/AlexKorovyansky/WearPomodoro – nicolas Dec 08 '14 at 13:03

2 Answers2

3

I'm using PowerManger to wakeup my wearable device each time i receive message from handled device. Do not forget to release PowerManager.WakeLock

public abstract class WatchFaceActivity extends Activity {

    private PowerManager.WakeLock mWakeLock;
    private Handler mWakeLockHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clock_watch_face);

        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        mWakeLock = powerManager.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "MyWakelockTag");

        mWakeLockHandler = new Handler();

        IntentFilter messageFilter = new IntentFilter("message-forwarded-from-data-layer");
        MessageReceiver messageReceiver = new MessageReceiver();
        LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, messageFilter);
    }

    public class MessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (!mWakeLock.isHeld()) {
                mWakeLock.acquire();
            }
            mWakeLockHandler.removeCallbacksAndMessages(null);
            mWakeLockHandler.postDelayed(mReleaseRunnable, 5000);
        }
    }

    private Runnable mReleaseRunnable = new Runnable() {

        @Override
        public void run() {
            mWakeLock.release();
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mWakeLockHandler.removeCallbacksAndMessages(null);
        mWakeLock.release();
    }
}

And allow WAKE_UP permission in your Manifest.

<uses-permission android:name="android.permission.WAKE_LOCK" />
Nasc
  • 109
  • 2
  • can you please tell me In which Manifest file(phone or wear) we have to specify the WAKE LOck permission?Because i tried in wearable manifest file after that phone stopped pushing the app to wearable – Vny Kumar Nov 16 '14 at 11:40
  • Instead of extending BroadcastReceiver, why don't you use a `WakefulBroadcastReceiver` ? – Tony Malghem Nov 24 '14 at 10:56
1

You should use AlarmManager along with WakefulBroadcastReceiver and startWakefulService(). See this working solution.
You may find answers for your further questions in chat history on that post here.This is the only solution worked for our app.

@SeaDog is successful in making http calls when device in deep sleep mode with this solution. Try it.

Community
  • 1
  • 1
cgr
  • 4,578
  • 2
  • 28
  • 52