简短摘要:我正在尝试使用 PutDataRequest 和 GoogleApiClient 将数据从 Android Wear 手表发送到 Android 手机。日志似乎显示数据已成功发送,但 onDataChanged 永远不会触发。我正在使用 Android Studio 1.0.2。我没有使用任何模拟器,而是使用我拥有的 Android Wear 手表——我已通过 Android Wear 设备和手机上的 Android Wear 应用程序配对并启用调试。在手机和 Wear 的 AndroidManifest.xml 中,我都包含了 com.google.android.gms.version。
在Android Phone(Android 4.4.4 版本)上,我使用了一个监听服务,它通过AndroidManifest.xml 绑定,并通过手机上的主Activity 启动。从日志中,我可以确认服务已在手机上成功创建,但从未收到任何数据(准确地说,onDataChanged 永远不会触发)。
<!-- Phone manifest, registers the listener -->
<service android:name=".DataLayerListenerService" >
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
这是在手机上运行的侦听器服务:
public class DataLayerListenerService extends WearableListenerService {
private static final String TAG = DataLayerListenerService.class.getName();
private GoogleApiClient mGoogleApiClient;
private static final String WEARABLE_DATA_PATH = "/audio";
@Override
public void onCreate() {
// I can see this fires properly on the Android mobile phone
Logger.d(TAG, "onCreate");
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
// This never fires on the Android mobile phone, even though Wear says data was sent successfully
Logger.d(TAG, "on change");
}
}
在 Wear 设备上,我有一个创建 Google API 客户端的主要活动。我使用 UI 按钮从音频生成输入(代码未显示),我知道由于日志记录而可以正常工作。然后我尝试将这些数据从穿戴设备发送到手机。在日志中,我看到“结果可用。状态:Status{statusCode=SUCCESS, resolution=null}”(我使用结果回调来跟踪)。
public class MainActivity extends Activity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = MainActivity.class.getName();
private static final int SPEECH_REQUEST_CODE = 1;
private static final int RECORDER_SAMPLERATE = 44100;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_STEREO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private TextView mTextView;
private AudioRecord recorder;
private int bufferSize = 0;
private Thread recordingThread = null;
private GoogleApiClient mGoogleApiClient;
private volatile boolean isRecording;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "Creating MainActivity");
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);
}
});
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
// Connect to the data layer when the Activity starts
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onResume() {
if (null != mGoogleApiClient && !mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
super.onResume();
}
@Override
protected void onStop() {
if (null != mGoogleApiClient && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
super.onStop();
}
// Placeholders for required connection callbacks
@Override
public void onConnectionSuspended(int cause) {
Log.d(TAG, "Connection suspended");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "Connection failed");
}
@Override
public void onConnected(Bundle connectionHint) {
Log.d(TAG, "Connected successfully");
}
// This is invoked from the UI, via a helper method not shown. Logs show the method is invoked fine.
private void processRawAudioData() {
byte data[] = new byte[bufferSize];
int read = 0;
while(isRecording) {
read = recorder.read(data, 0, bufferSize);
if(AudioRecord.ERROR_INVALID_OPERATION != read) {
Log.d(TAG, "Successfully read " + data.length + " bytes of audio");
Log.d(TAG, "Initial ten bytes: " + data[0] + data[1] + data[2] + data[3]
+ data[4] + data[5] + data[6] + data[7] + data[8] + data[9] + data[10]);
Asset myAsset = Asset.createFromBytes(data);
PutDataRequest request = PutDataRequest.create("/audio");
// might need to change time each time for other end to see change.
request.putAsset("profileImage", myAsset);
PendingResult<DataApi.DataItemResult> result =
Wearable.DataApi.putDataItem(mGoogleApiClient, request);
result.setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
@Override
public void onResult(DataApi.DataItemResult dataItemResult) {
// LOGS SHOW STATUS "MainActivity﹕ result available. Status: Status{statusCode=SUCCESS, resolution=null}"
Log.d(TAG, "result available. Status: " + dataItemResult.getStatus());
}
});
}
}
}
}