We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
GuidesWear EngineiOS Phone App DevelopmentApp DevelopmentApp-to-App Message Communications

App-to-App Message Communications

This API is used to establish the communications channel between your phone app and wearable app, to receive and send packet messages and files customized by your app. This makes the interaction between the phone app and its wearable counterpart a full-fledged reality, providing the distributed scenarios and experience for your users. For example, a phone app sends an audio file to the wearable app, to play music on the wearable app; the phone app sends a pause command, to pause the music playback and the like on the wearable device.

Before sending or receiving messages, apply for the DEVICE_MANAGER permission from the user on the phone (by referring to Requesting User Authorization), otherwise you may not be able to call the corresponding API.

Checking Whether the App Has Been Installed on the Wearable Device

  1. Refer to Querying Available Wearable Devices to obtain the paired device list and select the device for communications.
  2. Call the getP2PClient method in HiWear to obtain the P2PClient object.
  3. Call the isAppInstalled method to check whether the specified app has been installed.

    HiWear.getP2PClient().isAppInstalled(currentDevice, targetPkgName: "com.xxx" , srcPkgName: "com.yyy") { status, data in
        if (status == .WESErrorCodeSuccess) {
            let dataString :String = data ?? ""
            // The app installation status is successfully queried and is indicated by dataString.
        } else {
            // Fails to query the app installation status. Error code: status.rawValue
        }
    }

Obtaining the Version Number of the App on the Wearable Device

  1. Refer to Querying Available Wearable Devices to obtain the paired device list and select the device for communications.
  2. Call the getP2PClient method in HiWear to obtain the P2PClient object.
  3. Call the getAppVersion method to obtain the app version number of a specified device.

    HiWear.getP2PClient().getAppVersion(currentDevice, targetPkgName: "com.xxx" , srcPkgName: "com.yyy") { status, data in
        if (status == .WESErrorCodeSuccess) {
            var dataString :String = data ?? ""
            if data == "-1" {
                dataString = "Not installed"
            }
            // The app version number is successfully queried and is indicated by dataString.
        } else {
            // Fails to query the app version. Error code: status.rawValue
        }
    }

Checking Whether the Wearable App is Running

Before sending a P2P message, you can use the ping method to check whether your phone app can communicate with the wearable app.

  1. Refer to Querying Available Wearable Devices to obtain the paired device list and select the device for communications.
  2. Call the getP2PClient method in HiWear to obtain the P2PClient object.
  3. Call the ping method and specify the bundle name of your wearable app to be communicated with using the ping method. (This step is optional. If the bundle name is not specified, the bundle name of your phone app will be used by default.)
  4. Call the ping method to check whether your wearable app is connected.

    HiWear.getP2PClient().ping(currentDevice, srcPkgName: "com.xxx", destPkgName:
        "com.yyy") { status in
        if status == .WESErrorCodeP2PWatchAPPRunning {
            // The ping operation succeeds.
        } else {
            // The ping operation fails. Error code: status.rawValue
        }
    }

NOTE
  1. For the ping API, the Wear Engine SDK for iOS only allows for using the ping method on the phone to start a watch-based app, but not using the ping method on the watch to start a phone-based app.
  2. After the watch-based app is started using the ping method, the app needs to register a message listener to receive messages from the phone. If you want to send a message from the phone-based app to the watch-based app immediately, it is recommended that you wait until the watch-based app is started before sending a message to the phone-based app. Ensure that the watch can receive messages properly, and then send messages from the phone.

Sending P2P Messages from Your Phone App

The maximum size of a message is 1 KB. If the length of a message exceeds the limit, divide the message into several packets.

If the phone app is able to send messages, the wearable app must support receiving these messages.

  1. Refer to Querying Available Wearable Devices to obtain the paired device list and select the device for communications.
  2. Create a WESIdentityInfo object for the message sender, and specify the phone app bundle name and fingerprint. The fingerprint is used to verify the app information on the watch side. iOS apps do not have fingerprint information. You can enter any text as the fingerprint.
  3. Create a WESIdentityInfo object for the message recipient, and specify the wearable app bundle name and fingerprint.
  1. Create a WESP2PMessage object to construct the message to be sent.
  2. Create a WESP2PData object to configure whether the message is of the TLV format.
  3. Call the init method of TLVDataCmm to construct data. Create a TLVDataCmm object and construct data in TLV format.
  4. Assign the data in TLV format to the message.
  5. Call the getP2PClient method in HiWear to obtain the P2PClient object. Call the send method to send short messages from your phone app to the wearable app.

    • The callback parameter is used to return the message sending result.
    • The WearEngineServerErrorCode status parameter is used to return the sending result. WESErrorCodeCommSuccess indicates success, and other error codes indicate failure.
    @objc func sendDataBtnAction() {
        sendDataBtn.isSelected = !sendDataBtn.isSelected
        // Model conversion.
        let srcInfo = WESIdentityInfo()
        srcInfo.mPackageName = "XXXX" // Bundle name of the source app.
        srcInfo.mFingerPrint = "YYYY" // Fingerprint of the source app.
     
        let destInfo = WESIdentityInfo();
        destInfo.mPackageName = "XXXX" // Bundle name of the target app.
        destInfo.mFingerPrint = "YYYY" // Fingerprint of the target app.
     
        let message = WESP2PMessage();
        message.type = WESP2PMessageType.data;
     
        let wesP2PData = WESP2PData()
        wesP2PData.isTLVFormat = false
     
        let unitIsOpen = "XXXX" // Transmitted data.
        let tlv = TLVDataCmm.init(0, value: unitIsOpen.data(using: .utf8))
        let tlvData = tlv?.value ?? NSMutableData()
     
        let rootCmm = TLVDataCmm(type: 0x07, withLen: Int32(tlvData.count))
        let rootCmmData = rootCmm?.tlvToData() ?? Data()
     
        let tmpPayloadData = NSMutableData.init(data: rootCmmData)
        tmpPayloadData.append(tlvData as Data)
        wesP2PData.payloadData = tmpPayloadData.copy() as? Data
        message.p2pData = wesP2PData.copy() as? WESP2PData
        HiWear.getP2PClient().send(self.currentDevice, message: message, srcInfo: srcInfo, 
            destInfo: destInfo) { status, progress in
            if status == .WESErrorCodeCommSuccess {
                // P2P transmission is successful.
            } else {
                // P2P transmission fails. Error code: status.rawValue
            }
        }
    }

Receiving P2P Messages Sent from Your Wearable App

Create a listener for the P2P message sent from your wearable app.

  1. Refer to Querying Available Wearable Devices to obtain the paired device list and select the device for communications.
  2. Create a WESIdentityInfo object for the message sender, and specify the wearable app bundle name and fingerprint.
  3. Create a WESIdentityInfo object for the message recipient, and specify the phone app bundle name and fingerprint.
  4. Call the getP2pClient method in HiWear to obtain the P2pClient object, and call registerReceiver to register a listener.

    let srcInfo = WESIdentityInfo()
    srcInfo.mPackageName = "XXXX" // Bundle name of the source app.
    srcInfo.mFingerPrint = "YYYY" // Fingerprint of the source app.
    let destInfo = WESIdentityInfo()
    destInfo.mPackageName = "XXXX" // Bundle name of the target app.
    destInfo.mFingerPrint = "YYYY" // Fingerprint of the target app.
    receiverCallback = {status, message in
        if status != .WESErrorCodeSuccess {
            // Fails to register the listener. Error code: status.rawValue
            return
        }
        if (status == .WESErrorCodeSuccess && message == nil) {
            // The listener is successfully registered. Package name: destInfo.mPackageName; fingerprint: destInfo.mFingerPrint
        }
        if (message != nil) {
            let response = message?.data
            let responseString :String = String.init(data: response ?? Data() , encoding: .utf8) ?? ""
            if (responseString.count > 0) {
                // Received P2P message: responseString
            } else {
                let string = message?.data.toHexString() ?? ""
                // Received P2P message: string
            }
        }}
    HiWear.getP2PClient().registerReceiver(currentDevice, srcInfo: srcInfo, destInfo: destInfo, receiverCallback: receiverCallback)

Unregistering the Listener for P2P Messages on Wearable Devices

Unregister the listener for P2P messages.

  1. Refer to Querying Available Wearable Devices to obtain the paired device list and select the device for communications.
  2. Create a WESIdentityInfo object for the message sender, and specify the wearable app bundle name and fingerprint.
  3. Create a WESIdentityInfo object for the message recipient, and specify the phone app bundle name and fingerprint.
  4. Call the getP2pClient method in HiWear to obtain the P2pClient object, and call unRegisterReceiver to unregister the listener.

    let srcInfo = WESIdentityInfo()
    srcInfo.mPackageName = "XXXX" // Bundle name of the source app.
    srcInfo.mFingerPrint = "YYYY" // Fingerprint of the source app.
    let destInfo = WESIdentityInfo()
    destInfo.mPackageName = "XXXX" // Bundle name of the target app.
    destInfo.mFingerPrint = "YYYY" // Fingerprint of the target app.
    HiWear.getP2PClient().unRegisterReceiver(currentDevice, srcInfo: srcInfo, destInfo: destInfo) { status, message in
        if status != .WESErrorCodeSuccess {
            // Fails to unregister the listener. Error code: status.rawValue
            return
        }
        if (status == .WESErrorCodeSuccess) {
            // The listener is successfully unregistered. Package name: destInfo.mPackageName; fingerprint: destInfo.mFingerPrint
        }
    }

NOTE

The iOS Wear Engine SDK only supports P2P message communications between the phone and the watch, but not file transfers.

Search
Enter a keyword.