未收到电话佩戴消息(通过消息api)

时间:2016-10-10 10:25:05

标签: android wear-os

我一直在尝试通过消息API从手机发送消息到我的androidWear。它到目前为止还没有工作,我已经不知道为什么了。

我手机上有一个简单的应用程序发送消息,这是代码:

public class MainActivity extends Activity 
implements GoogleApiClient.ConnectionCallbacks {

private static final String START_ACTIVITY = "/start_activity";
private static final String WEAR_MESSAGE_PATH = "/message";

private GoogleApiClient mApiClient;

private ArrayAdapter<String> mAdapter;

private ListView mListView;
private EditText mEditText;
private Button mSendButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    init();
    initGoogleApiClient();
}

private void initGoogleApiClient() {
    mApiClient = new GoogleApiClient.Builder( this )
            .addConnectionCallbacks(this)
            .addApi( Wearable.API )
            .build();

    mApiClient.connect();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mApiClient.disconnect();
}

private void init() {
    mListView = (ListView) findViewById( R.id.list_view );
    mEditText = (EditText) findViewById( R.id.input );
    mSendButton = (Button) findViewById( R.id.btn_send );

    mAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1 );
    mListView.setAdapter( mAdapter );

    mSendButton.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String text = mEditText.getText().toString();
            if (!TextUtils.isEmpty(text)) {
                mAdapter.add(text);
                mAdapter.notifyDataSetChanged();

                sendMessage(WEAR_MESSAGE_PATH, text);
            }
        }
    });
}

private void sendMessage( final String path, final String text ) {
    new Thread( new Runnable() {
        @Override
        public void run() {
            PutDataMapRequest pdmr = PutDataMapRequest.create(WEAR_MESSAGE_PATH);
            pdmr.getDataMap().putString("message", text);
            PutDataRequest dataRequest = pdmr.asPutDataRequest();
            dataRequest.setUrgent();
            PendingResult<DataApi.DataItemResult> result = Wearable.DataApi.putDataItem(mApiClient, dataRequest);

            result.setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
                @Override
                public void onResult(@NonNull DataApi.DataItemResult dataItemResult) {
                    if(dataItemResult.getStatus().isSuccess()){
                        Log.v("MESSAGE APP", "MESSAGE SENT");
                    }
                }
            });

            runOnUiThread( new Runnable() {
                @Override
                public void run() {
                    mEditText.setText( "" );
                }
            });
        }
    }).start();
}

@Override
public void onConnected(Bundle bundle) {
    sendMessage(START_ACTIVITY, "");
}

@Override
public void onConnectionSuspended(int i) {

}
}

当我点击发送按钮时,它会发送消息,当我调试它时,我可以看到它找到了我的服装(节点)并发送消息(回调返回成功)。

但是这件衣服什么都没收到: 活动代码:

public class MainActivity extends Activity 
implements MessageApi.MessageListener, GoogleApiClient.ConnectionCallbacks {

private static final String WEAR_MESSAGE_PATH = "/message";
private GoogleApiClient mApiClient;
private ArrayAdapter<String> mAdapter;

private ListView mListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mListView = (ListView) findViewById(R.id.list);

    mAdapter = new ArrayAdapter<String>( this, R.layout.list_item );
    mListView.setAdapter( mAdapter );

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    initGoogleApiClient();
}

private void initGoogleApiClient() {
    mApiClient = new GoogleApiClient.Builder( this )
            .addApi( Wearable.API )
            .addConnectionCallbacks( this )
            .build();

    if( mApiClient != null && !( mApiClient.isConnected() || mApiClient.isConnecting() ) )
        mApiClient.connect();
}

@Override
protected void onResume() {
    super.onResume();
    if( mApiClient != null && !( mApiClient.isConnected() || mApiClient.isConnecting() ) )
        mApiClient.connect();
}

@Override
protected void onStart() {
    super.onStart();
}

@Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
    Log.v("WEAR MESSAGE", "Data changed");

    for (DataEvent event : dataEventBuffer) {
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            // DataItem changed
            DataItem item = event.getDataItem();
            if (item.getUri().getPath().compareTo(WEAR_MESSAGE_PATH) == 0) {
                DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
                final String msg = dataMap.getString("message");
                runOnUiThread( new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.add(msg);
                        mAdapter.notifyDataSetChanged();
                    }
                });
            }
        } else if (event.getType() == DataEvent.TYPE_DELETED) {
            // DataItem deleted
        }
    }
}

@Override
public void onConnected(Bundle bundle) {
    Wearable.DataApi.addListener( mApiClient, this );
    startService(new Intent(this, WearMessageListenerService.class));
}

@Override
protected void onStop() {
    if ( mApiClient != null ) {
        Wearable.DataApi.removeListener( mApiClient, this );
        if ( mApiClient.isConnected() ) {
            mApiClient.disconnect();
        }
    }
    super.onStop();
}

@Override
protected void onDestroy() {
    if( mApiClient != null )
        mApiClient.unregisterConnectionCallbacks( this );
    super.onDestroy();
}

@Override
public void onConnectionSuspended(int i) {
    Log.d("MESSAGE APP", "Connection suspended");
}
}

清单(我想问题来自这里):     

<uses-feature android:name="android.hardware.type.watch" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@android:style/Theme.DeviceDefault">
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter>
            <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
            <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
            <data android:scheme="wear" android:host="*"
                android:pathPattern=".*" />
        </intent-filter>

    </activity>
</application>

</manifest>

我尝试过使用path =“/ message”,它也不起作用。

任何想法如何使这项工作

更新: 在看到使用消息API的成功响应并不意味着消息实际到达磨损后,我改变了每次使用DataApi。 (我相应地更改了上面的代码。)

穿着还没什么....但是我尝试了一些东西,我在手机活动上放了一个DataApi监听器,它看到了变化(如果这有助于弄清楚什么是错的)。所以电话活动看到的是DataCHange而不是磨损...... 这让我觉得我实际上没有正确设置我的硬件。

2 个答案:

答案 0 :(得分:6)

找到它:两个应用程序(佩戴和电话)必须具有相同的ApplicationId(packageName)。 谢谢你的帮助。

答案 1 :(得分:0)

我认为这里存在设计问题。您正在尝试启动活动以响应数据层事件,然后在此活动中响应此事件。这并不是数据API的设计方式。

你有几个选择。

  1. 您的活动已在运行,可通过Wearable.DataApi.addListenerWearable.MessageApi.addListener等接收和处理数据层事件。
  2. 您尚未运行活动,并且您通过自己的WearableListenerService收到数据事件或消息,并从此服务开始新的活动。只要存在有趣的“可穿戴”事件(例如数据,消息或节点更改),系统就会为您启动此服务。您必须记住包含适当的清单条目以使您的服务唤醒以响应每种类型的事件 - 请查看我上面链接的文档中的“示例清单条目”部分,以便更加清晰。
  3. 请参阅WearableListenerService文档。基本上,您应该覆盖onDataChanged(DataEventBuffer)onMessageReceived(MessageEvent),具体取决于您是要处理数据事件还是消息事件,然后从您的服务开始您的活动(即使用Intent.FLAG_ACTIVITY_NEW_TASK)< / p>

    希望这有帮助