Flutter WearOS Connectivity

Version Publisher Points LINCENSE

A plugin that provides a wrapper that enables Flutter apps to communicate with apps running on WearOS.

Note: I'd also written packages to communicate with WatchOS devices, you can check it out right here.

Table of contents

Screenshots

Supported platforms

  • Android

Features

Use this plugin in your Flutter app to:

  • Communicate with WearOS application.
  • Send message data.
  • Obtaining and handling data items.
  • Sync data.
  • Transfer files.
  • Obtain connected device list.
  • Detect other devices capabilities.

Getting started

For WatchOS companion app, this plugin uses Watch Connectivity framework under the hood to communicate with IOS app.

Configuration

Android

  1. Create an WearOS companion app, you can follow this instruction to create new WearOS app.

Note: The WearOS companion app package name must be same as your Android app package name in order to communicate with each other.

That's all, you're ready to communicate with WearOS app now.

How to use

Get started

Import the library

import 'package:flutter_wear_os_connectivity/flutter_wear_os_connectivity.dart';

Create new instance of FlutterWearOsConnectivity

FlutterWearOsConnectivity _flutterWearOsConnectivity =
      FlutterWearOsConnectivity();

Configuring Data Layer API dependencies

Configure

NOTE: Your will unable to use other methods if you don't call configureWearableAPI() method.

_flutterWearOsConnectivity.configureWearableAPI();

Obtaning connected devices in Android Wear network

Each Android device can connect to many WearOS devices at the same time, so you should keep track on all available devices.

For each WearOsDevice in Android Wear network, Google Server generates a corresponding path.

For example: wear://<deviceId>/<customPath>

WearOsDevice has following properties:

  • id

An opaque string that represents a device in the Android Wear network.

  • name

The name of the device.

  • isNearby

A bool value indicating that this device can be considered geographically nearby the local device.

  • getCompanionPackageName

Obtaining all connected devices

Get current connected devices in Android Wear network. This method returns a List of WearOsDevice.

List<WearOsDevice> _connectedDevices = await _flutterWearOsConnectivity.getConnectedDevices();

Obtaining local device

Get current local device (your phone) information. This method returns a single WearOsDevice object shows local device information.

WearOsDevice _localDevice = await _flutterWearOsConnectivity.getLocalDevice();

Find Device ID from bluetooth address

Call this method when you have a bluetooth address and need to find WearOsDevice's id. This method return a nullable String value indicating whether a device is exist on Android Wear network.

String? deviceId = await _flutterWearOsConnectivity.findDeviceIdFromBluetoothAddress("AA-AA-AA-AA-AA-AA");

FlutterWearOsConnectivity plugin provides information on which WearOsDevices on the Android Wear network support which custom app capabilities. WearOsDevices represent both mobile and wearable devices that are connected to the network. A capability is a feature that an app defines.

For example, a mobile Android app could advertise that it supports remote control of video playback. When the wearable version of that app is installed, it can use the FlutterWearOsConnectivity plugin to check if the mobile version of the app is installed and supports that feature. If it does, the wearable app can show the play/pause button to control the video on the other device using a message.

This can also work in the opposite direction, with the wearable app listing capabilities it supports.

The capability information will be retrieved as CapabilityInfo object.

CapabilityInfo object contains following properties:

  • name

Name of capability which can be declared from Advertise new device capability.

  • associatedDevices

A Set of WearOsDevice indicating which devices associated with corresponding capability.

Announces that a capability has become available on the local device. After you declare new capability, other devices can see your local device's capabilities via getAllCapabilities method.

There are two ways to do that:

1/ Declare new capabilities inside res/values/wear.xml:

  • Create an XML configuration file in the res/values/ directory of your project and name it wear.xml.
  • Add a resource named android_wear_capabilities to wear.xml.
  • Define capabilities that the device provides.
<resources xmlns:tools="http://schemas.android.com/tools"
           tools:keep="@array/android_wear_capabilities">
    <string-array name="android_wear_capabilities">
        <item>sample_capability_1</item>
        <item>sample_capability_2</item>
        <item>sample_capability_3</item>
        <item>sample_capability_4</item>
    </string-array>
</resources>

2/ Declare new capabilities with registerNewCapability(name) method of FlutterWearOsConnectivity:

await Future.wait([
_flutterWearOsConnectivity.registerNewCapability("sample_capability_1"),
_flutterWearOsConnectivity.registerNewCapability("sample_capability_2"),
_flutterWearOsConnectivity.registerNewCapability("sample_capability_3"),
_flutterWearOsConnectivity.registerNewCapability("sample_capability_4")
]);

Remove existing capability

If you don't want to advertise a capability anymore, you can just simply remove it by calling removeExistingCapability(name).

await _flutterWearOsConnectivity.removeExistingCapability("sample_capability_1");

Obtain all available capabilities on Android Wear network

You can also get all available capabilies associated with other devices on Android Wear network.

List<CapabilityInfo> _availableCapabilities = _flutterWearOsConnectivity.getAllCapabilities();

Find a capability by name

Find CapabilityInfo via capability name. This method returns a nullable CapabilityInfo instance indicating whether a capability is existed.

CapabilityInfo? _capabilityInfo = await _flutterWearOsConnectivity.findCapabilityByName("sample_capability_1");

Subscribing to capability changed

You can listen to CapabilityInfo changed via capability name

_flutterWearOsConnectivity.capabilityChanged(capabilityName: "sample_capability_1").listen((capabilityInfo) {
    inspect(capabilityInfo);
});

Or via capability path

_flutterWearOsConnectivity.capabilityChanged(capabilityPath: Uri(scheme: "wear", host: "*", path: "/sample_capability_1")).listen((capabilityInfo) {
    inspect(capabilityInfo);
});

Note: Capability path is provided in following format by Wear network: wear://*/<capabilityName>

Unsubscribing to capability changed

You can also unlisten to CapabilityInfo changed via capability name

_flutterWearOsConnectivity.removeCapabilityListener(capabilityName: "sample_capability_1");

Or via capability path

_flutterWearOsConnectivity.removeCapabilityListener(capabilityPath: Uri(scheme: "wear", host: "*", path: "/sample_capability_1"));

Note: Capability path is provided in following format by Wear network: wear://*/<capabilityName>


Sending and handling WearOsMessage

Listen to upcoming WearOsMessage receive events

Unlisten for upcoming WearOsMessage receive events


Synchronize and manage DataItem

Synchronize data on specific path

Find DataItem from specific path

Find DataItem list from specific path

Delete DataItem list from specific path

Obtain all available DataItems

Listen to upcoming DataItem changed

Unlisten to DataItem changed