android佩戴消息给掌机

时间:2016-02-13 17:39:57

标签: android message wear-os receiver

我正在使用Android Wearable API向我的手持设备发送简单的字符串消息。消息发送成功实现,消息状态为SUCCES。

我创建了一个接收消息的服务,但它无法正常工作。每次我从可穿戴应用程序发送消息时,onConnected回调方法都在调用。很奇怪。我展示了代码:

服务:

public class MobileService extends WearableListenerService {
private String TAG = "TAG_SERVICE";
private GoogleApiClient mGoogleApiClient;

@Override
public void onCreate() {
    super.onCreate();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle connectionHint) {
                    Log.d(TAG, "onConnected: " + connectionHint);

                }
                @Override
                public void onConnectionSuspended(int cause) {
                    Log.d(TAG, "onConnectionSuspended: " + cause);
                }
            })
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult result) {
                    Log.d(TAG, "onConnectionFailed: " + result);
                }
            })
            .addApi(Wearable.API)
            .build();
    mGoogleApiClient.connect();
}

@Override
public void onMessageReceived(MessageEvent messageEvent) {
    Log.v(TAG, "Message received!");
    super.onMessageReceived(messageEvent);
}

}

接收服务的Logcat,当我发送消息:(非常奇怪)

02-13 18:25:50.652 21490-21490/hu.padar.app.todo D/TAG_SERVICE: onConnected: null

Manifest mobile:

 <service android:name="hu.padar.app.todo.MobileService">
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
        </intent-filter>
    </service>
</application>

<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

两个gradle文件中的applicationId相同。

我添加了一些额外的信息。这是我发送消息的地方:

            if (transcriptionNodeId != null) {
            Wearable.MessageApi.sendMessage(mGoogleApiClient, transcriptionNodeId,
                    MESSAGE_PATH, "my message".getBytes()).setResultCallback(
                    new ResultCallback() {
                        @Override
                        public void onResult(Result result) {
                            Log.d(TAG, result.getStatus().toString());
                        }
                    }
            );
        } else {
            Log.d(TAG, "Unable to send message!");
        }

这是我初始化GoogleApiClient对象的地方:

 mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
                    .addApi(Wearable.API)
                    .build();
            mGoogleApiClient.connect();

            updateNodeId();

updateNodeId()方法仅将正确的节点ID放入transcirptionNodeId。

在build.gradle中使用依赖关系:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.support:wearable:1.3.0'
compile 'com.google.android.gms:play-services-wearable:8.4.0'
}

build.gradle中的移动依赖项如下:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
wearApp project(':wear')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.google.android.gms:play-services:8.4.0'
}

0 个答案:

没有答案