我正在开发一款适用于Android服装的应用程序,它正在收集传感器数据(例如加速度,......)并将其与手机上的相应应用程序同步。每次触发传感器(onSensorChanged)时,都会执行以下代码:
Runnable runnable = new Runnable() {
@Override
public void run() {
PutDataMapRequest dataMap = PutDataMapRequest.create(WearConstants.WTP_SENSOR_DATA_CHANGED + sensorType);
dataMap.getDataMap().putInt(WearConstants.ACCURACY, accuracy);
dataMap.getDataMap().putLong(WearConstants.TIMESTAMP_WEAR, timestamp);
dataMap.getDataMap().putFloatArray(WearConstants.VALUES, values);
Wearable.DataApi.putDataItem(mApiClient, dataMap.asPutDataRequest()).setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
@Override
public void onResult(DataApi.DataItemResult dataItemResult) {
Log.d(TAG, "Sending sensor data: " + dataItemResult.getStatus().isSuccess());
}
});
}
}; Thread thread = new Thread(runnable); thread.start();
这很好用,传感器数据传输到手机并显示在应用程序中。该应用程序的第二个功能是可以记笔记。有两种可能用于记笔记:a)在应用程序打开时按下按钮,b)由&#34; Ok Google触发,记下&#34;应用程序关闭时。 (见Google documentation)
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
startActivityForResult(intent, WearConstants.W_SPEECH);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case WearConstants.W_SPEECH: {
if (resultCode == RESULT_OK && data != null) {
List<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String spokenText = results.get(0);
sendSpeech(spokenText);
}
}
}
}
这也很有效,但前提是应用程序没有监听传感器更改。这就是问题所在:每当我在app(或后台服务)正在侦听传感器更改时触发语音操作时,语音都无法识别,我收到错误&#34; Google无法访问&#34;。在我看来,BluetoothLE连接上没有足够的容量。因此,我试图批量传感器数据(在ArrayList中)并在收听语音命令时暂停发送到手机,但没有区别。经过五天寻找解决方案后,我现在变得有点绝望了:-)我希望你有新的想法或提示接下来要尝试什么。
答案 0 :(得分:1)
经过几次额外的研究后,我终于找到了一个解决方案:当我第一次尝试批量传感器数据时,我只是暂停了发送。但是当达到我的限制时,我仍然分别发送每个值。那是个大错。在查找我的数据项以包含一系列数据映射后,电池使用情况和性能看起来相当不错。这是我现在使用的代码:
DataMap dataMap = PutDataMapRequest.create(WearConstants.WTP_SENSOR_DATA_CHANGED + sensorType).getDataMap();
dataMap.putInt(WearConstants.SENSOR, sensorType);
dataMap.putInt(WearConstants.ACCURACY, accuracy);
dataMap.putLong(WearConstants.TIMESTAMP_WEAR, timestamp);
dataMap.putFloatArray(WearConstants.VALUES, values);
dataMaps.add(dataMap);
if(dataMaps.size() > maxMaps) {
final ArrayList dataMapsCopy = (ArrayList) dataMaps.clone();
dataMaps.clear();
Runnable runnable = new Runnable() {
@Override
public void run() {
PutDataMapRequest dataMap = PutDataMapRequest.create(WearConstants.WTP_SENSOR_DATA_CHANGED + WearConstants.BATCH);
dataMap.getDataMap().putDataMapArrayList(WearConstants.DATA_ARRAY, dataMapsCopy);
Wearable.DataApi.putDataItem(mApiClient, dataMap.asPutDataRequest()).setResultCallback(new ResultCallback() {
@Override
public void onResult(DataApi.DataItemResult dataItemResult) {
Log.d(TAG, "Sending sensor data: " + dataItemResult.getStatus().isSuccess());
}
});
}
};
Thread thread = new Thread(runnable);
thread.start();
}