Overview
TheuseMobileVoiceAgent hook provides a complete voice agent implementation for React Native applications. It manages WebRTC connections, handles tool calls, and integrates with your app’s navigation and functions.
Import
Usage
Type Signature
Parameters
Configuration options for the voice agent
Return Value
Current session status:
idle- Not connected, ready to startconnecting- Connection in progressconnected- Active voice sessionerror- Connection or execution error
Error message if status is
error, otherwise null.Convenience flag:
true when status is connecting.Convenience flag:
true when status is connected.Start the voice session. This will:
- Request microphone permissions (Android)
- Load WebRTC native module
- Initialize WebRTC connection
- Configure agent runtime with tools
- Begin listening for voice input
Stop the voice session and clean up resources. Safe to call multiple times.
Behavior
Session Lifecycle
Microphone Permissions
On Android, the hook automatically requestsRECORD_AUDIO permission when start() is called. Permission states:
- Granted: Session starts normally
- Denied: Error with message to retry
- Never Ask Again: Error with message to enable in settings
Tool Call Handling
The hook automatically handles all realtime tool calls:- Navigation Commands: Calls
navigate()with resolved route path - Mobile Functions: Executes registered frontend functions
- Backend Functions: Calls backend API to execute functions
- Tool Results: Sends results back to realtime session
WebRTC Configuration
The hook uses default WebRTC settings:- Audio constraints:
{ audio: true, video: false } - Remote audio volume:
10(maximum) - Model: From runtime config or
gpt-realtime
Examples
Basic Integration
With Loading State
With Status Indicator
Error Handling
Common errors and solutions:| Error | Cause | Solution |
|---|---|---|
| ”WebRTC native module is not available” | react-native-webrtc not installed | Install and link react-native-webrtc |
| ”Runtime is still loading” | Called start() while runtimeLoading: true | Wait for runtime to load |
| ”Microphone permission denied” | User denied permission | Request user to retry |
| ”Microphone permission blocked” | User selected “Never Ask Again” | Direct user to system settings |
| ”Functions are still loading” | Functions not ready | Wait for initialization |
| ”Realtime WebRTC negotiation failed” | Network or auth error | Check API URL and credentials |
Implementation Notes
Internal State
The hook maintains several refs to manage state across renders:sessionRef- Voice session instanceagentRuntimeRef- Agent runtime with toolsfrontendRegistryRef- Loaded function registryhandledToolCallIdsRef- Prevents duplicate tool executiontoolCallNamesByIdRef- Maps call IDs to function namespendingToolCallsRef- Queues early tool calls
Cleanup
The hook automatically cleans up on unmount:- Stops voice session
- Clears all refs
- Cancels pending operations
Performance
Function loading is memoized and only runs whenruntime changes. Tool call handlers use useCallback to prevent unnecessary re-renders.
See Also
- Agent Runtime API - Runtime configuration
- WebRTC Transport - Transport layer
- Types Reference - TypeScript types