我有一个可穿戴设备,我尝试连接到GoogleApiClient,但从不会调用callback(onConnected,onConnectionSuspended或onConnectionFailed)。 其他一切工作正常,DataLayerListenerService能够从手持设备接收消息,并在连接时调用onPeerConnected。 我试过了模拟器和三星Gear Live设备。 这是我正在尝试连接到GoogleApiClient的Activity中的代码。 public class WearReaderActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { public static final String CONTENT_EXTRA = "contentExtra"; private String LOG_TAG = WearReaderActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.reader); mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Wearable.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); } private GoogleApiClient mGoogleApiClient; @Override protected void onStart() { super.onStart(); Log.e("Connected?", String.valueOf(mGoogleApiClient.isConnected())); //new […]
所以我发现了一些关于GoogleApiClient的不太清楚的东西。 GoogleApiClient有一个名为onConnected的函数,在客户端连接时运行(当然) 我有我自己的函数称为: startLocationListening最终被调用在GoogleApiClient的onConnected函数。 所以我的startLocationListening函数无法运行没有GoogleApiClient连接。 代码和日志: @Override public void onConnected(Bundle bundle) { log("Google_Api_Client:connected."); initLocationRequest(); startLocationListening(); //Exception caught inside this function } … private void startLocationListening() { log("Starting_location_listening:now"); //Exception caught here below: LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); } 例外是: 03-30 12:23:28.947: E/AndroidRuntime(4936): java.lang.IllegalStateException: GoogleApiClient is not connected yet. 03-30 12:23:28.947: E/AndroidRuntime(4936): at com.google.android.gms.internal.jx.a(Unknown Source) 03-30 12:23:28.947: E/AndroidRuntime(4936): at […]