Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 dataChangeService extends WearableListenerService {
- @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>
Add Comment
Please, Sign In to add comment