마모를 사용하여 프로젝트를 시작한 첫 번째 단계는 DataLayer를 사용하여 Android에서 Wear로 한 문자열을 전송하는 것입니다. 이것은 실제로 훨씬 더 큰 프로젝트를위한 테스트입니다. 필요한 모든 것은 동기화 된 문자열을 갖는 것입니다.Connection이 null을 표시하고 DataLayer에서 수신하지 않습니다.
프로젝트를 실행하면 Log는 항상 문자열이 성공적으로 전송되었음을 나타내며 "onConnected"는 항상 null을 표시합니다. 말할 필요도없이 시계가 연결됩니다. 나는 연결이 문제라고 생각합니다. 왜 아무 일도 일어나지 않거나 마모가되는 이유를 설명합니다. 디버깅하려면 : USB를 통해 연결된 Moto Z Play와 블루투스 디버깅을 통해 Lg G Watch를 사용하고 있습니다. 착용시 설치에 문제가 없습니다. 그러나 Connection은 항상 null을 표시합니다.
변경을 감지하면 Toast를 통해 문자열 ("Testing")을 표시하는 onDataChanged 서비스가 있습니다. 이것은 결코 일어나지 않는다. 위에서 언급 한 바와 같이,이 연결 문제가 있다고 가정하지만 부적절한 사용으로 인해 발생할 수도 있습니다. 나는 사과한다. 나는 스스로 가르치는 고등학생이다. 나는 어떤 도움이나 명료화에 감사드립니다.
전화 활동 :
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
//Global variables.
private static final int importFileCode = 1;
//Used for Wear DataLayer
private static final String TAG = "MainActivity";
private GoogleApiClient mGoogleApiClient;
private boolean mResolvingError = false;
//Method001: Creates the interfaces, calls necessary methods.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//Used to initiate GoogleAPiClient
initiateAPI();
//Used to test the data layer
sendAccsRequest("Testing");
}
@Override
public void onConnected(Bundle connectionHint) {
Log.d(TAG, "onConnected: " + connectionHint);
}
@Override
public void onConnectionSuspended(int i) {
Log.d(TAG, "onConnectionSuspended: " + i);
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed: " + connectionResult);
}
//Initiates the google api client.
public void initiateAPI() {
//GoogleApiClient used for connection w/the phone
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
//Used to send the string with info to wear, hopefully.
public void sendAccsRequest(String allInfo) {
//Creates a request with a unique key
PutDataMapRequest putDataMapRequest = PutDataMapRequest.create("/allInfo");
//Inserts the variables using the keys
putDataMapRequest.getDataMap().putString("strAllInfo", allInfo);
PutDataRequest request = putDataMapRequest.asPutDataRequest();
Wearable.DataApi.putDataItem(mGoogleApiClient, request)
.setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
@Override
public void onResult(DataApi.DataItemResult dataItemResult) {
if (!dataItemResult.getStatus().isSuccess()) {
Log.e("sendAccs", "Failed to send accs");
} else {
Log.d("sendAccs", "Successfully sent accs");
}
}
});
}
}
착용 활동 :
public class Main extends WearableActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
DataApi.DataListener, MessageApi.MessageListener, CapabilityApi.CapabilityListener {
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
//Initiates the google api client
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
// Enables Always-on
setAmbientEnabled();
}
@Override
public void onConnected(Bundle connectionHint) {
Log.d("onConnected(): ","Successfully connected to Google API client");
Wearable.DataApi.addListener(mGoogleApiClient, this);
Wearable.MessageApi.addListener(mGoogleApiClient, this);
Wearable.CapabilityApi.addListener(
mGoogleApiClient, this, Uri.parse("wear://"), CapabilityApi.FILTER_REACHABLE);
}
@Override
public void onConnectionSuspended(int i) {
Log.e("onConnectionSuspended()",": Connection to Google API client was suspended");
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e("onConnectionFailed(): ","Failed to connect, with result: " + connectionResult);
}
@Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
for (DataEvent event : dataEventBuffer) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
String path = event.getDataItem().getUri().getPath();
if (dataChangeService.accPath.equals(path)) {
DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
String allInfo = dataMapItem.getDataMap()
.getString("strAllInfo");
//Prints out received string
Log.e("strAllInfo: ", allInfo);
}
} else {
Log.e("Unknown data event type", "Type = " + event.getType());
}
}
}
@Override
public void onMessageReceived(MessageEvent messageEvent) {
}
@Override
public void onCapabilityChanged(CapabilityInfo capabilityInfo) {
}
}
dataChangeService (착용) 착용에 대한
public class dataChangeService extends WearableListenerService {
GoogleApiClient mGoogleApiClient;
public static final String accPath = "/allInfo";
@Override
public void onCreate() {
super.onCreate();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
Log.d("dataChangeService", "Successfully running");
for (DataEvent dataEvent : dataEvents) {
if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
DataMap dataMap = DataMapItem.fromDataItem(dataEvent.getDataItem()).getDataMap();
String path = dataEvent.getDataItem().getUri().getPath();
if (path.equals("/allInfo")) {
String allInfo = dataMap.getString("strAllInfo");
Toast.makeText(dataChangeService.this, "AllInfo: " + allInfo, Toast.LENGTH_SHORT).show();
}
}
}
}
}
안드로이드 매니페스트 :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="noobyguy.testapp">
<uses-feature android:name="android.hardware.type.watch" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.DeviceDefault">
<uses-library
android:name="com.google.android.wearable"
android:required="true" />
<!--
Set to true if your app is Standalone, that is, it does not require the handheld
app to run.
-->
<meta-data
android:name="com.google.android.wearable.standalone"
android:value="true" />
<activity
android:name=".Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<service
android:name=".dataChangeService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name = "come.google.android.gms.wearable.BIND_LISTENER"/>
</intent-filter>
</service>
</application>
</manifest>