AndroidWear Galaxy Gear 2で動かす
Samsungのサイトからサンプルプログラミングをダウンロードします。
Sample Applications | SAMSUNG Developers
最初なので、 Hello Accessoryをダウンロードしました。
http://developer.samsung.com/sample-app/view.do?v=S000000047#none
eclipseにHelloAccessoryProviderの方をimportして、実行!
Galaxy Gear2のアプリケーションからアプリを選択実行。
すると下記の画面が出て、connectー>fetchで時間が表示されてる様です。
HelloAccessoryProviderService.javaのソース(98行目)を読んでみると、
@Override public void onReceive(int channelId, byte[] data) { Time time = new Time(); time.set(System.currentTimeMillis()); String timeStr = " " + String.valueOf(time.minute) + ":" + String.valueOf(time.second); String strToUpdateUI = new String(data); final String message = strToUpdateUI.concat(timeStr); final HelloAccessoryProviderConnection uHandler = mConnectionsMap.get(Integer .parseInt(String.valueOf(mConnectionId))); if(uHandler == null){ return; } new Thread(new Runnable() { public void run() { try { uHandler.send(HELLOACCESSORY_CHANNEL_ID, message.getBytes()); } catch (IOException e) { e.printStackTrace(); } } }).start(); }
onReceiveの後、uHandler.sendで時間を返してるのがわかります。
HelloAccessoryConsumer(Gear側)のmain.jsを見てみると、
function fetch() { try { SASocket.setDataReceiveListener(onreceive); SASocket.sendData(CHANNELID, "Hello Accessory!"); } catch(err) { console.log("exception [" + err.name + "] msg[" + err.message + "]"); } }
fetchボタンでsendDataしてるのが分かります。
なるほど、こんな感じでやり取りするんですね。