Skip to content

Commit

Permalink
Handheld<->サーバーの送信部分と受信部分を分離した
Browse files Browse the repository at this point in the history
  • Loading branch information
Hayato Kimura committed Mar 3, 2016
1 parent d7a546e commit a6ff077
Showing 1 changed file with 21 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,29 @@ protected void onPostExecute(Object result) {
/**
* サーバとメッセージの送受信を行う非同期タスク。
*/
private class SendTask extends AsyncTask<String, Void, Object> {
private class SendTask extends AsyncTask<String, Void, Void> {
@Override
protected Object doInBackground(String... params) {
protected Void doInBackground(String... params) {
try {
btOut.flush();
return null;
} catch (Throwable t) {
doClose();
return null;
}
}

@Override
protected void onPostExecute(Void result) {
Log.d("BluetoothInfo","Sending is Success");
}
}

private class ReceiveTask extends AsyncTask<Void, Void, Object> {
@Override
protected Object doInBackground(Void... params) {
try {
byte[] buff = new byte[512];
await_ = true;
int len = btIn.read(buff);

return new String(buff, 0, len);
Expand All @@ -270,7 +285,7 @@ protected Object doInBackground(String... params) {
}

@Override
protected void onPostExecute(Object result) {
protected void onPostExecute(Object result){
if (result instanceof Exception) {
Log.e(TAG, result.toString(), (Throwable) result);
activity.errorDialog(result.toString());
Expand All @@ -279,52 +294,18 @@ protected void onPostExecute(Object result) {
if( r_.equals("\0") ){
//Log.i("test","empty message");
} else {
// TODO:Wearに転送する処理
// TODO:readyやvibratorの転送.
Pattern p = Pattern.compile("scene:([^\n\0\r]*)");
Matcher m = p.matcher(r_);
if( m.find() ){
String scene_name_ = m.group(1);
Toast.makeText(activity,"scene is changed"+scene_name_,Toast.LENGTH_SHORT).show();//とりあえずトーストで表示
activity.SyncData(scene_name_, "scene_name");
}
}
await_ = false;
}
}
}

private class ReceiveTask extends AsyncTask<String, Void, Object> {
@Override
protected Object doInBackground(String... params) {
try {
byte[] buff = new byte[512];
int len = btIn.read(buff);

return new String(buff, 0, len);
} catch (Throwable t) {
doClose();
return t;
}
}

@Override
protected void onPostExecute(Object result) {
if (result instanceof Exception) {
Log.e(TAG, result.toString(), (Throwable) result);
activity.errorDialog(result.toString());
} else {
// 結果を画面に反映。
//activity.doSetResultText(result.toString());
String[] detection_code = result.toString().split(":", 0);
if(detection_code[0].equals("scene")){
activity.SyncData(detection_code[1], "scene_name");
}else if(detection_code[0].equals("ready")){
activity.SyncData(detection_code[1],detection_code[0]);
}else if(detection_code[0].equals("vibrator")){
activity.SyncData(detection_code[1],detection_code[0]);
}

}
}
}
}

0 comments on commit a6ff077

Please sign in to comment.