Connection Management¶
BLE Connection Manager¶
gopro_sdk.connection.ble_manager.BleConnectionManager
¶
BLE connection manager.
Responsibilities: - Establish and disconnect BLE connections - Accumulate BLE response fragments - Handle BLE notification callbacks
Initialize BLE connection manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
str
|
Last four digits of camera serial number |
required |
timeout_config
|
TimeoutConfig
|
Timeout configuration |
required |
Attributes¶
Functions¶
connect
async
¶
Establish BLE connection.
Implementation based on official Tutorial, directly using bleak's BleakScanner and BleakClient.
Raises:
| Type | Description |
|---|---|
BleConnectionError
|
BLE connection failed |
wait_for_response
async
¶
Wait for BLE response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float | None
|
Timeout in seconds, None means use default timeout |
None
|
Returns:
| Type | Description |
|---|---|
bytes
|
Response data |
Raises:
| Type | Description |
|---|---|
BleConnectionError
|
Wait timeout |
write
async
¶
Write BLE data (automatic fragmentation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Characteristic UUID string (standard format: 8-4-4-4-12) |
required |
data
|
bytes
|
Data to write |
required |
Raises:
| Type | Description |
|---|---|
BleConnectionError
|
Write failed |
HTTP Connection Manager¶
gopro_sdk.connection.http_manager.HttpConnectionManager
¶
HttpConnectionManager(
target: str,
timeout_config: TimeoutConfig,
credentials: CohnCredentials | None = None,
)
HTTP/COHN connection manager.
Responsibilities: - Establish and disconnect HTTP sessions - Configure SSL context (support COHN self-signed certificates) - Send HTTP requests and handle responses
Initialize HTTP connection manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
str
|
Last four digits of camera serial number |
required |
timeout_config
|
TimeoutConfig
|
Timeout configuration |
required |
credentials
|
CohnCredentials | None
|
COHN credentials (optional, can be set later) |
None
|
Attributes¶
base_url
property
¶
HTTP base URL.
Multi-camera scenarios must use COHN mode (each camera has independent IP).
Returns:
| Type | Description |
|---|---|
str
|
COHN mode: https://{ip_address} (default port 443) |
Raises:
| Type | Description |
|---|---|
HttpConnectionError
|
COHN credentials not configured |
Functions¶
set_credentials
¶
Set COHN credentials.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
credentials
|
CohnCredentials
|
COHN credentials |
required |
connect
async
¶
Establish HTTP connection (COHN mode).
Multi-camera scenarios must use COHN mode: HTTPS + SSL + authentication
Raises:
| Type | Description |
|---|---|
HttpConnectionError
|
HTTP connection failed or COHN credentials not configured |
quick_connectivity_check
async
¶
Quickly check if IP is reachable (without waiting too long).
Returns:
| Type | Description |
|---|---|
bool
|
True if connection is possible, False otherwise |
get
¶
Send GET request (returns async context manager).
Note: - This method is synchronous, returns an async context manager - Will automatically establish HTTPS connection on first call - Usage: async with self.get(...) as resp:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
API endpoint (relative path, e.g., "gopro/camera/state") |
required |
params
|
dict[str, Any] | None
|
Query parameters |
None
|
Returns:
| Type | Description |
|---|---|
_AutoConnectContext
|
Async context manager for HTTP response (_AutoConnectContext) |
Raises:
| Type | Description |
|---|---|
HttpConnectionError
|
Request failed |
Usage example
async with self.get("gopro/camera/state") as resp: data = await resp.json()
put
¶
Send PUT request (returns async context manager).
Note: - This method is synchronous, returns an async context manager - Will automatically establish HTTPS connection on first call - Usage: async with self.put(...) as resp:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
API endpoint |
required |
data
|
Any
|
Request data |
None
|
Returns:
| Type | Description |
|---|---|
_AutoConnectContext
|
Async context manager for HTTP response (_AutoConnectContext) |
Raises:
| Type | Description |
|---|---|
HttpConnectionError
|
Request failed |
Usage example
async with self.put("gopro/camera/setting", data=...) as resp: result = await resp.json()
download
async
¶
download(
endpoint: str,
destination: str,
chunk_size: int = 8192,
progress_callback: Callable[[int, int], None]
| None = None,
) -> int
Download file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
API endpoint |
required |
destination
|
str
|
Destination file path |
required |
chunk_size
|
int
|
Chunk size |
8192
|
progress_callback
|
Callable[[int, int], None] | None
|
Progress callback function (downloaded: int, total: int) -> None |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Number of bytes downloaded |
Raises:
| Type | Description |
|---|---|
HttpConnectionError
|
Download failed |
Health Check Mixin¶
gopro_sdk.connection.health_check.HealthCheckMixin
¶
Health check and auto-reconnect Mixin.
Provides connection health check and auto-reconnect functionality. Requires subclasses to have ble and http attributes.
Functions¶
is_healthy
async
¶
Check connection health status.
Returns:
| Type | Description |
|---|---|
bool
|
True if connection is healthy, False otherwise |
reconnect
async
¶
Smart reconnect.
Returns:
| Type | Description |
|---|---|
bool
|
True if reconnect successful, False otherwise |
set_auto_reconnect
¶
Set whether to enable auto-reconnect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enabled
|
bool
|
True to enable, False to disable |
required |
set_max_reconnect_attempts
¶
Set maximum reconnect attempt count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attempts
|
int
|
Maximum attempt count (>= 1) |
required |
get_health_stats
¶
Get health statistics.
Returns:
| Type | Description |
|---|---|
dict[str, any]
|
Health statistics dictionary |
BLE Scanner¶
gopro_sdk.connection.ble_scanner.BleScanner
¶
BLE scanner
Used to discover nearby GoPro camera devices.
Functions¶
scan_devices_stream
async
staticmethod
¶
scan_devices_stream(
duration: float = 8.0,
idle_timeout: float = 2.0,
target_count: int | None = None,
) -> AsyncIterator[list[dict[str, Any]]]
Stream scan for GoPro devices
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
duration
|
float
|
Maximum scan time (seconds) |
8.0
|
idle_timeout
|
float
|
Idle timeout (seconds), ends early if no new devices after this time |
2.0
|
target_count
|
int | None
|
Target device count, ends early when reached |
None
|
Yields:
| Type | Description |
|---|---|
AsyncIterator[list[dict[str, Any]]]
|
Device list, each device is a {"name": str, "address": str} dictionary |
Usage example:
scan_devices
async
staticmethod
¶
One-time scan for GoPro devices
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
duration
|
float
|
Scan time (seconds) |
8.0
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
Device list, each device is a {"name": str, "address": str} dictionary |
Usage example:
scan_serials_stream
async
staticmethod
¶
scan_serials_stream(
duration: float = 8.0,
idle_timeout: float = 2.0,
target_count: int | None = None,
) -> AsyncIterator[str]
Stream scan for GoPro serial numbers
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
duration
|
float
|
Maximum scan time (seconds) |
8.0
|
idle_timeout
|
float
|
Idle timeout (seconds) |
2.0
|
target_count
|
int | None
|
Target count |
None
|
Yields:
| Type | Description |
|---|---|
AsyncIterator[str]
|
Camera serial number (extracted from device name) |
Usage example: