Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
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.
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
}
} 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
}
} Before sending a P2P message, you can use the ping method to check whether your phone app can communicate with the wearable app.
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
}
} 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.
@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
}
}
} Create a listener for the P2P message sent from your wearable app.
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) Unregister the listener for P2P messages.
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
}
} The iOS Wear Engine SDK only supports P2P message communications between the phone and the watch, but not file transfers.