gpt4 book ai didi

android - WearableListenerService - onPeerConnected 从未调用过

转载 作者:行者123 更新时间:2023-11-29 17:37:00 29 4
gpt4 key购买 nike

我创建了简单的 Phone<->Wear 应用程序。

Wear 应用有一个带有代码的 Activity:

public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks {

private TextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
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);
}
});

getGoogleApiClient(this).connect();
}


private GoogleApiClient getGoogleApiClient(Context context) {
return new GoogleApiClient.Builder(context)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.build();
}

@Override
public void onConnected(Bundle bundle) {

Toast.makeText(this, "onConnected", Toast.LENGTH_SHORT).show();
}

@Override
public void onConnectionSuspended(int i) {

Toast.makeText(this, "onConnectionSuspended", Toast.LENGTH_SHORT).show();
}}

Phone 应用有一个 WearableListenerService

public class ListenerOfConnection extends WearableListenerService  {

private static final String TAG = "DataLayerListenerServic";


protected GoogleApiClient googleApiClient;
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(ListenerOfConnection.this, "onCreate", Toast.LENGTH_SHORT).show();
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.build();

googleApiClient.connect();
}


@Override
public void onPeerConnected(Node peer) {
super.onPeerConnected(peer);
Toast.makeText(ListenerOfConnection.this, "onPeerConnected", Toast.LENGTH_SHORT).show();
LOGD(TAG, "onPeerConnected: " + peer);
}

@Override
public void onPeerDisconnected(Node peer) {
super.onPeerDisconnected(peer);
Toast.makeText(ListenerOfConnection.this, "onPeerDisconnected", Toast.LENGTH_SHORT).show();
LOGD(TAG, "onPeerDisconnected: " + peer);
}

public static void LOGD(final String tag, String message) {
if (Log.isLoggable(tag, Log.DEBUG)) {
Log.d(tag, message);
}
}}

问题是 onPeerConnectedonPeerDisconnected 从未被调用,如果我不调用,onCreate 也不会被调用发送任何消息。

如果我发送的消息很少 onCreate 被调用,但是 onPeerConnectedonPeerDisconnected 从不...

在向我提问之前:1.applicationId相同2. onConnected 在可穿戴 Activity 上调用

最佳答案

据我所知,onPeerConnectedonPeerDisconnected 事件仅在您的应用程序正在运行且 蓝牙 之间的连接时被调用 watch 和电话是制造的还是坏的。换句话说,您很少会看到这些事件,而且在您调试 table 上的两台设备时几乎不会看到这些事件。

如果您在应用首次启动并且设备之间的 API 级连接已初始化时尝试做一些工作,则需要使用 Wearable.NodeApi.getConnectedNodes 方法相反。

这些东西的文档不太清楚...

关于android - WearableListenerService - onPeerConnected 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30176601/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com