如何使用 MessageApi 将 Bundle 传递给 Android Wear
How to pass a Bundle to Android Wear using MessageApi
我目前正在使用以下方式将字节传递给 AnroidWear:
MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
mGoogleApiClient, node.getId(), path, text.getBytes() ).await();
我想向我的可穿戴设备发送适当的数据包,我该怎么做?
将您的包转换为字节,然后在接收方将字节转换为包。
使用这种简单方法,您只能发送String Bundle值。
(而且,您似乎也可以使用 GSON 和 BundleTypeAdapterFactory 将 Bundle 转换为 String,但我没有测试过。)
public void example() {
// Value
Bundle inBundle = new Bundle();
inBundle.putString("key1", "value");
inBundle.putInt("key2", 1); // will be failed
inBundle.putFloat("key3", 0.5f); // will be failed
inBundle.putString("key4", "this is key4");
// From Bundle to bytes
byte[] inBytes = bundleToBytes(inBundle);
// From bytes to Bundle
Bundle outBundle = jsonStringToBundle(new String(inBytes));
// Check values
String value1 = outBundle.getString("key1"); // good
int value2 = outBundle.getInt("key2"); // fail
float value3 = outBundle.getFloat("key3"); // fail
String value4 = outBundle.getString("key4"); // good
}
private byte[] bundleToBytes(Bundle inBundle) {
JSONObject json = new JSONObject();
Set<String> keys = inBundle.keySet();
for (String key : keys) {
try {
json.put(key, inBundle.get(key));
} catch (JSONException e) {
//Handle exception here
}
}
return json.toString().getBytes();
}
public static Bundle jsonStringToBundle(String jsonString) {
try {
JSONObject jsonObject = new JSONObject(jsonString);
return jsonToBundle(jsonObject);
} catch (JSONException ignored) {
}
return null;
}
public static Bundle jsonToBundle(JSONObject jsonObject) throws JSONException {
Bundle bundle = new Bundle();
Iterator iter = jsonObject.keys();
while (iter.hasNext()) {
String key = (String) iter.next();
String value = jsonObject.getString(key);
bundle.putString(key, value);
}
return bundle;
}
我目前正在使用以下方式将字节传递给 AnroidWear:
MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
mGoogleApiClient, node.getId(), path, text.getBytes() ).await();
我想向我的可穿戴设备发送适当的数据包,我该怎么做?
将您的包转换为字节,然后在接收方将字节转换为包。
使用这种简单方法,您只能发送String Bundle值。
(而且,您似乎也可以使用 GSON 和 BundleTypeAdapterFactory 将 Bundle 转换为 String,但我没有测试过。)
public void example() {
// Value
Bundle inBundle = new Bundle();
inBundle.putString("key1", "value");
inBundle.putInt("key2", 1); // will be failed
inBundle.putFloat("key3", 0.5f); // will be failed
inBundle.putString("key4", "this is key4");
// From Bundle to bytes
byte[] inBytes = bundleToBytes(inBundle);
// From bytes to Bundle
Bundle outBundle = jsonStringToBundle(new String(inBytes));
// Check values
String value1 = outBundle.getString("key1"); // good
int value2 = outBundle.getInt("key2"); // fail
float value3 = outBundle.getFloat("key3"); // fail
String value4 = outBundle.getString("key4"); // good
}
private byte[] bundleToBytes(Bundle inBundle) {
JSONObject json = new JSONObject();
Set<String> keys = inBundle.keySet();
for (String key : keys) {
try {
json.put(key, inBundle.get(key));
} catch (JSONException e) {
//Handle exception here
}
}
return json.toString().getBytes();
}
public static Bundle jsonStringToBundle(String jsonString) {
try {
JSONObject jsonObject = new JSONObject(jsonString);
return jsonToBundle(jsonObject);
} catch (JSONException ignored) {
}
return null;
}
public static Bundle jsonToBundle(JSONObject jsonObject) throws JSONException {
Bundle bundle = new Bundle();
Iterator iter = jsonObject.keys();
while (iter.hasNext()) {
String key = (String) iter.next();
String value = jsonObject.getString(key);
bundle.putString(key, value);
}
return bundle;
}