问题
I created a very simple android wearable app for a watch that uses the accelerometer sensor. I have the watch connected to a mobile device over bluetooth. When a certain value is detected by the accelerometer, I'd like for the phone to sound an alarm. I'm quite new to Android Programming, but I've been trying to read the documentation. I was looking at the Notification documentation, but I can't seem to figure out which data layer functionality I need. What I am trying to accomplish seems very simple to me: Run the app on the watch, detect a value, sound alarm on handheld. The documentation is suggesting a more complicated process. Am I overthinking this?
回答1:
Once you detect the event you are interested in on the wear side, you need to send a message to your phone app (using MessageApi) and then you need to have an implementation of WearableListenerService on your phone app that can capture that message and trigger whatever you are interested in.
To send a message to your phone, you should use the CapabilityApi; basically your phone app should declare that it provides, say, "alarm_capability" (or whatever you want to call it) and then your wear, upon detecting the appropriate event, can ask the framework for the connected nodes that provide "alarm_capability". Framework will return the node corresponding to your phone and then you can simply use the MessageApi to send a message to that node.
All of this is pretty straightforward to do and there are a number of samples on our GitHub repo that accomplish similar things (for example, FindMyPhone is pretty close to what you want). You can look a those samples and also read bout the APIs and give it a try. An alternate approach is to use a library that simplifies these steps, for example you can use WearCompanionLibrary.
来源:https://stackoverflow.com/questions/34111186/how-can-i-make-a-wearable-android-app-set-off-the-alarm-on-the-handheld