永远不会调用WearableSensorListener onDataChanged

时间:2017-03-21 10:46:08

标签: android wear-os android-wear-notification

我最近开始使用Android设备,我想从Android Watch收集传感器数据。

我成功使用了Message API,但Data API对我来说并不起作用。我查了一下:

https://stackoverflow.com/a/34175410/2487227

OnDataChanged is never called

Android Wear onDataChanged isn't called

但他们都没有为我工作。

我的观察代码:

@Override
public void onCreate() {
    super.onCreate();
    // sensor registration
    // ......
    googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    googleApiClient.connect();
    Log.i(this.getClass().toString(), "Google API Client connected");
}

private void sendSensorResult(SensorEvent event) {
    if (!googleApiClient.isConnected())
        googleApiClient.connect();

    PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(DATA_API_PATH);

    putDataMapRequest.getDataMap().putLong("UNIX_TIME", System.currentTimeMillis());
    putDataMapRequest.getDataMap().putInt("TYPE", event.sensor.getType());
    // nanosecond
    putDataMapRequest.getDataMap().putLong("TIME", event.timestamp);
    putDataMapRequest.getDataMap().putFloatArray("DATA", event.values);

    PutDataRequest request = putDataMapRequest.asPutDataRequest().setUrgent();
    Wearable.DataApi.putDataItem(googleApiClient, request)
            .setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
                @Override
                public void onResult(@NonNull DataApi.DataItemResult dataItemResult) {
                    if (dataItemResult.getStatus().isSuccess()) {
                        Log.i(this.getClass().toString(), "Sent: " + event.toString());
                    } else {
                        Log.i(this.getClass().toString(), "FAILED TO SEND: " + event.toString());
                    }
                }
            });
}

电话:

public void onDataChanged(DataEventBuffer dataEvents) {
    Log.i(this.getClass().toString(), "Got data at " + System.currentTimeMillis());
    for (DataEvent dataEvent : dataEvents) {
        Log.i(this.getClass().toString(), "Got event: " + dataEvent.toString());
        if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
            DataMap dataMap = DataMapItem.fromDataItem(dataEvent.getDataItem()).getDataMap();
            String path = dataEvent.getDataItem().getUri().getPath();
            if (path.equals(DATA_API_PATH)) {
                int type = dataMap.getInt("TYPE");
                long time = dataMap.getLong("TIME");
                float[] data = dataMap.getFloatArray("DATA");
                String message = String.format(Locale.UK, "TYPE: %d, TIME: %d, DATA: %s",
                        type,
                        time,
                        Arrays.toString(data));

                showSensorResult(message);
            }
        }
    }
}

电话AndroidManifest.xml

<service
        android:name=".WearableSensorListener"
        android:enabled="true"
        android:exported="true">
    <intent-filter>
        <action android:name="com.google.android.gms.wearable.DATA_CHANGED"/>
        <data android:host="*"
              android:scheme="wear"
              android:pathPrefix="/wearable-sensor"/>
    </intent-filter>
</service>

我可以看到来自手表的日志,但没有任何内容显示在手机上。

可穿戴设备和手机的applicationId是相同的。

我是否需要任何其他权限才能使用Data API?

1 个答案:

答案 0 :(得分:1)

尽可能早地使用GooglePlayServices版本。

打开 .\Sdk\extras\google\m2repository\com\google\android\gms\play-services-wearable\ 并使用例如8.3.0。查看您的磨损设备的GooglePlayServices版本。它的数量必须大于你使用的数量。

我认为你的实施不正确。这是错误的。

private void sendSensorResult(SensorEvent event) {
    if (!googleApiClient.isConnected())
        googleApiClient.connect();
///....
Wearable.DataApi.putDataItem(googleApiClient, request)

我的意思是你必须等待连接。因此,仅在putDataItem回调后使用onConnected