0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

iOSにおけるファーウェイWear Engineのウォッチ接続手順についての考察

0
Last updated at Posted at 2026-10-01

公式資料によると、iOSからファーウェイウォッチに接続するロジックは次の通りです。

HiWear.getDeviceClient().getCommonDevices { status, deviceArr in
    if status != .WESErrorCodeSuccess {
        // Fails to obtain the device list. Error code: status.rawValue
        return
    }
    guard let deviceArr else {
        // The device list is null.
        return
    }
    guard deviceArr.first != nil else {
        //The first device in the obtained list is null.
        return
    }
    var deviceString = ""
    var tag = 0
    for device in deviceArr {
        let deviceDescription :String = device.description
        deviceString.append("device<\(tag)>:\(deviceDescription),\n")
        tag += 1
    }
    //The obtained device list is deviceString.
}
let deviceClient: WESDeviceClient = HiWear.getDeviceClient()
// currentDevice indicates the wearable device queries by the API above.
deviceClient.connect(currentDevice) { status, connectStatus in
    if status != .WESErrorCodeSuccess {
        // Connection to the wearable device fails.
    }
    if status == .WESErrorCodeSuccess {
        // Connection to the wearable device is successful.
    }
}

あるいはこのようなものになります。

import UIKit
import WearEngineSDK

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    /// ... ...

    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        guard let url = URLContexts.first?.url else {
            return
        }
        let youWearEngineScheme = "test://" // it should replace with you scheme
        if url.absoluteString.contains(youWearEngineScheme) {
            HiWear.getAuthClient().processAuthResult(with: url)
            connectPreferredDevice(url: url.absoluteString)
        }
    }

    /// connect preferred device which probability connected on Huawei Health app.
    /// warning: if the current device of Huawei Health app is not support iOS WearEngine SDK, the preferred device maybe one of history device of Huawei Health app.
    private func connectPreferredDevice(url: String) {
        let params: String = url.components(separatedBy: "?").last ?? ""
        let allItem = params.components(separatedBy: "&")
        var dict: [String: String] = [:]
        for item in allItem {
            let subItems = item.components(separatedBy: "=")
            guard let key = subItems.first, let value = subItems.last else {
                continue
            }
            dict[key] = value
        }
        let errorCode = dict["errorCode"] ?? ""
        debugPrint("errorCode: \(errorCode).")
        let result = WearEngineServerErrorCode(rawValue: Int(errorCode) ?? 0)
        if result != .WESErrorCodeAuthCodeAppShouldLaunch {
            debugPrint("error that auth fail or no devices found.")
            return
        }
        let uuid = dict["uuid"] ?? ""
        HiWear.getDeviceClient().getCommonDevices { status, allDevices in
            if status != .WESErrorCodeSuccess {
                debugPrint("query devices fail, status: \(status).")
                return
            }
            guard let allDevices else {
                debugPrint("devices is nil.")
                return
            }
            for device in allDevices {
                if device.uuid != uuid {
                    continue
                }
                debugPrint("current device was found")
                HiWear.getDeviceClient().connect(device) { status, connectStatus in
                    if status == .WESErrorCodeSuccess && connectStatus == .stateConnected {
                        // now the device was connected, you can use it by call getCurrentDevices
                        let currentDevice = HiWear.getDeviceClient().getCurrentDevices()
                        // do something here
                    }
                }
            }
        }
    }
}

しかし、ここに落とし穴があります。HiWear.getDeviceClient().getCommonDevicesで取得するのはあくまでキャッシュであり、リアルタイムの状態ではありません。

このキャッシュはHiWear.getAuthClient().authによる権限取得時に生成されます。それ以降ウォッチ接続状態が変わってもこのキャッシュは更新されません。キャッシュを更新するのに、HiWear.getAuthClient().authをもう一度呼び出す必要があります。

つまり、HiWear.getDeviceClient().connectで接続を試みて失敗した場合、キャッシュが更新されていない可能性があるということです。ここで、HiWear.getAuthClient().authを呼び出し、キャッシュを更新し、もう一度HiWear.getDeviceClient().connectを呼び出せば、接続が成功する可能性があります。

参考

https://developer.huawei.com/consumer/en/doc/connectivity-Guides/request-user-authorization-ios-0000001920933293
https://developer.huawei.com/consumer/en/doc/connectivity-Guides/check-availabla-dev-ios-0000001921237861

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?