Module: Toybox.Communications
Overview
The Communications Module provides tools for communication.
With the Communications module, widgets and apps will be able to communicate with a mobile phone via Bluetooth Low Energy (BLE). The mobile phone may be sharing data with the device, or it may act as a bridge between the app and the Internet. This allows the device to become part of the Internet of Things.
This module was made available to foreground data fields with API 5.0.0
Classes Under Namespace
Classes: ConnectionListener, MailboxIterator, Message, OAuthMessage, PhoneAppMessage, SyncDelegate
Constant Summary
-
Error
-
TokenResult
-
SigningMethod
-
HttpRequestMethod
-
HttpResponseContentType
-
WifiConnectionStatus
-
HttpRequestContentType
-
PackingFormat
-
Dithering
Name | Value | Since | Description | See Also |
---|---|---|---|---|
UNKNOWN_ERROR | 0 | API Level 1.0.0 |
An unknown error has occurred. |
|
BLE_ERROR | -1 | API Level 1.0.0 |
A generic BLE error has occurred. |
|
BLE_HOST_TIMEOUT | -2 | API Level 1.0.0 |
We timed out waiting for a response from the host. |
|
BLE_SERVER_TIMEOUT | -3 | API Level 1.0.0 |
We timed out waiting for a response from a server. |
|
BLE_NO_DATA | -4 | API Level 1.0.0 |
Response contained no data. |
|
BLE_REQUEST_CANCELLED | -5 | API Level 1.0.0 |
The request was cancelled at the request of the system. |
|
BLE_QUEUE_FULL | -101 | API Level 1.0.0 |
Too many requests have been made. |
|
BLE_REQUEST_TOO_LARGE | -102 | API Level 1.0.0 |
Serialized input data for the request was too large. |
|
BLE_UNKNOWN_SEND_ERROR | -103 | API Level 1.0.0 |
Send failed for an unknown reason. |
|
BLE_CONNECTION_UNAVAILABLE | -104 | API Level 1.0.0 |
No BLE connection is available. |
|
INVALID_HTTP_HEADER_FIELDS_IN_REQUEST | -200 | API Level 1.0.0 |
Request contained invalid http header fields. |
|
INVALID_HTTP_BODY_IN_REQUEST | -201 | API Level 1.0.0 |
Request contained an invalid http body. |
|
INVALID_HTTP_METHOD_IN_REQUEST | -202 | API Level 1.0.0 |
Request used an invalid http method. |
|
NETWORK_REQUEST_TIMED_OUT | -300 | API Level 1.0.0 |
Request timed out before a response was received. |
|
INVALID_HTTP_BODY_IN_NETWORK_RESPONSE | -400 | API Level 1.0.0 |
Response body data is invalid for the request type. |
|
INVALID_HTTP_HEADER_FIELDS_IN_NETWORK_RESPONSE | -401 | API Level 1.0.0 |
Response contained invalid http header fields. |
|
NETWORK_RESPONSE_TOO_LARGE | -402 | API Level 1.0.0 |
Serialized response was too large. |
|
NETWORK_RESPONSE_OUT_OF_MEMORY | -403 | API Level 3.0.0 |
Ran out of memory processing network response. |
|
STORAGE_FULL | -1000 | API Level 2.2.0 |
Filesystem too full to store response data. |
|
SECURE_CONNECTION_REQUIRED | -1001 | API Level 2.3.0 |
Indicates an https connection is required for the request. |
|
UNSUPPORTED_CONTENT_TYPE_IN_RESPONSE | -1002 | API Level 2.4.1 |
Content type given in response is not supported or does not match what is expected. |
|
REQUEST_CANCELLED | -1003 | API Level 2.4.2 |
Http request was cancelled by the system. |
|
REQUEST_CONNECTION_DROPPED | -1004 | API Level 3.0.0 |
Connection was lost before a response could be obtained. |
|
UNABLE_TO_PROCESS_MEDIA | -1005 | API Level 3.0.2 |
Downloaded media file was unable to be read. |
|
UNABLE_TO_PROCESS_IMAGE | -1006 | API Level 3.0.3 |
Downloaded image file was unable to be processed. |
|
UNABLE_TO_PROCESS_HLS | -1007 | API Level 3.0.10 |
HLS content could not be downloaded. Most often occurs when requested and provided bit rates do not match. |
Name | Value | Since | Description | See Also |
---|---|---|---|---|
OAUTH_RESULT_TYPE_URL | 0 | API Level 1.3.0 |
How the OAuth token will be returned in the final step. |
Name | Value | Since | Description | See Also |
---|---|---|---|---|
OAUTH_SIGNING_METHOD_HMAC_SHA1 | 0 | API Level 1.3.0 |
How the OAuth request will be signed |
Name | Value | Since | Description | See Also |
---|---|---|---|---|
HTTP_REQUEST_METHOD_GET | 1 | API Level 1.2.0 |
Specifies a request be executed using the GET method. |
|
HTTP_REQUEST_METHOD_PUT | 2 | API Level 1.2.0 |
Specifies a request be executed using the PUT method. |
|
HTTP_REQUEST_METHOD_POST | 3 | API Level 1.2.0 |
Specifies a request be executed using the POST method. |
|
HTTP_REQUEST_METHOD_DELETE | 4 | API Level 1.2.0 |
Specifies a request be executed using the DELETE method. |
Name | Value | Since | Description | See Also |
---|---|---|---|---|
HTTP_RESPONSE_CONTENT_TYPE_JSON | 0 | API Level 1.3.0 |
Content type specifier for response is expected to be a json type. Content type string must be "application/json". |
|
HTTP_RESPONSE_CONTENT_TYPE_URL_ENCODED | 1 | API Level 1.3.0 |
Content type specifier for response is expected to indicate url encoding. Content type string must be "application/x-www-form-urlencoded". |
|
HTTP_RESPONSE_CONTENT_TYPE_GPX | 2 | API Level 2.2.0 |
Content type specifier for response is expected to be a gpx type. |
|
HTTP_RESPONSE_CONTENT_TYPE_FIT | 3 | API Level 2.2.0 |
Content type specifier for response is expected to be a FIT type. |
|
HTTP_RESPONSE_CONTENT_TYPE_AUDIO | 4 | API Level 3.0.0 |
Content type specifier for response is expected to be an audio type. Content type string must be of the "audio/*" format. |
|
HTTP_RESPONSE_CONTENT_TYPE_TEXT_PLAIN | 5 | API Level 3.0.0 |
Content type specifier for response is expected to be plain text type. Content type string must be "text/plain" |
|
HTTP_RESPONSE_CONTENT_TYPE_HLS_DOWNLOAD | 6 | API Level 3.0.10 |
Content type specifier for response is expected to be an HLS data type. Content type string must be either "application/vnd.apple.mpegurl" or "audio/mpegurl". |
|
HTTP_RESPONSE_CONTENT_TYPE_ANIMATION_MANIFEST | 7 | API Level 3.1.0 |
Content type specifier for response is expected to be a CIQ animation manifest data type. Content type string must be "application/vnd.garmin.connectiq.animation.manifest". |
|
HTTP_RESPONSE_CONTENT_TYPE_ANIMATION | 8 | API Level 3.1.0 |
Content type specifier for response is expected to be a CIQ animation data type. Content type string must be "image/vnd.garmin.connectiq.animation". |
Name | Value | Since | Description | See Also |
---|---|---|---|---|
WIFI_CONNECTION_STATUS_LOW_BATTERY | 1 | API Level 3.2.0 |
Specifies an error condition, battery is too low to start a WIFI connection. |
|
WIFI_CONNECTION_STATUS_NO_ACCESS_POINTS | 2 | API Level 3.2.0 |
Specifies an error condition, no access-point is stored on the device. |
|
WIFI_CONNECTION_STATUS_UNSUPPORTED | 3 | API Level 3.2.0 |
Specifies an error condition, WIFI is not supported on current device. |
|
WIFI_CONNECTION_STATUS_USER_DISABLED | 4 | API Level 3.2.0 |
Specifies an error condition, WIFI is disabled by user. |
|
WIFI_CONNECTION_STATUS_BATTERY_SAVER_ACTIVE | 5 | API Level 3.2.0 |
Specifies an error condition, WIFI is disabled by battery saver. |
|
WIFI_CONNECTION_STATUS_STEALTH_MODE_ACTIVE | 6 | API Level 3.2.0 |
Specifies an error condition, WIFI is disabled by stealth mode. |
|
WIFI_CONNECTION_STATUS_AIRPLANE_MODE_ACTIVE | 7 | API Level 3.2.0 |
Specifies an error condition, WIFI is disabled by airplane mode. |
|
WIFI_CONNECTION_STATUS_POWERED_DOWN | 8 | API Level 3.2.0 |
Specifies an error condition, WIFI is disabled by the device. |
|
WIFI_CONNECTION_STATUS_UNKNOWN | 9 | API Level 3.2.0 |
Specifies an error condition, WIFI is not usable but status is unknown. |
|
WIFI_CONNECTION_STATUS_CANNOT_CONNECT_TO_ACCESS_POINT | 10 | API Level 3.3.0 |
Specifies an error condition, WIFI can not connect to saved AccessPoint. |
|
WIFI_CONNECTION_STATUS_TRANSFER_ALREADY_IN_PROGRESS | 11 | API Level 3.3.0 |
Specifies an error condition, WIFI transfer already in progress |
Name | Value | Since | Description | See Also |
---|---|---|---|---|
REQUEST_CONTENT_TYPE_URL_ENCODED | 0 | API Level 1.2.0 |
Specifies a content type of application/x-www-form-urlencoded |
|
REQUEST_CONTENT_TYPE_JSON | 1 | API Level 1.2.0 |
Specifies a content type of application/json |
Image packing format used for image request.
The packing format describes the encoding a requested image should use when being transmitted. The encoding used affects the transfer size, decoding time, and image quality.
Name | Value | Since | Description | See Also |
---|---|---|---|---|
PACKING_FORMAT_DEFAULT | 0 | API Level 4.2.0 |
Image data is encoded in the device native format, a lossless encoding that available on all devices. It is very efficient to decode, but often results in large transfer sizes so is slow to download. |
|
PACKING_FORMAT_YUV | 1 | API Level 4.2.0 |
Image data is encoded in YUV format. This is a lossy encoding that is compressed, and is fast to load. It is ideal for photographic imagery with transparency. |
|
PACKING_FORMAT_PNG | 2 | API Level 4.2.0 |
Image data is encoded in PNG format. This is a lossless encoding that is compressed, but is relatively slow to load. It is ideal for non-photographic imagery. |
|
PACKING_FORMAT_JPG | 3 | API Level 4.2.0 |
Image data is encoded in JPG format. This is a lossy encoding that is compressed, and is reasonably fast to load. It is ideal for photographic imagery. |
Name | Value | Since | Description | See Also |
---|---|---|---|---|
IMAGE_DITHERING_NONE | 1 | API Level 1.2.0 |
Do not apply dithering to an image. |
|
IMAGE_DITHERING_FLOYD_STEINBERG | 2 | API Level 1.2.0 |
Apply Floyd-Steinberg dithering to an image. |
Typedef Summary collapse
- PhoneMessageCallback as Lang.Method(msg as Communications.PhoneAppMessage) as Void
Instance Method Summary collapse
-
cancelAllRequests() as Void
Cancel all pending JSON and Image requests.
-
checkWifiConnection(connectionStatusCallback as Lang.Method(result as { :wifiAvailable as Lang.Boolean, :errorCode as Communications.WifiConnectionStatus }) as Void) as Void
Checks if an internet-enabled WIFI access point is visible and can be connected to.
-
emptyMailbox() as Void
deprecated
Clear the contents of the mailbox.
-
encodeURL(url as Lang.String) as Lang.String
Convert a URL String into a percent-encoded string.
-
generateSignedOAuthHeader(url as Lang.String, params as Lang.Dictionary<Lang.String, Lang.Object>, requestMethod as Communications.HttpRequestMethod, signatureMethod as Communications.SigningMethod, token as Lang.String or Null, tokenSecret as Lang.String, consumerKey as Lang.String, consumerSecret as Lang.String) as Lang.String
Generate the value for the "Authorization" header in an OAuth 1.0a request.
-
getMailbox() as Communications.MailboxIterator
deprecated
Get the MailboxIterator for this Application's mailbox.
-
makeImageRequest(url as Lang.String, parameters as Lang.Dictionary or Null, options as { :palette as Lang.Array<Lang.Number>, :maxWidth as Lang.Number, :maxHeight as Lang.Number, :dithering as Communications.Dithering, :packingFormat as Communications.PackingFormat }, responseCallback as Lang.Method(responseCode as Lang.Number, data as WatchUi.BitmapResource or Graphics.BitmapReference or Null) as Void) as Void
Initiate an image download request.
-
makeJsonRequest(url as Lang.String, parameters as Lang.Dictionary<Lang.Object, Lang.Object> or Null, options as { :method as Communications.HttpRequestMethod, :headers as Lang.Dictionary } or Null, responseCallback as Lang.Method(responseCode as Lang.Number, data as Lang.Dictionary or Lang.String or PersistedContent.Iterator or Null) as Void) as Void
deprecated
Initiate a download request.
-
makeOAuthRequest(requestUrl as Lang.String, requestParams as Lang.Dictionary, resultUrl as Lang.String, resultType as Communications.TokenResult, resultKeys as Lang.Dictionary<Lang.String, Lang.String>) as Void
Request an OAuth sign-in through Garmin Connect Mobile.
-
makeWebRequest(url as Lang.String, parameters as Lang.Dictionary<Lang.Object, Lang.Object> or Null, options as { :method as Communications.HttpRequestMethod, :headers as Lang.Dictionary, :responseType as Communications.HttpResponseContentType, :context as Lang.Object or Null, :maxBandwidth as Lang.Number, :fileDownloadProgressCallback as Lang.Method(totalBytesTransferred as Lang.Number, fileSize as Lang.Number or Null) as Void } or Null, responseCallback as Lang.Method(responseCode as Lang.Number, data as Lang.Dictionary or Lang.String or PersistedContent.Iterator or Null) as Void or Lang.Method(responseCode as Lang.Number, data as Lang.Dictionary or Lang.String or PersistedContent.Iterator or Null, context as Lang.Object) as Void) as Void
Initiate a download request.
-
notifySyncComplete(errorMessage as Lang.String or Null) as Void
Send a system notification to indicate that the sync completed.
-
notifySyncProgress(percentageComplete as Lang.Number) as Void
Send a system notification to indicate overall sync progress.
-
openWebPage(url as Lang.String, params as Lang.Dictionary or Null, options as Lang.Dictionary or Null) as Void
Request that GCM issue a phone notification that will open a web page.
-
registerForOAuthMessages(method as Lang.Method(data as Communications.OAuthMessage) as Void) as Void
Register a callback for receiving OAuth messages.
-
registerForPhoneAppMessages(method as Communications.PhoneMessageCallback or Null) as Void
Register a callback for receiving Phone App messages.
-
setMailboxListener(listener as Lang.Method(mailboxIterator as Communications.MailboxIterator) as Void) as Void
deprecated
Add a listener for mailbox events.
-
startSync() as Void
Exit the AppBase and launch it in sync mode.
-
startSync2(options as { :message as Lang.String } or Null) as Void
Exit the AppBase and launch it in sync mode with the provided message.
-
transmit(content as Application.PersistableType, options as Lang.Dictionary or Null, listener as Communications.ConnectionListener) as Void
Send data across the the BLE link.
Typedef Details
PhoneMessageCallback as Lang.Method(msg as Communications.PhoneAppMessage) as Void
Instance Method Details
cancelAllRequests() as Void
Cancel all pending JSON and Image requests.
The number of active requests running in parallel is limited in the Connect IQ platform. This call will cancel all outstanding requests.
checkWifiConnection(connectionStatusCallback as Lang.Method(result as { :wifiAvailable as Lang.Boolean, :errorCode as Communications.WifiConnectionStatus }) as Void) as Void
Checks if an internet-enabled WIFI access point is visible and can be connected to
emptyMailbox() as Void
This method may be removed after System 4.
Clear the contents of the mailbox.
encodeURL(url as Lang.String) as Lang.String
Convert a URL String into a percent-encoded string.
The reserved characters in the string will be replaced with their corresponding hex-value pairs. This follows the URI-encoding scheme as detailed by RFC 3986.
generateSignedOAuthHeader(url as Lang.String, params as Lang.Dictionary<Lang.String, Lang.Object>, requestMethod as Communications.HttpRequestMethod, signatureMethod as Communications.SigningMethod, token as Lang.String or Null, tokenSecret as Lang.String, consumerKey as Lang.String, consumerSecret as Lang.String) as Lang.String
Generate the value for the "Authorization" header in an OAuth 1.0a request.
The returned value can be set as the "Authorization" header for makeWebRequest().
getMailbox() as Communications.MailboxIterator
This method may be removed after System 4.
Get the MailboxIterator for this Application's mailbox.
makeImageRequest(url as Lang.String, parameters as Lang.Dictionary or Null, options as { :palette as Lang.Array<Lang.Number>, :maxWidth as Lang.Number, :maxHeight as Lang.Number, :dithering as Communications.Dithering, :packingFormat as Communications.PackingFormat }, responseCallback as Lang.Method(responseCode as Lang.Number, data as WatchUi.BitmapResource or Graphics.BitmapReference or Null) as Void) as Void
Initiate an image download request.
GCM will scale and dither the image based on the capabilities of the device, but the user will be able to pass additional options (like dithering it down to a one color image)
This method can be used when connected to WiFi or a mobile device over Bluetooth.
makeJsonRequest(url as Lang.String, parameters as Lang.Dictionary<Lang.Object, Lang.Object> or Null, options as { :method as Communications.HttpRequestMethod, :headers as Lang.Dictionary } or Null, responseCallback as Lang.Method(responseCode as Lang.Number, data as Lang.Dictionary or Lang.String or PersistedContent.Iterator or Null) as Void) as Void
This method may be removed after System 4.
Initiate a download request.
The request is asynchronous; the responseCallback will be called when the request returns.
This method can be used when connected to WiFi or a mobile device over Bluetooth.
makeOAuthRequest(requestUrl as Lang.String, requestParams as Lang.Dictionary, resultUrl as Lang.String, resultType as Communications.TokenResult, resultKeys as Lang.Dictionary<Lang.String, Lang.String>) as Void
Request an OAuth sign-in through Garmin Connect Mobile.
A notification will trigger on the phone, that when clicked, provides a
web view that shows requestUrl
. If the user grants permission to the app,
then the callback registered by
registerForOAuthMessages()
will be called with an OAuthMessage
from the OAuth response.
This method can only be used when connected to a mobile device over Bluetooth.
makeWebRequest(url as Lang.String, parameters as Lang.Dictionary<Lang.Object, Lang.Object> or Null, options as { :method as Communications.HttpRequestMethod, :headers as Lang.Dictionary, :responseType as Communications.HttpResponseContentType, :context as Lang.Object or Null, :maxBandwidth as Lang.Number, :fileDownloadProgressCallback as Lang.Method(totalBytesTransferred as Lang.Number, fileSize as Lang.Number or Null) as Void } or Null, responseCallback as Lang.Method(responseCode as Lang.Number, data as Lang.Dictionary or Lang.String or PersistedContent.Iterator or Null) as Void or Lang.Method(responseCode as Lang.Number, data as Lang.Dictionary or Lang.String or PersistedContent.Iterator or Null, context as Lang.Object) as Void) as Void
Initiate a download request.
Web requests are asynchronous. The supplied response callback method will be called when the request returns.
This method can be used when connected to WiFi or a mobile device over Bluetooth.
notifySyncComplete(errorMessage as Lang.String or Null) as Void
Send a system notification to indicate that the sync completed.
notifySyncProgress(percentageComplete as Lang.Number) as Void
Send a system notification to indicate overall sync progress.
openWebPage(url as Lang.String, params as Lang.Dictionary or Null, options as Lang.Dictionary or Null) as Void
Request that GCM issue a phone notification that will open a web page.
This method will push a phone notification that must be accepted by the user. If the used accepts it, a web page defined by this method will be opened in the default browser on the phone.
This method can only be used when connected to a mobile device over Bluetooth.
registerForOAuthMessages(method as Lang.Method(data as Communications.OAuthMessage) as Void) as Void
Register a callback for receiving OAuth messages.
The callback will be called once for each received OAuth message. If there are messages waiting for the app when this function is called, the callback will immediately be called once for each waiting message.
registerForPhoneAppMessages(method as Communications.PhoneMessageCallback or Null) as Void
Register a callback for receiving Phone App messages.
The callback will be called once for each message received. If there are messages waiting for the app when this function is called, the callback will immediately be called once for each waiting message.
setMailboxListener(listener as Lang.Method(mailboxIterator as Communications.MailboxIterator) as Void) as Void
This method may be removed after System 4.
Add a listener for mailbox events.
The listener method is called whenever a new message is received.
startSync() as Void
Exit the AppBase and launch it in sync mode.
startSync2(options as { :message as Lang.String } or Null) as Void
Exit the AppBase and launch it in sync mode with the provided message.
transmit(content as Application.PersistableType, options as Lang.Dictionary or Null, listener as Communications.ConnectionListener) as Void
Send data across the the BLE link.