Skip to content

Commit

Permalink
adding checkApiAvailability for Wearable API in case Wear App is not …
Browse files Browse the repository at this point in the history
…installed (#29)
  • Loading branch information
fabOnReact authored Aug 4, 2024
1 parent ded9bb3 commit 862875e
Show file tree
Hide file tree
Showing 4 changed files with 285 additions and 5 deletions.
39 changes: 39 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,42 @@ yarn android
[21]: https://developer.android.com/training/wearables/get-started/connect-phone
[22]: https://gist.github.com/assets/24992535/f6cb9f84-dc50-492b-963d-6d9e9396f451 'wear os large round'
[23]: https://reactnative.dev/docs/debugging

### Sending a pull request

> **Working on your first pull request?** You can learn how from this _free_ series: [How to Contribute to an Open Source Project on GitHub](https://app.egghead.io/playlists/how-to-contribute-to-an-open-source-project-on-github).
When you're sending a pull request:

- Prefer small pull requests focused on one change.
- Verify that linters and tests are passing.
- Review the documentation to make sure it looks good.
- Follow the pull request template when opening a pull request.
- For pull requests that change the API or implementation, discuss with maintainers first by opening an issue.

### Linting and tests

[ESLint](https://eslint.org/), [Prettier](https://prettier.io/), [TypeScript](https://www.typescriptlang.org/)

We use [TypeScript](https://www.typescriptlang.org/) for type checking, [ESLint](https://eslint.org/) with [Prettier](https://prettier.io/) for linting and formatting the code, and [Jest](https://jestjs.io/) for testing.

Our pre-commit hooks verify that the linter and tests pass when committing.

### Publishing to npm

We use [release-it](https://github.com/release-it/release-it) to make it easier to publish new versions. It handles common tasks like bumping version based on semver, creating tags and releases etc.

To publish new versions, run the following:

```sh
yarn release
```

### Scripts

The `package.json` file contains various scripts for common tasks:

- `yarn`: setup project by installing dependencies.
- `yarn typecheck`: type-check files with TypeScript.
- `yarn lint`: lint files with ESLint.
- `yarn test`: run unit tests with Jest.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ const unsubscribe = watchEvents.on('message', (message) => {

## FAQ

While some error messages are displayed on the metro server for the mobile or wearOS device (port 8082), other warnings are only available through logcat.
To display them you need to open the android logcat tool from within Android Studio, where you can select the emulator and filter the messages by package name (more info in this [screenshot][41]).

[41] https://github.com/user-attachments/assets/87016f71-782d-4f28-88dc-2c5d013eae2f

#### Wearable App not installed on Mobile Device

The following error display if the mobile device did not install the Wearable App, which is used to pair mobile device with wearOS device.

```
WearConnectivityModule failed to retrieve nodes with error:
java.util.concurrent.ExecutionException: com.google.android.gms.common.api.ApiException: 17: API: Wearable.API is not available on this device.
Expand All @@ -130,6 +139,23 @@ Connection failed with: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=

The Android Phone did not install the Wear OS app and did not pair with Wear OS device. Follow this [instructions][21].

#### wearOS device too far for bluetooth connection

Logcat (wearOS) includes the following warning when sending messages to the mobile device.
There is no message in the Metro Server.

```
Pixel_8_Pro_API_35Device is too far for bluetooth connection.
```

#### The mobile or wearOS device is not paired with any bluetooth device

Metro includes a message that no connected nodes are found message that no connected nodes are found. A node is a bluetooth device connected with another wearOS or Mobile device.

```logcat
No connected nodes found. client: com.google.android.gms.wearable.internal.zzgo@cc11cd connectedNodes: []
```

## Contributing

See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
Expand All @@ -25,11 +26,12 @@
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.android.gms.common.GoogleApiAvailability;

public class WearConnectivityModule extends WearConnectivitySpec
implements MessageClient.OnMessageReceivedListener, LifecycleEventListener {
public static final String NAME = "WearConnectivity";
private static final String TAG = "WearConnectivityModule ";
private static final String TAG = "react-native-wear-connectivity ";
private final MessageClient client;
private String CLIENT_ADDED =
TAG + "onMessageReceived listener added when activity is created. Client receives messages.";
Expand All @@ -40,6 +42,9 @@ public class WearConnectivityModule extends WearConnectivitySpec
private String ADD_CLIENT =
TAG + "onMessageReceived listener added when activity is resumed. Client receives messages.";
private String RETRIEVE_NODES_FAILED = TAG + "failed to retrieve nodes with error: ";
private String CONNECTED_DEVICE_IS_FAR = "Device is too far for bluetooth connection. ";
private String INSTALL_GOOGLE_PLAY_WEARABLE = "The Android mobile phone needs to install the Google Play Wear app. ";
private String MISSING_GOOGLE_PLAY_SERVICES = "GooglePlay Services not available.";

WearConnectivityModule(ReactApplicationContext context) {
super(context);
Expand All @@ -57,9 +62,19 @@ public String getName() {

private List<Node> retrieveNodes(Callback errorCb) {
try {
int result = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(getReactApplicationContext());
ConnectionResult connectionResult = new ConnectionResult(result);
if (!connectionResult.isSuccess()) {
errorCb.invoke( MISSING_GOOGLE_PLAY_SERVICES + connectionResult.getErrorMessage());
return null;
}
NodeClient nodeClient = Wearable.getNodeClient(getReactApplicationContext());
// TODO: implement Runnable to run task in the background thread
// https://stackoverflow.com/a/64969640/7295772
try {
Tasks.await(GoogleApiAvailability.getInstance().checkApiAvailability(nodeClient));
} catch (Exception e) {
errorCb.invoke(INSTALL_GOOGLE_PLAY_WEARABLE + e);
return null;
}
return Tasks.await(nodeClient.getConnectedNodes());
} catch (Exception e) {
errorCb.invoke(RETRIEVE_NODES_FAILED + e);
Expand All @@ -74,10 +89,15 @@ public void sendMessage(ReadableMap messageData, Callback replyCb, Callback erro
for (Node connectedNode : connectedNodes) {
if (connectedNode.isNearby()) {
sendMessageToClient(messageData, connectedNode, replyCb, errorCb);
} else {
FLog.w(
TAG,
TAG
+ "connectedNode: "
+ connectedNode.getDisplayName()
+ CONNECTED_DEVICE_IS_FAR);
}
}
} else {
FLog.w(TAG, NO_NODES_FOUND + " client: " + client + " connectedNodes: " + connectedNodes);
}
}

Expand Down
Loading

0 comments on commit 862875e

Please sign in to comment.