-
Notifications
You must be signed in to change notification settings - Fork 9
feat: add apple watch connector #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fabOnReact
wants to merge
1
commit into
main
Choose a base branch
from
codex/add-react-native-watch-connectivity-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Apple Watch Setup | ||
|
||
This library integrates with [`react-native-watch-connectivity`](https://github.com/mtford90/react-native-watch-connectivity) | ||
to enable communication between an iOS app and its paired Apple Watch. | ||
|
||
## Xcode configuration | ||
|
||
1. Open the iOS project in Xcode. | ||
2. Select **File > New > Target…** and add a **Watch App for iOS App**. | ||
3. In **Signing & Capabilities** for both the iOS app and the WatchKit extension: | ||
- Add **App Groups** and use the same identifier (e.g. `group.com.example.watch`). | ||
- Enable **Background Modes** and tick **Uses Bluetooth LE accessories**. | ||
|
||
## Entitlements | ||
|
||
Both the iOS application and the WatchKit extension must include the chosen | ||
`com.apple.security.application-groups` entry in their entitlements files so | ||
that the two apps can communicate. | ||
|
||
## Pairing with a watch | ||
|
||
1. Pair an Apple Watch with the iPhone or simulator using the **Watch** app. | ||
2. In Xcode choose a run destination that includes both the phone and watch. | ||
3. Build and run; Xcode installs the watch app and establishes the pairing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Watch, { watchEvents } from 'react-native-watch-connectivity'; | ||
import type { | ||
Payload, | ||
ReplyCallback, | ||
ErrorCallback, | ||
} from '../NativeWearConnectivity'; | ||
|
||
/** | ||
* Connector responsible for activating the WatchConnectivity session and | ||
* forwarding messages between the iOS app and the paired Apple Watch. | ||
*/ | ||
class AppleWatchConnector { | ||
private activated = false; | ||
|
||
/** | ||
* Ensures that the underlying WCSession is activated before any | ||
* communication attempts. | ||
*/ | ||
activateSession() { | ||
if (this.activated) { | ||
return; | ||
} | ||
try { | ||
Watch.activateSession(); | ||
this.activated = true; | ||
} catch (err) { | ||
console.warn('Failed to activate Apple Watch session', err); | ||
} | ||
} | ||
|
||
/** | ||
* Sends a message to the paired Apple Watch device. | ||
*/ | ||
sendMessage( | ||
message: Payload, | ||
cb?: ReplyCallback, | ||
errCb?: ErrorCallback | ||
) { | ||
this.activateSession(); | ||
Watch.sendMessage(message, cb, errCb); | ||
} | ||
} | ||
|
||
const appleWatchConnector = new AppleWatchConnector(); | ||
|
||
export { appleWatchConnector, watchEvents as appleWatchEvents }; | ||
export default appleWatchConnector; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
declare module 'react-native-watch-connectivity' { | ||
import { EmitterSubscription } from 'react-native'; | ||
|
||
export interface WatchConnectivity { | ||
activateSession(): void; | ||
sendMessage( | ||
message: any, | ||
reply?: (data: any) => void, | ||
error?: (err: any) => void | ||
): void; | ||
} | ||
|
||
export const watchEvents: { | ||
addListener: ( | ||
event: string, | ||
listener: (...args: any[]) => void | ||
) => EmitterSubscription; | ||
on: ( | ||
event: string, | ||
listener: (...args: any[]) => void | ||
) => EmitterSubscription; | ||
}; | ||
|
||
const Watch: WatchConnectivity; | ||
export default Watch; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Activate WCSession before registering iOS listeners
The new iOS code registers event listeners directly via
appleWatchEvents.addListener
but never activates the underlying WatchConnectivity session. Activation currently only happens insideAppleWatchConnector.sendMessage
, so an app that starts by listening for incoming watch messages (without sending anything first) will never activate the session and will miss all events. CallappleWatchConnector.activateSession()
before subscribing so inbound watch messages are received even when no outbound message is sent.Useful? React with 👍 / 👎.