手表通讯协议 - tomoon/sdk GitHub Wiki
本SDK为土曼手表app和土曼手机app之间相互通信使用,SDK提供了3个jar包: https://github.com/tomoon/sdk/tree/master/libs
-
tmwatchsdk.jar 基础包,所有应用都要用到
-
tmlibwatch.jar 手表app开发需要用到
-
tmlibphone.jar 手机app开发需要用到
做具体开发时,把对应的包加入到eclipse工程目录下的libs目录即可。例如如果你要开发手表app,就把tmwatchsdk.jar和tmlibwatch.jar拷贝到libs目录;要开发手机app,就把tmwatchsdk.jar和tmlibphone.jar拷贝到libs目录
手机app和手表app之间需要通过蓝牙实现信息交流,例如手表app发送请求,通过蓝牙传送到手机app,手机app接收后,通过蓝牙返回结果给手表app。
开发者不用关心蓝牙通信的细节,sdk已经为你完成所有的这些工作。我们通过android的广播机制来实现API,你只需写几行代码,就可以实现app之间通信。
我们的sdk提供了几个示例程序,以供参考。
注意:
如果你使用手表模拟器开发,在手表程序的 AndroidMainfest.xml 文件application标签 中请加入如下项:
<meta-data android:name="is_tomoon_app" android:value="true" />
这样在土曼模拟器中可以被识别。(为实际手表开发的程序不需要这个标记, 但我们建议你还是加上它)。
手表程序通过 TMWatchSender 工具类发送请求,通过 TMWatchReceiver 来接收回传的请求结果。
TMWatchSender工具类中提供如下方法:
-
static public void sendHttpRequest(Context ctx,int transId, String url)
获取某个http url返回的字符串,此方法只支持http get请求,并且url必须返回字符串(可以压缩)。 transId是事务id,每个请求都可以对应一个事务,事务id由程序指定(可以为-1,表示不关心事务id)。返回的消息中将带回此id。
-
public static void sendAppRequest(Context ctx,int transId, String targetPkg, JSONObject json)
向手机上第三方应用发送请求,具体的请求内容由json指定,而且必须知道手机上第三方应用的包名。同样也可以指定事务id。
-
public static void registerReceiver(Context ctx, TMWatchReceiver recv)
注册应答监听器,监听器用来接收请求的回传结果。
手表app的监听器需要继承自 TMWatchReceiver ,并重载 onHttpResponse 和 onAppResponse 2个方法,用以接收http请求结果和第三方应用的请求结果。
-
protected void onHttpResponse(Context ctx, int status, JSONObject json);
处理收到Http请求, status 是返回的状态码,状态码有很多种,定义在 TMWatchConstant.STATUS_XXX 中,例如蓝牙连接失败,http请求失败,没有安装第三方包等等。
-
protected void onAppResponse(Context ctx, int status,int transId, JSONObject json);
处理3方应用返回的结果。status也是状态码。transId是返回的的事务Id。
你可以在程序中使用 TMWatchSender.registerReceiver 动态注册监听器(注意动态注册监听器后请在不需要监听器的时候调用context.unregisteReceiver(receiver)来解除监听器),你也可以在AndroidMainfest.xml中定义一个永久监听器,定义格式如下:
<receiver android:name=”<你的监听器类名>”>
<intent-filter>
<android:name=”<你的程序包名>.response” />
</intent-filter>
</receiver>
即过滤器的Intent Action字符串为:你的包名+”.response”(例如com.tomoon.sample.response)
具体使用请查看WatchSample中的TestHttpActivity。
要接收手表程序的请求,手机app首先需要注册手表请求的的监听器,你需要继承类 TMPhoneReceiver 并实现 onAppRequest 方法:
-
protected abstract void onAppRequest(Context ctx, String senderPackage, int transId, JSONObject json)
senderPackage是请求发送程序的包名,json是具体发送的字符串。transId是返回的的事务Id。
同样的,你可以在程序中使用TMMobileSender.registerReceiver动态注册监听器(在不需要监听器的时候调用context.unregisteReceiver(receiver)来解除监听器),你也可以在AndroidMainfest.xml中定义一个永久监听器,定义格式如下:
<receiver android:name=”<你的监听器类名>”>
<intent-filter>
<android:name=”<你的程序包名>.request” />
</intent-filter>
</receiver>
即过滤器的Intent Action字符串为:你的程序包名+”.request”(例如com.tomoon.sample.request)
手机端发送应答的功能的功能封装在TMPhoneSender工具类中,处理完手表请求后,通过TMPhoneSender.sendAppResponse来向手表端发送应答。
-
TMMobileSender.sendAppResponse(Context ctx, String targetPackage , JSONObject json);
targetPackage是接收消息的手表程序的包名。
TMPhoneSender.sendAppResponse并不需要在接收到手表请求后才发送,即手机端主可以主动向手表程序发送信息,前提是你知道手表程序的包名,并且手表程序要有对应的监听器来接收。
具体使用请查看WatchSample中的TestNotificationActivity。
PhoneSample和WatchSample例子提供了完整的手表与手机的通信。手表端向手机端发送一个通知请求,然后手机端每秒向手表端发送一个通知,连续10次。手表端接收通知后在界面显示出来。PhoneSample是手机端程序。
手机端需要安装土曼App才能向手表端发送信息。因此手机端向手表端发送信息时,应先判断手机上是否安装了土曼的App,具体请查看PhoneSample中的isTomoonAppInstalled方法。
我们的SDK对于pebble程序具有兼容性,如果你为pebble编写了手机端程序,那么不用修改一行代码,你的程序就可以和土曼手表app通信!(当然,你需要为土曼手表实现一个app:pebble的手表程序无法运行在土曼手表上)。
手表app要接收手机pebble app 的信息,首先需要设置一个uuid,在AndroidMainfest.xml的 Application标签中加入项:
<meta-data android:name="pebble-uuid" android:value="<你的app的uuid>" />
这个uuid 和你为pebble 手表开发的app uuid一致。 如果你还没有开发pebble手表的app,这个网站可以帮你自动生成一个uuid: http://www.uuidgenerator.net/ (我们建议你先开发pebble手表的app)
有了uuid后,只要实现 TMWatchReceiver 的如下方法即可接收pebble的信息
-
protected void onPebbleData(Context ctx,int status, int transId, String jsonData) ;
接收到pebble 数据。status是状态码, (对于pebble而言,状态码可以忽略,目前没有意义),transId是pebble事务码,由pebble程序定义。
-
protected void onPebbleAck(Context ctx,int status, int transId);
接收到pebble ack
-
protected void onPebbleNack(Context ctx,int status, int transId) ;
接收到pebble nak
通过 TMWatchSender 中的几个方法可以向手机pebble app 发送回应信息
-
public static boolean sendPebbleMessage(Context ctx, UUID uuid,int transId, PebbleDictionary data);
发送数据。
-
public static boolean sendPebbleAck(Context ctx, UUID uuid, int transId) ;
发送ack
-
public static boolean sendPebbleNack(Context ctx, UUID uuid, int transId) ;
发送nack
####可使用的pebble通讯api:
Send pre-formatted notifications to be displayed by Pebble’s built-in notification app.
com.getpebble.action.SEND_NOTIFICATION
String messageType – An enum that identifies the type of message being sent to the Pebble
String sender – A String that uniquely identifies the application sending a message to Pebble
String notificationData – A JSON representation of the data specific to the messageType
//Sending a custom notification to Pebble
public void sendAlertToPebble() {
final Intent i = new Intent("com.getpebble.action.SEND_NOTIFICATION");
final Map data = new HashMap();
data.put("title", "Test Message");
data.put("body", "Whoever said nothing was impossible never tried to slam a revolving door.");
final JSONObject jsonData = new JSONObject(data);
final String notificationData = new JSONArray().put(jsonData).toString();
i.putExtra("messageType", "PEBBLE_ALERT");
i.putExtra("sender", "MyAndroidApp");
i.putExtra("notificationData", notificationData);
Log.d(TAG, "About to send a modal alert to Pebble: " %2B notificationData);
sendBroadcast(i);
}
Send music metadata to be displayed by Pebble’s built-in music app.
com.getpebble.action.NOW_PLAYING
String artist – The song artist to be displayed
String album – The song album to be displayed
String track – The song title to be displayed
//Sending a music update to Pebble
public void sendMusicUpdateToPebble() {
final Intent i = new Intent("com.getpebble.action.NOW_PLAYING");
i.putExtra("artist", "Carly Rae Jepsen");
i.putExtra("album", "Kiss");
i.putExtra("track", "Call Me Maybe");
sendBroadcast(i);
}
This intent is broadcast when the Android device establishes a new Bluetooth connection to Pebble.
com.getpebble.action.PEBBLE_CONNECTED
String address – A String representation of Pebble’s Bluetooth MAC address.
//Listening for ‘Pebble connected’ messages
public class PebbleConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String pebbleAddress = intent.getStringExtra("address");
Log.i(TAG, "Pebble (%s) connected", pebbleAddress);
}
}
This intent is broadcast when the Android device loses its Bluetooth connection to Pebble.
com.getpebble.action.PEBBLE_DISCONNECTED
String address – A String representation of Pebble’s Bluetooth MAC address.
//Listening for ‘Pebble disconnected’ messages
public class PebbleConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String pebbleAddress = intent.getStringExtra("address");
Log.i(TAG, "Pebble (%s) disconnected", pebbleAddress);
}
}