WearableListenerService不接收数据

时间:2015-06-11 22:04:24

标签: wear-os

我正在尝试将传感器数据从我的磨损设备发送到手持设备。问题是我的onMessageReceived的{​​{1}}函数永远不会被调用,即使它被创建了。 磨损和移动应用程序的包名称是相同的,这里也是清单文件中服务的条目:

WearableListenerService

在MainActivity中的掌上电脑中:

<service
    android:name=".WatchDataReceiver"
    android:enabled="true"
    android:exported="true" >
</service>

在我的掌上电脑上,服务:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        startService(new Intent(this, WatchDataReceiver.class));

        setContentView(R.layout.activity_fullscreen);
        ...
}

在手表/服装上:

public class WatchDataReceiver extends WearableListenerService {

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(WatchDataReceiver.class.getSimpleName(), "WEAR create");
    }

    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        Log.i(WatchDataReceiver.class.getSimpleName(), "WEAR Data changed " );
    }

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        Log.i(WatchDataReceiver.class.getSimpleName(), "WEAR Message " + messageEvent.getPath());
    }
}

根据LogCat,找到了正确的手持设备,因为在实际 public class MainActivity extends Activity implements SensorEventListener { private static final String LOG_TAG = "Watch: "; private SensorManager sensorManager; private Sensor accelerometer; private GoogleApiClient mGoogleApiClient; private String msg = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub); stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() { @Override public void onLayoutInflated(WatchViewStub stub) { mTextView = (TextView) stub.findViewById(R.id.text); } }); sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_FASTEST); if (accelerometer != null) { Log.v(LOG_TAG, "Accelerometer registered!"); } else { Log.v(LOG_TAG, "Accelerometer _not_ registered!"); } mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Wearable.API).build(); mGoogleApiClient.connect(); } @Override public void onSensorChanged(SensorEvent event) { if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER && event.values.length > 0) { double value_x = event.values[0]; double value_y = event.values[1]; double value_z = event.values[2]; msg = Double.toString(value_x) + ";" + Double.toString(value_y) + ";" + Double.toString(value_z); sendMessageToHandheld(msg); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { //TODO } private void sendMessageToHandheld(String msg) { if (mGoogleApiClient == null) return; final String message = msg; Log.d(LOG_TAG,"sending a message to handheld: "+message); // use the api client to send the heartbeat value to our handheld final PendingResult<NodeApi.GetConnectedNodesResult> nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient); nodes.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() { @Override public void onResult(NodeApi.GetConnectedNodesResult result) { final List<Node> nodes = result.getNodes(); if (nodes != null) { for (int i=0; i<nodes.size(); i++) { final Node node = nodes.get(i); Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), message, null); } } } }); } } 之后的sendMessageToHandheld中,它使用节点的名称执行Log.d。 我错过了什么?

1 个答案:

答案 0 :(得分:1)

您必须将此IntentFilter添加到您的服务中:

<intent-filter>
    <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>

它告诉系统处理Service生命周期(即根据需要启动它)。