Skip to content

Commit 862875e

Browse files
authored
adding checkApiAvailability for Wearable API in case Wear App is not installed (#29)
1 parent ded9bb3 commit 862875e

File tree

4 files changed

+285
-5
lines changed

4 files changed

+285
-5
lines changed

CONTRIBUTING.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,42 @@ yarn android
5454
[21]: https://developer.android.com/training/wearables/get-started/connect-phone
5555
[22]: https://gist.github.com/assets/24992535/f6cb9f84-dc50-492b-963d-6d9e9396f451 'wear os large round'
5656
[23]: https://reactnative.dev/docs/debugging
57+
58+
### Sending a pull request
59+
60+
> **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).
61+
62+
When you're sending a pull request:
63+
64+
- Prefer small pull requests focused on one change.
65+
- Verify that linters and tests are passing.
66+
- Review the documentation to make sure it looks good.
67+
- Follow the pull request template when opening a pull request.
68+
- For pull requests that change the API or implementation, discuss with maintainers first by opening an issue.
69+
70+
### Linting and tests
71+
72+
[ESLint](https://eslint.org/), [Prettier](https://prettier.io/), [TypeScript](https://www.typescriptlang.org/)
73+
74+
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.
75+
76+
Our pre-commit hooks verify that the linter and tests pass when committing.
77+
78+
### Publishing to npm
79+
80+
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.
81+
82+
To publish new versions, run the following:
83+
84+
```sh
85+
yarn release
86+
```
87+
88+
### Scripts
89+
90+
The `package.json` file contains various scripts for common tasks:
91+
92+
- `yarn`: setup project by installing dependencies.
93+
- `yarn typecheck`: type-check files with TypeScript.
94+
- `yarn lint`: lint files with ESLint.
95+
- `yarn test`: run unit tests with Jest.

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ const unsubscribe = watchEvents.on('message', (message) => {
122122

123123
## FAQ
124124

125+
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.
126+
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]).
127+
128+
[41] https://github.com/user-attachments/assets/87016f71-782d-4f28-88dc-2c5d013eae2f
129+
130+
#### Wearable App not installed on Mobile Device
131+
132+
The following error display if the mobile device did not install the Wearable App, which is used to pair mobile device with wearOS device.
133+
125134
```
126135
WearConnectivityModule failed to retrieve nodes with error:
127136
java.util.concurrent.ExecutionException: com.google.android.gms.common.api.ApiException: 17: API: Wearable.API is not available on this device.
@@ -130,6 +139,23 @@ Connection failed with: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=
130139

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

142+
#### wearOS device too far for bluetooth connection
143+
144+
Logcat (wearOS) includes the following warning when sending messages to the mobile device.
145+
There is no message in the Metro Server.
146+
147+
```
148+
Pixel_8_Pro_API_35Device is too far for bluetooth connection.
149+
```
150+
151+
#### The mobile or wearOS device is not paired with any bluetooth device
152+
153+
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.
154+
155+
```logcat
156+
No connected nodes found. client: com.google.android.gms.wearable.internal.zzgo@cc11cd connectedNodes: []
157+
```
158+
133159
## Contributing
134160

135161
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.

android/src/main/java/com/wearconnectivity/WearConnectivityModule.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.facebook.react.bridge.ReadableMap;
1414
import com.facebook.react.bridge.WritableMap;
1515
import com.facebook.react.modules.core.DeviceEventManagerModule;
16+
import com.google.android.gms.common.ConnectionResult;
1617
import com.google.android.gms.tasks.OnFailureListener;
1718
import com.google.android.gms.tasks.OnSuccessListener;
1819
import com.google.android.gms.tasks.Task;
@@ -25,11 +26,12 @@
2526
import java.util.List;
2627
import org.json.JSONException;
2728
import org.json.JSONObject;
29+
import com.google.android.gms.common.GoogleApiAvailability;
2830

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

4449
WearConnectivityModule(ReactApplicationContext context) {
4550
super(context);
@@ -57,9 +62,19 @@ public String getName() {
5762

5863
private List<Node> retrieveNodes(Callback errorCb) {
5964
try {
65+
int result = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(getReactApplicationContext());
66+
ConnectionResult connectionResult = new ConnectionResult(result);
67+
if (!connectionResult.isSuccess()) {
68+
errorCb.invoke( MISSING_GOOGLE_PLAY_SERVICES + connectionResult.getErrorMessage());
69+
return null;
70+
}
6071
NodeClient nodeClient = Wearable.getNodeClient(getReactApplicationContext());
61-
// TODO: implement Runnable to run task in the background thread
62-
// https://stackoverflow.com/a/64969640/7295772
72+
try {
73+
Tasks.await(GoogleApiAvailability.getInstance().checkApiAvailability(nodeClient));
74+
} catch (Exception e) {
75+
errorCb.invoke(INSTALL_GOOGLE_PLAY_WEARABLE + e);
76+
return null;
77+
}
6378
return Tasks.await(nodeClient.getConnectedNodes());
6479
} catch (Exception e) {
6580
errorCb.invoke(RETRIEVE_NODES_FAILED + e);
@@ -74,10 +89,15 @@ public void sendMessage(ReadableMap messageData, Callback replyCb, Callback erro
7489
for (Node connectedNode : connectedNodes) {
7590
if (connectedNode.isNearby()) {
7691
sendMessageToClient(messageData, connectedNode, replyCb, errorCb);
92+
} else {
93+
FLog.w(
94+
TAG,
95+
TAG
96+
+ "connectedNode: "
97+
+ connectedNode.getDisplayName()
98+
+ CONNECTED_DEVICE_IS_FAR);
7799
}
78100
}
79-
} else {
80-
FLog.w(TAG, NO_NODES_FOUND + " client: " + client + " connectedNodes: " + connectedNodes);
81101
}
82102
}
83103

0 commit comments

Comments
 (0)