Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.

Send_en

Hengyu Liu edited this page Dec 20, 2017 · 2 revisions

We recommend you to read Google doc to learn the difference between Message and DataItem.

Send Message

You can call WTSender.sendMessage() anywhere to send Message. You can choose to pass in a SendMsgCallback to get the result.

String path = "/msg/test";
String content = "This is a Message";
WTSender.sendMessage(context, path, content, new WTSender.SendMsgCallback() {
		@Override
		public void onSuccess() {
			Log.i(TAG, "Send msg OK");
		}

		@Override
		public void onFailed(int resultCode) {
			Log.e(TAG, "Send msg failed");
		}
});

Notice: The success in return means the results for sending requests to Google Play Service or Mobvoi API, which does not assure you that users will receive the data.

Send DataItem (DataMap)

Because DataMap is more useful than DataItem, WearTools only supports sending structured DataMap, which can totally replace DataItem.

Similarly, you can call WTSender.sendData() to send DataMap. But you should call WTSender.getPutDataMapRequest() in advanced, to get PutDataMapRequest and add data to it.

String path = "/data/test";
String conetnt = "This is a dataMap."
PutDataMapRequest putDataMapRequest = WTSender.getPutDataMapRequest(path);
putDataMapRequest.getDataMap().putString("test",conetnt);
// Request the system to sync this DataMap right now, otherwise the system will sync when free to save energy.
putDataMapRequest.setUrgent();
// Send
WTSender.sendData(context, putDataMapRequest, new WTSender.SendDataCallback() {
	@Override
	public void onSuccess(Uri uri) {
		Log.i(TAG, "Send data OK");
	}

	@Override
	public void onFailed(int resultCode) {
		Log.e(TAG, "Send data failed");
	}
});

Notice: Like sending message, the return value from this func call only indicates the status of the request that was sent to Google Play or Mobvoi API.

Delete DataItem (DataMap)

Because DataItem (DataMap) is designed to sync data between phone and watch, it also provides delete func.

You can call WTSender.deleteData() anywhere to delete DataMap. You should store DataMap's URI in advanced by calling putDataMapRequest.getUri(). Here's a demo.

Uri mDataUri;
WTSender.deleteData(context, mDataUri, new WTSender.DeleteDataCallback() {
	@Override
  	public void onSuccess() {
		Log.i(TAG, "Del data OK");
	}

	@Override
	public void onFailed(int resultCode) {
		Log.e(TAG, "Del data failed");
	}
});

Notice: Delete and Send are two separate calls which cannot be counteract.

Send file

DataMap's size limit is 100KB. If you wanna transmit big date like pics, you should use asset, which should be paired up with DataMap. Here's a sending-pic demo.

try {
  	// Read pics from assets
	InputStream in = getResources().getAssets().open("image1.webp");
	int len = in.available();
	byte[] buffer = new byte[len];
	in.read(buffer);
	in.close();

	Asset asset = Asset.createFromBytes(buffer); // Create Asset
	PutDataMapRequest putDataMapRequest = WTSender.getPutDataMapRequest("/image");
	putDataMapRequest.setUrgent(); // Request the system to transmit right now
	putDataMapRequest.getDataMap().putAsset("image", asset); //将Asset绑定到DataMap
	Log.i(TAG, "Sending image data...");
	WTSender.sendData(context, putDataMapRequest, new WTSender.SendDataCallback() {
		@Override
		public void onSuccess(Uri uri) {
			Log.i(TAG, "Send image OK");
		}

		@Override
		public void onFailed(int resultCode) {
			Log.e(TAG, "Send image failed");
		}
	});
} catch (IOException e) {
	e.printStackTrace();
}

Notice: Theoretically, Asset is not limited by size, but it is limited by bluetooth bandwidth. We do not recommend you to transmit big files by this.

Clone this wiki locally