Android Wear将数据发送到Android手机,但手机似乎永远不会收到它

前端之家收集整理的这篇文章主要介绍了Android Wear将数据发送到Android手机,但手机似乎永远不会收到它前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
简短摘要:我正在尝试使用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手机(Android 4.4.4版本)上,我使用了一个侦听器服务,该服务通过AndroidManifest.xml绑定,并通过手机上的主要活动启动.从日志中,我可以确认在手机上成功创建了服务,但是没有收到任何数据(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按钮从音频生成输入(代码显示),我知道这是因为日志记录正常工作.然后我尝试将这些数据从磨损设备发送到手机.在日志中,我看到“结果可用.状态:状态{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,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());
                    }
                });
            }
        }
    }
}

解决方法

为了让WearableListenerService触发’onDataChanged’事件,主应用程序和可穿戴应用程序中的applicationId必须匹配(build.gradle文件).

另外,你不能发送静态数据,数据必须改变.这意味着PutDataMapRequest对象中的数据必须更改.

原文链接:https://www.f2er.com/android/309476.html

猜你在找的Android相关文章