com.google.android.gms.wearable.CapabilityClient Java Examples

The following examples show how to use com.google.android.gms.wearable.CapabilityClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LoggingUtil.java    From APDE with GNU General Public License v2.0 6 votes vote down vote up
public static void initMessageBroadcaster(final Context context) {
	Wearable.getCapabilityClient(context).addListener(new CapabilityClient.OnCapabilityChangedListener() {
		@Override
		public void onCapabilityChanged(@NonNull CapabilityInfo capabilityInfo) {
			updateBestNode(context);
		}
	}, "apde_receive_logs");
	
	// Can't do this on the main thread
	(new Thread(new Runnable() {
		@Override
		public void run() {
			updateBestNode(context);
		}
	})).start();
}
 
Example #2
Source File: MainActivity.java    From wear-os-samples with Apache License 2.0 6 votes vote down vote up
@Override
public void onResume() {
    super.onResume();
    mDataItemGeneratorFuture =
            mGeneratorExecutor.scheduleWithFixedDelay(
                    new DataItemGenerator(), 1, 5, TimeUnit.SECONDS);

    mStartActivityBtn.setEnabled(true);
    mSendPhotoBtn.setEnabled(mCameraSupported);

    // Instantiates clients without member variables, as clients are inexpensive to create and
    // won't lose their listeners. (They are cached and shared between GoogleApi instances.)
    Wearable.getDataClient(this).addListener(this);
    Wearable.getMessageClient(this).addListener(this);
    Wearable.getCapabilityClient(this)
            .addListener(this, Uri.parse("wear://"), CapabilityClient.FILTER_REACHABLE);
}
 
Example #3
Source File: MainWearActivity.java    From wear-os-samples with Apache License 2.0 6 votes vote down vote up
private void checkIfPhoneHasApp() {
    Log.d(TAG, "checkIfPhoneHasApp()");

    Task<CapabilityInfo> capabilityInfoTask = Wearable.getCapabilityClient(this)
            .getCapability(CAPABILITY_PHONE_APP, CapabilityClient.FILTER_ALL);

    capabilityInfoTask.addOnCompleteListener(new OnCompleteListener<CapabilityInfo>() {
        @Override
        public void onComplete(Task<CapabilityInfo> task) {

            if (task.isSuccessful()) {
                Log.d(TAG, "Capability request succeeded.");
                CapabilityInfo capabilityInfo = task.getResult();
                mAndroidPhoneNodeWithApp = pickBestNodeId(capabilityInfo.getNodes());

            } else {
                Log.d(TAG, "Capability request failed to return any results.");
            }

            verifyNodeAndUpdateUI();
        }
    });
}
 
Example #4
Source File: MainMobileActivity.java    From wear-os-samples with Apache License 2.0 6 votes vote down vote up
private void findWearDevicesWithApp() {
    Log.d(TAG, "findWearDevicesWithApp()");

    Task<CapabilityInfo> capabilityInfoTask = Wearable.getCapabilityClient(this)
            .getCapability(CAPABILITY_WEAR_APP, CapabilityClient.FILTER_ALL);

    capabilityInfoTask.addOnCompleteListener(new OnCompleteListener<CapabilityInfo>() {
        @Override
        public void onComplete(Task<CapabilityInfo> task) {

            if (task.isSuccessful()) {
                Log.d(TAG, "Capability request succeeded.");

                CapabilityInfo capabilityInfo = task.getResult();
                mWearNodesWithApp = capabilityInfo.getNodes();

                Log.d(TAG, "Capable Nodes: " + mWearNodesWithApp);

                verifyNodeAndUpdateUI();

            } else {
                Log.d(TAG, "Capability request failed to return any results.");
            }
        }
    });
}
 
Example #5
Source File: WearDataLayerListenerService.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
public static void sendMessageToWatch(final String path, final byte[] data, String capability){
	Wearable.getCapabilityClient(ApplicationLoader.applicationContext)
			.getCapability(capability, CapabilityClient.FILTER_REACHABLE)
			.addOnCompleteListener(new OnCompleteListener<CapabilityInfo>(){
				@Override
				public void onComplete(@NonNull Task<CapabilityInfo> task){
					CapabilityInfo info=task.getResult();
					if(info!=null){
						MessageClient mc=Wearable.getMessageClient(ApplicationLoader.applicationContext);
						Set<Node> nodes=info.getNodes();
						for(Node node:nodes){
							mc.sendMessage(node.getId(), path, data);
						}
					}
				}
			});
}
 
Example #6
Source File: CompanionNotifyActivity.java    From PixelWatchFace with GNU General Public License v3.0 6 votes vote down vote up
private void checkIfPhoneHasApp() {
  Log.d(TAG, "checkIfPhoneHasApp()");

  Task<CapabilityInfo> capabilityInfoTask = Wearable.getCapabilityClient(this)
      .getCapability(CAPABILITY_PHONE_APP, CapabilityClient.FILTER_ALL);

  capabilityInfoTask.addOnCompleteListener(new OnCompleteListener<CapabilityInfo>() {
    @Override
    public void onComplete(Task<CapabilityInfo> task) {

      if (task.isSuccessful()) {
        Log.d(TAG, "Capability request succeeded.");
        CapabilityInfo capabilityInfo = task.getResult();
        mAndroidPhoneNodeWithApp = pickBestNodeId(capabilityInfo.getNodes());

      } else {
        Log.d(TAG, "Capability request failed to return any results.");
      }

      verifyNodeAndUpdateUI();
    }
  });
}
 
Example #7
Source File: WearDataLayerListenerService.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
public static void sendMessageToWatch(final String path, final byte[] data, String capability){
	Wearable.getCapabilityClient(ApplicationLoader.applicationContext)
			.getCapability(capability, CapabilityClient.FILTER_REACHABLE)
			.addOnCompleteListener(new OnCompleteListener<CapabilityInfo>(){
				@Override
				public void onComplete(@NonNull Task<CapabilityInfo> task){
					CapabilityInfo info=task.getResult();
					if(info!=null){
						MessageClient mc=Wearable.getMessageClient(ApplicationLoader.applicationContext);
						Set<Node> nodes=info.getNodes();
						for(Node node:nodes){
							mc.sendMessage(node.getId(), path, data);
						}
					}
				}
			});
}
 
Example #8
Source File: WearManager.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
/**
 * Wearとの接続状況を検知するリスナー.
 */
private void setCapabilityListener() {
    Wearable.getCapabilityClient(mContext).addListener((capabilityInfo) -> {
        for (Node node : capabilityInfo.getNodes()) {
            if (node.isNearby()) {
                mLogger.info("isNearby=true: name = " + node.getDisplayName()
                        + ", id = " + node.getId());
                mNodeCache.put(node.getId(), node);
                notifyOnNodeConnected(node);
            } else {
                mLogger.info("onPeerDisconnected: name = " + node.getDisplayName()
                        + ", id = " + node.getId());
                mNodeCache.remove(node.getId());
                notifyOnNodeDisconnected(node);
            }
        }
    }, Uri.parse("wear://"), CapabilityClient.FILTER_REACHABLE);
}
 
Example #9
Source File: LoggingUtil.java    From APDE with GNU General Public License v2.0 5 votes vote down vote up
private static void updateBestNode(Context context) {
	bestNodeId = null;
	
	try {
		CapabilityInfo info = Tasks.await(Wearable.getCapabilityClient(context).getCapability("apde_receive_logs", CapabilityClient.FILTER_REACHABLE));
		for (Node node : info.getNodes()) {
			if (node.isNearby()) {
				bestNodeId = node.getId();
			}
		}
	} catch (Exception e) {
		// Don't call printStackTrace() because that would make an infinite loop
		Log.e("apde", e.toString());
	}
}
 
Example #10
Source File: MainWearActivity.java    From wear-os-samples with Apache License 2.0 5 votes vote down vote up
@Override
protected void onResume() {
    Log.d(TAG, "onResume()");
    super.onResume();

    // Instantiates clients without member variables, as clients are inexpensive to create and
    // won't lose their listeners. (They are cached and shared between GoogleApi instances.)
    Wearable.getMessageClient(this).addListener(this);
    Wearable.getCapabilityClient(this).addListener(
            this, Constants.CAPABILITY_PHONE_APP);

    // Initial check of capabilities to find the phone.
    Task<CapabilityInfo> capabilityInfoTask = Wearable.getCapabilityClient(this)
            .getCapability(Constants.CAPABILITY_PHONE_APP, CapabilityClient.FILTER_REACHABLE);

    capabilityInfoTask.addOnCompleteListener(new OnCompleteListener<CapabilityInfo>() {
        @Override
        public void onComplete(Task<CapabilityInfo> task) {

            if (task.isSuccessful()) {
                Log.d(TAG, "Capability request succeeded.");

                CapabilityInfo capabilityInfo = task.getResult();
                mPhoneNodeId = pickBestNodeId(capabilityInfo.getNodes());

            } else {
                Log.d(TAG, "Capability request failed to return any results.");
            }
        }
    });

    // Enables app to handle 23+ (M+) style permissions.
    mWearBodySensorsPermissionApproved =
            ActivityCompat.checkSelfPermission(this, Manifest.permission.BODY_SENSORS)
                    == PackageManager.PERMISSION_GRANTED;
}
 
Example #11
Source File: WearableUtil.java    From APDE with GNU General Public License v2.0 5 votes vote down vote up
public static void checkWatchAvailable(final Context context, final ResultCallback callback) {
	(new Thread(new Runnable() {
		@Override
		public void run() {
			boolean successful = false;
			
			try {
				CapabilityInfo info = Tasks.await(Wearable.getCapabilityClient(context).getCapability("apde_run_watch_sketches", CapabilityClient.FILTER_REACHABLE));
				
				for (Node node : info.getNodes()) {
					if (node.isNearby()) {
						successful = true;
						break;
					}
				}
			} catch (ExecutionException | InterruptedException e) {
				e.printStackTrace();
			}
			
			if (successful) {
				callback.success();
			} else {
				callback.failure();
			}
		}
	})).start();
}
 
Example #12
Source File: WearDataLayerListenerService.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
public static void sendMessageToWatch(final String path, final byte[] data, String capability) {
	Wearable.getCapabilityClient(ApplicationLoader.applicationContext)
			.getCapability(capability, CapabilityClient.FILTER_REACHABLE)
			.addOnCompleteListener(task -> {
				CapabilityInfo info = task.getResult();
				if (info != null) {
					MessageClient mc = Wearable.getMessageClient(ApplicationLoader.applicationContext);
					Set<Node> nodes = info.getNodes();
					for (Node node : nodes) {
						mc.sendMessage(node.getId(), path, data);
					}
				}
			});
}
 
Example #13
Source File: WearProviderService.java    From cordova-androidwear with Apache License 2.0 5 votes vote down vote up
private void loadNodes() {
	Task<CapabilityInfo> capabilityTask = Wearable.getCapabilityClient(this)
			.getCapability(CORDOVA_CAPABILITY, CapabilityClient.FILTER_REACHABLE);
	capabilityTask.addOnSuccessListener(new OnSuccessListener<CapabilityInfo>() {
		@Override
		public void onSuccess(CapabilityInfo capabilityInfo) {
			WearProviderService.this.onCapabilityChanged(capabilityInfo);
		}
	});
}
 
Example #14
Source File: WearProviderService.java    From cordova-androidwear with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate() {
	super.onCreate();

	LOGD(TAG, "onCreate");
	Wearable.getMessageClient(this).addListener(this);
	Wearable.getCapabilityClient(this)
			.addListener(
					this, Uri.parse("wear://"), CapabilityClient.FILTER_REACHABLE);

	Thread mBackgroundThread = new BackgroundThread();
	mBackgroundThread.start();
}
 
Example #15
Source File: MainActivity.java    From wear-os-samples with Apache License 2.0 5 votes vote down vote up
/** Find the connected nodes that provide at least one of the given capabilities. */
private void showNodes(final String... capabilityNames) {

    Task<Map<String, CapabilityInfo>> capabilitiesTask =
            Wearable.getCapabilityClient(this)
                    .getAllCapabilities(CapabilityClient.FILTER_REACHABLE);

    capabilitiesTask.addOnSuccessListener(
            new OnSuccessListener<Map<String, CapabilityInfo>>() {
                @Override
                public void onSuccess(Map<String, CapabilityInfo> capabilityInfoMap) {
                    Set<Node> nodes = new HashSet<>();

                    if (capabilityInfoMap.isEmpty()) {
                        showDiscoveredNodes(nodes);
                        return;
                    }
                    for (String capabilityName : capabilityNames) {
                        CapabilityInfo capabilityInfo = capabilityInfoMap.get(capabilityName);
                        if (capabilityInfo != null) {
                            nodes.addAll(capabilityInfo.getNodes());
                        }
                    }
                    showDiscoveredNodes(nodes);
                }
            });
}
 
Example #16
Source File: MainActivity.java    From wear-os-samples with Apache License 2.0 5 votes vote down vote up
@Override
protected void onResume() {
    super.onResume();

    // Instantiates clients without member variables, as clients are inexpensive to create and
    // won't lose their listeners. (They are cached and shared between GoogleApi instances.)
    Wearable.getDataClient(this).addListener(this);
    Wearable.getMessageClient(this).addListener(this);
    Wearable.getCapabilityClient(this)
            .addListener(this, Uri.parse("wear://"), CapabilityClient.FILTER_REACHABLE);
}
 
Example #17
Source File: MainPhoneActivity.java    From wear-os-samples with Apache License 2.0 4 votes vote down vote up
@Override
protected void onResume() {
    Log.d(TAG, "onResume()");
    super.onResume();

    /* Enables app to handle 23+ (M+) style permissions. It also covers user changing
     * permission in settings and coming back to the app.
     */
    mPhoneStoragePermissionApproved =
            ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_GRANTED;

    if (mPhoneStoragePermissionApproved) {
        mPhoneStoragePermissionButton.setCompoundDrawablesWithIntrinsicBounds(
                R.drawable.ic_permission_approved, 0, 0, 0);
    }

    // Clients are inexpensive to create, so in this case we aren't creating member variables.
    // (They are cached and shared between GoogleApi instances.)
    Wearable.getMessageClient(this).addListener(this);
    Wearable.getCapabilityClient(this).addListener(
            this, Constants.CAPABILITY_WEAR_APP);

    // Initial check of capabilities to find the phone.
    Task<CapabilityInfo> capabilityInfoTask = Wearable.getCapabilityClient(this)
            .getCapability(Constants.CAPABILITY_WEAR_APP, CapabilityClient.FILTER_REACHABLE);

    capabilityInfoTask.addOnCompleteListener(new OnCompleteListener<CapabilityInfo>() {
        @Override
        public void onComplete(Task<CapabilityInfo> task) {

            if (task.isSuccessful()) {
                Log.d(TAG, "Capability request succeeded.");
                CapabilityInfo capabilityInfo = task.getResult();
                String capabilityName = capabilityInfo.getName();

                boolean wearSupportsSampleApp =
                        capabilityName.equals(Constants.CAPABILITY_WEAR_APP);

                if (wearSupportsSampleApp) {
                    mWearNodeIds = capabilityInfo.getNodes();

                    /*
                     * Upon getting all wear nodes, we now need to check if the original
                     * request to launch this activity (and PhonePermissionRequestActivity) was
                     * initiated by a wear device. If it was, we need to send back the
                     * permission results (data or rejection of permission) to the wear device.
                     *
                     * Also, note we set variable to false, this enables the user to continue
                     * changing permissions without sending updates to the wear every time.
                     */
                    if (mWearRequestingPhoneStoragePermission) {
                        mWearRequestingPhoneStoragePermission = false;
                        sendWearPermissionResults();
                    }
                }

            } else {
                Log.d(TAG, "Capability request failed to return any results.");
            }
        }
    });
}