Guest User

Untitled

a guest
Dec 18th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
  2.  
  3. //Global variables.
  4. private static final int importFileCode = 1;
  5.  
  6. //Used for Wear DataLayer
  7. private static final String TAG = "MainActivity";
  8. private GoogleApiClient mGoogleApiClient;
  9. private boolean mResolvingError = false;
  10.  
  11. //Method001: Creates the interfaces, calls necessary methods.
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15.  
  16. setContentView(R.layout.activity_main);
  17. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  18.  
  19. //Used to initiate GoogleAPiClient
  20. initiateAPI();
  21.  
  22. //Used to test the data layer
  23. sendAccsRequest("Testing");
  24. }
  25.  
  26. @Override
  27. public void onConnected(Bundle connectionHint) {
  28. Log.d(TAG, "onConnected: " + connectionHint);
  29. }
  30.  
  31. @Override
  32. public void onConnectionSuspended(int i) {
  33. Log.d(TAG, "onConnectionSuspended: " + i);
  34. }
  35.  
  36. @Override
  37. public void onConnectionFailed(ConnectionResult connectionResult) {
  38. Log.d(TAG, "onConnectionFailed: " + connectionResult);
  39. }
  40.  
  41. //Initiates the google api client.
  42. public void initiateAPI () {
  43. //GoogleApiClient used for connection w/the phone
  44. mGoogleApiClient = new GoogleApiClient.Builder(this)
  45. .addApi(Wearable.API)
  46. .addConnectionCallbacks(this)
  47. .addOnConnectionFailedListener(this)
  48. .build();
  49. mGoogleApiClient.connect();
  50. }
  51.  
  52. //Used to send the string with info to wear, hopefully.
  53. public void sendAccsRequest(String allInfo) {
  54. //Creates a request with a unique key
  55. PutDataMapRequest putDataMapRequest = PutDataMapRequest.create("/allInfo");
  56.  
  57. //Inserts the variables using the keys
  58. putDataMapRequest.getDataMap().putString("strAllInfo", allInfo);
  59.  
  60. PutDataRequest request = putDataMapRequest.asPutDataRequest();
  61. Wearable.DataApi.putDataItem(mGoogleApiClient, request)
  62. .setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
  63. @Override
  64. public void onResult(DataApi.DataItemResult dataItemResult) {
  65. if (!dataItemResult.getStatus().isSuccess()) {
  66. Log.e("sendAccs", "Failed to send accs");
  67. } else {
  68. Log.d("sendAccs", "Successfully sent accs");
  69. }
  70. }
  71. });
  72. }
  73.  
  74. }
  75.  
  76. public class dataChangeService extends WearableListenerService {
  77. @Override
  78. public void onDataChanged(DataEventBuffer dataEvents) {
  79. Log.d("dataChangeService", "Successfully running");
  80. for (DataEvent dataEvent : dataEvents) {
  81. if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
  82. DataMap dataMap = DataMapItem.fromDataItem(dataEvent.getDataItem()).getDataMap();
  83. String path = dataEvent.getDataItem().getUri().getPath();
  84. if (path.equals("/allInfo")) {
  85. String allInfo = dataMap.getString("strAllInfo");
  86. Toast.makeText(dataChangeService.this, "AllInfo: " + allInfo, Toast.LENGTH_SHORT).show();
  87. }
  88. }
  89. }
  90. }
  91. }
  92.  
  93. <?xml version="1.0" encoding="utf-8"?>
  94. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  95. package="noobyguy.testapp">
  96.  
  97. <uses-feature android:name="android.hardware.type.watch" />
  98.  
  99. <uses-permission android:name="android.permission.WAKE_LOCK" />
  100.  
  101. <application
  102. android:allowBackup="true"
  103. android:icon="@mipmap/ic_launcher"
  104. android:label="@string/app_name"
  105. android:supportsRtl="true"
  106. android:theme="@android:style/Theme.DeviceDefault">
  107. <uses-library
  108. android:name="com.google.android.wearable"
  109. android:required="true" />
  110. <!--
  111. Set to true if your app is Standalone, that is, it does not require the handheld
  112. app to run.
  113. -->
  114. <meta-data
  115. android:name="com.google.android.wearable.standalone"
  116. android:value="true" />
  117.  
  118. <activity
  119. android:name=".Main"
  120. android:label="@string/app_name">
  121. <intent-filter>
  122. <action android:name="android.intent.action.MAIN" />
  123.  
  124. <category android:name="android.intent.category.LAUNCHER" />
  125. </intent-filter>
  126. </activity>
  127. <meta-data android:name="com.google.android.gms.version"
  128. android:value="@integer/google_play_services_version" />
  129.  
  130. <service
  131. android:name=".dataChangeService"
  132. android:enabled="true"
  133. android:exported="true">
  134. <intent-filter>
  135. <action android:name = "come.google.android.gms.wearable.BIND_LISTENER"/>
  136. </intent-filter>
  137. </service>
  138. </application>
  139.  
  140. </manifest>
Add Comment
Please, Sign In to add comment