ssttonn / flutter_wear_os_connectivity

A plugin that provides a wrapper that enables Flutter apps to communicate with apps running on WearOS.
BSD 3-Clause "New" or "Revised" License
11 stars 18 forks source link

Problems getting devices. #6

Open gabojara opened 1 month ago

gabojara commented 1 month ago

I'm trying to get the devices but I get errors, the same thing happens when I want to listen to the messages received on the phone.

class _HomeScreenState extends State<HomeScreen> {
  late FlutterWearOsConnectivity _wearOsConnectivity;

  @override
  void initState() {
    super.initState();
    _wearOsConnectivity = FlutterWearOsConnectivity();

    _initWearOs();
  }

  void _initWearOs() async {
    _wearOsConnectivity = FlutterWearOsConnectivity();

    await _wearOsConnectivity.configureWearableAPI();

    List<WearOsDevice> connectedDevices = await _wearOsConnectivity.getConnectedDevices();

    print(connectedDevices);
  }
}

Error:

 Unhandled Exception: PlatformException(500, Can't retrieve connected devices, please try again, null, null)
                                                                                                    #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:648:7)
                                                                                                    #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
                                                                                                    <asynchronous suspension>
                                                                                                    #2      FlutterWearOsConnectivity.getConnectedDevices (package:flutter_wear_os_connectivity/flutter_wear_os_connectivity.dart:49:21)
                                                                                                    <asynchronous suspension>
                                                                                                    #3      _HomeScreenState._initWearOs (package:padelapp/app/home.dart:36:43)
                                                                                                    <asynchronous suspension>
deveshm commented 1 month ago

Your phone might not be on the wearos network. have you tried using the main Wear OS app to connect your phone to your watch?

gabojara commented 1 month ago

My wear os doesn't work with that, it works with Galaxy Wearable https://play.google.com/store/apps/details?id=com.samsung.android.app.watchmanager

trabulium commented 1 month ago

Have you tried using the Emulator in Android Studio as the first step? I'm successfully using this with a Galaxy Wearable.

You need to ensure your namespace & applicationId are the same in your Watch and Phone app

  1. android/app/build.gradle: namespace "com.example.wearexample" android/app/build.gradle: applicationId "com.example.wearexample"

  2. in android/app/src/main/AndroidManifest.xml, the name and permissions are defined

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.wearexample">

<!-- Permissions that the app requires -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<!-- Add this if targeting Android 12 or higher -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>

3. in android/app/build.gradle you need to have your signing config setup in **BOTH Android and WearOS app** to use the same key.
`
def keystoreProperties = new Properties()

def keystorePropertiesFile = rootProject.file('key.properties') if (keystorePropertiesFile.exists()) { keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) } ........

    signingConfigs {
    release {
        keyAlias keystoreProperties['keyAlias']
        keyPassword keystoreProperties['keyPassword']
        storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
        storePassword keystoreProperties['storePassword']
    }
}`

I'm using the following code to initialize / connect on the WearOS side.

` WatchViewModel() { print("WatchViewModel initialized"); _initWearableAPI(); _setupChannelHandler(); }

void _setupChannelHandler() { _channel.setMethodCallHandler((call) async { print("Received method call: ${call.method}"); _handleMessage(call.arguments); return null; }); }

void _initWearableAPI() { print("Initializing Wearable API"); wearOsConnectivity.configureWearableAPI().then(() { print("Wearable API configured"); _wearOsConnectivity.getConnectedDevices().then((devices) { print("Connected devices: ${devices.length}"); if (devices.isNotEmpty) { _phoneDeviceId = devices.first.id; print("Phone device ID: $_phoneDeviceId"); _listenForMessages(); } else { print("No connected devices found"); } }); }).catchError((error) { print("Error configuring Wearable API: $error"); }); } `