Skip to content

Commit

Permalink
バグ直し 三つ巴問題とIntentスタック問題とその他いろいろ
Browse files Browse the repository at this point in the history
  • Loading branch information
NewNotMoon committed Mar 3, 2016
1 parent a6ff077 commit 58fcd4c
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 225 deletions.
3 changes: 2 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 26 additions & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -113,76 +113,76 @@ public void doClose() {
* @param msg 送信メッセージ.
*/
public void doSend(String msg) {
byte[] buffer_ = new byte[msg.length() / 2 + 1];
for (int i = 0; i < msg.length(); ++i) {
int tmp;
switch (msg.charAt(i)) {
case '0':
tmp = 0x01;
break;
case '1':
tmp = 0x02;
break;
case '2':
tmp = 0x03;
break;
case '3':
tmp = 0x04;
break;
case '4':
tmp = 0x05;
break;
case '5':
tmp = 0x06;
break;
case '6':
tmp = 0x07;
break;
case '7':
tmp = 0x08;
break;
case '8':
tmp = 0x09;
break;
case '9':
tmp = 0x0a;
break;
case '-':
tmp = 0x0b;
break;
case '.':
tmp = 0x0c;
break;
case ',':
tmp = 0x0d;
break;
default:
tmp = 0x00;
break;
}
if (i % 2 == 0) {
buffer_[i / 2] = (byte) (tmp << 4);
} else {
buffer_[i / 2] += (byte) tmp;
}
}
try {
btOut.write(buffer_);
if( await_ ) {
btOut.write('\n');
if( !msg.isEmpty() ){

byte[] buffer_ = new byte[msg.length() / 2 + 1];
for (int i = 0; i < msg.length(); ++i) {
int tmp;
switch (msg.charAt(i)) {
case '0':
tmp = 0x01;
break;
case '1':
tmp = 0x02;
break;
case '2':
tmp = 0x03;
break;
case '3':
tmp = 0x04;
break;
case '4':
tmp = 0x05;
break;
case '5':
tmp = 0x06;
break;
case '6':
tmp = 0x07;
break;
case '7':
tmp = 0x08;
break;
case '8':
tmp = 0x09;
break;
case '9':
tmp = 0x0a;
break;
case '-':
tmp = 0x0b;
break;
case '.':
tmp = 0x0c;
break;
case ',':
tmp = 0x0d;
break;
default:
tmp = 0x00;
break;
}
if (i % 2 == 0) {
buffer_[i / 2] = (byte) (tmp << 4);
} else {
buffer_[i / 2] += (byte) tmp;
}
}
btOut.write(buffer_);
if( await_ ) {
btOut.write('\n');
}
}
else {
new SendTask().execute(msg);

if( !await_ ) {
new SendTask().execute();
}
} catch (Throwable t) {
doClose();
}
}

public void doReceive(){
new ReceiveTask().execute();
}

/**
* Bluetoothと接続を開始する非同期タスク。
* - 時間がかかる場合があるのでProcessDialogを表示する。
Expand Down Expand Up @@ -230,9 +230,6 @@ protected Object doInBackground(Void... params) {
try {
btOut.close();
} catch (Throwable t) {/*ignore*/}
try {
btIn.close();
} catch (Throwable t) {/*ignore*/}
bluetoothSocket.close();
} catch (Throwable t) {
return t;
Expand All @@ -252,35 +249,17 @@ protected void onPostExecute(Object result) {
/**
* サーバとメッセージの送受信を行う非同期タスク。
*/
private class SendTask extends AsyncTask<String, Void, Void> {
private class SendTask extends AsyncTask<String, Void, Object> {
@Override
protected Void doInBackground(String... params) {
protected Object 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];
int len = btIn.read(buff);

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

Expand All @@ -290,20 +269,24 @@ protected void onPostExecute(Object result){
Log.e(TAG, result.toString(), (Throwable) result);
activity.errorDialog(result.toString());
} else {
String r_ = result.toString();
if( r_.equals("\0") ){
//Log.i("test","empty message");
if( result == null ){
Log.e("test","disconnect");
} else {
// 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");
String r_ = result.toString();
if( r_.equals("\0") ){
//Log.i("test","empty message");
} else {
// 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", scene_name_);
}
}
await_ = false;
}
await_ = false;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
* Created by prprhyt on 2016/02/21.
*/
public class GlobalVariables extends Application {
final static String DATA_PATH = "/datapath";
final static String DATA_PATH = "/abc2016cdata";
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.bluetooth.BluetoothDevice;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
Expand Down Expand Up @@ -48,7 +49,7 @@ public class MainActivity extends AppCompatActivity implements GoogleApiClient.C
private String resultstr;

private boolean connected_;

private Handler _handler = new Handler();

private GlobalVariables globalv;

Expand All @@ -66,7 +67,17 @@ protected void onCreate(Bundle savedInstanceState) {
mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addApi(Wearable.API).build();
resultstr="";

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
_handler.postDelayed(new Runnable() {//Timer的な
@Override
public void run() {
if( connected_ ){
SendSensorNum("");
}
_handler.postDelayed(this, 50);
}
}, 50);
SyncData("scene","title");
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand Down Expand Up @@ -275,7 +286,7 @@ public void SyncData(String key_name,String sync_data){//HandheldとWear間の
pendingResult.setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
@Override
public void onResult(DataApi.DataItemResult dataItemResult) {
Log.d("TAG", "onResult:" + dataItemResult.getStatus().toString());
Log.d("test", "onResult:" + dataItemResult.getStatus().toString());
}
});

Expand Down Expand Up @@ -305,8 +316,6 @@ public boolean onOptionsItemSelected(MenuItem item) {

public void SendSensorNum(String sendstr){
bluetoothClient.doSend(sendstr);
bluetoothClient.doReceive();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
*/
public class GlobalVariables extends Application {
GoogleApiClient mGoogleApiClient;
final static String DATA_PATH = "/datapath";
final static String DATA_PATH = "/abc2016cdata";
}

Loading

1 comment on commit 58fcd4c

@NewNotMoon
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

見つかった問題点、修正点
・そもそもDataApiのリスナー登録をしていない→登録
・送信部と受信部を結合
・三つ巴デッドロック→ハンドラに→仮にセンサデータがなくても定期的に空パケ送信して受診に行くようにした
・onDataChangedが最初呼ばれない→ダミーの更新を1回入れた→changedだから作成は含まれない…?
・onDataChangedはパス単位での通知なのでどのキーが更新されたかはわかりませんぞ→どの更新なのか判別するならパスを変えるしかない→めんどいので気にしない
・ハンドラが停止しない→そもそもonStopもonDestroyもon(ryも呼ばれてない→IntentしてもActivityはスタックにたまるだけで消えません
・無言落ち→メモリ不足→Activityがスタックに溜まり続けるから
・Intentフラグを変え、常にスタッククリーンするようにした
・GoogleAPIClientのリスナーは1つしか受け付けない模様(スタックの関係で消えることもない)→登録してもコールバックが呼ばれないので悩んだ→めんどいのでメインActivity以外はリスナー登録しないようにした

Please sign in to comment.