Skip to content

Commit

Permalink
pirateship detectrpi in header (fixes #14) (fixes #22) (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
dogi authored Mar 29, 2018
1 parent 473b3b8 commit 5a58463
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 118 deletions.
149 changes: 79 additions & 70 deletions app/src/main/java/io/treehouses/remote/BluetoothChatFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -502,77 +502,90 @@ public void run() {
@Override
public void handleMessage(Message msg) {
FragmentActivity activity = getActivity();
switch (msg.what) {
case Constants.MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case BluetoothChatService.STATE_CONNECTED:
setStatus(getString(R.string.title_connected_to, mConnectedDeviceName));
mConversationArrayAdapter.clear();
break;
case BluetoothChatService.STATE_CONNECTING:
setStatus(R.string.title_connecting);
break;
case BluetoothChatService.STATE_LISTEN:
case BluetoothChatService.STATE_NONE:
setStatus(R.string.title_not_connected);
break;
}
break;
case Constants.MESSAGE_WRITE:
isRead = false;
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
Log.d(TAG, "writeMessage = " + writeMessage);
mConversationArrayAdapter.add("Command: " + writeMessage);

break;
case Constants.MESSAGE_READ:
isRead = true;
switch (msg.what) {
case Constants.MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case BluetoothChatService.STATE_CONNECTED:
setStatus(getString(R.string.title_connected_to, mConnectedDeviceName));
try {
Thread.sleep(500);
String[] firstRun = {"cd boot\n", "cat version.txt\n", "pirateship detectrpi\n", "cd ..\n"};
for (int i = 0; i <= 3; i++) {
mChatService.write(firstRun[i].getBytes());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
mConversationArrayAdapter.clear();
mConversationArrayAdapter.notifyDataSetChanged();
break;
case BluetoothChatService.STATE_CONNECTING:
setStatus(R.string.title_connecting);
break;
case BluetoothChatService.STATE_LISTEN:
case BluetoothChatService.STATE_NONE:
setStatus(R.string.title_not_connected);
break;
}
break;
case Constants.MESSAGE_WRITE:
isRead = false;
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
Log.d(TAG, "writeMessage = " + writeMessage);
mConversationArrayAdapter.add("Command: " + writeMessage);

break;
case Constants.MESSAGE_READ:
isRead = true;
// byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
// String readMessage = new String(readBuf, 0, msg.arg1);
// String readMessage = new String(readBuf);
String readMessage = (String)msg.obj;
Log.d(TAG, "readMessage = " + readMessage);
//TODO: if message is json -> callback from RPi
if(isJson(readMessage)){
handleCallback(readMessage);
}else{
if(isCountdown){
mHandler.removeCallbacks(watchDogTimeOut);
isCountdown = false;
String readMessage = (String) msg.obj;
Log.d(TAG, "readMessage = " + readMessage);
//TODO: if message is json -> callback from RPi
if (isJson(readMessage)) {
handleCallback(readMessage);
} else {
if (isCountdown) {
mHandler.removeCallbacks(watchDogTimeOut);
isCountdown = false;
}
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
Toast.makeText(activity, R.string.config_alreadyConfig, Toast.LENGTH_SHORT).show();
}
if (hProgressDialog.isShowing()) {
hProgressDialog.dismiss();
Toast.makeText(activity, R.string.config_alreadyConfig_hotspot, Toast.LENGTH_SHORT).show();
}
//remove the space at the very end of the readMessage -> eliminate space between items
readMessage = readMessage.substring(0, readMessage.length() - 1);
//mConversationArrayAdapter.add(mConnectedDeviceName + ": " + readMessage);
mConversationArrayAdapter.add(readMessage);
}
if(mProgressDialog.isShowing()){
mProgressDialog.dismiss();
Toast.makeText(activity, R.string.config_alreadyConfig, Toast.LENGTH_SHORT).show();

break;
case Constants.MESSAGE_DEVICE_NAME:
// save the connected device's name
mConnectedDeviceName = msg.getData().getString(Constants.DEVICE_NAME);
if (null != activity) {
Toast.makeText(activity, "Connected to "
+ mConnectedDeviceName, Toast.LENGTH_SHORT).show();
}
if(hProgressDialog.isShowing()){
hProgressDialog.dismiss();
Toast.makeText(activity, R.string.config_alreadyConfig_hotspot, Toast.LENGTH_SHORT).show();
break;
case Constants.MESSAGE_TOAST:
if (null != activity) {
Toast.makeText(activity, msg.getData().getString(Constants.TOAST),
Toast.LENGTH_SHORT).show();
}
//remove the space at the very end of the readMessage -> eliminate space between items
readMessage = readMessage.substring(0,readMessage.length()-1);
//mConversationArrayAdapter.add(mConnectedDeviceName + ": " + readMessage);
mConversationArrayAdapter.add(readMessage);
}

break;
case Constants.MESSAGE_DEVICE_NAME:
// save the connected device's name
mConnectedDeviceName = msg.getData().getString(Constants.DEVICE_NAME);
if (null != activity) {
Toast.makeText(activity, "Connected to "
+ mConnectedDeviceName, Toast.LENGTH_SHORT).show();
}
break;
case Constants.MESSAGE_TOAST:
if (null != activity) {
Toast.makeText(activity, msg.getData().getString(Constants.TOAST),
Toast.LENGTH_SHORT).show();
}
break;
}
break;
}
//} catch (InterruptedException e) {
// e.printStackTrace();
//}
}
};

Expand Down Expand Up @@ -734,7 +747,6 @@ public void showNWifiDialog() {
dialogFrag.setTargetFragment(this, REQUEST_DIALOG_FRAGMENT);
dialogFrag.show(getFragmentManager().beginTransaction(), "dialog");


}

public void showHotspotDialog(){
Expand Down Expand Up @@ -774,16 +786,13 @@ public void handleCallback(String str){
if(!result.equals("SUCCESS")){
Toast.makeText(getActivity(), R.string.config_fail,
Toast.LENGTH_LONG).show();
}else{
// Toast.makeText(getActivity(), R.string.config_success,
// Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(),getString(R.string.config_success) + ip,Toast.LENGTH_LONG).show();
}
}else{ Toast.makeText(getActivity(),getString(R.string.config_success) + ip,Toast.LENGTH_LONG).show(); }

}catch (JSONException e){
// error handling
Toast.makeText(getActivity(), "SOMETHING WENT WRONG", Toast.LENGTH_LONG).show();
}

}

}
89 changes: 42 additions & 47 deletions app/src/main/java/io/treehouses/remote/BluetoothChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
* Created by yubo on 7/11/17.
*/

import android.app.ActionBar;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import java.io.IOException;
Expand Down Expand Up @@ -71,6 +72,9 @@ public class BluetoothChatService {
private ConnectedThread mConnectedThread;
private int mState;
private int mNewState;
private FragmentActivity mActivity;
private String HWver = "";
private String SWver = "";


// Constants that indicate the current connection state
Expand All @@ -82,14 +86,15 @@ public class BluetoothChatService {
/**
* Constructor. Prepares a new BluetoothChat session.
*
* @param context The UI Activity Context
* @param activity The BluetoothCharFragment Activity
* @param handler A Handler to send messages back to the UI Activity
*/
public BluetoothChatService(Context context, Handler handler) {
public BluetoothChatService(FragmentActivity activity, Handler handler) {
mAdapter = BluetoothAdapter.getDefaultAdapter();
mState = STATE_NONE;
mNewState = mState;
mHandler = handler;
mActivity = activity;
}

/**
Expand Down Expand Up @@ -501,47 +506,10 @@ public void run() {
byte[] buffer = new byte[1024];
int bytes = -1;
String out = "";
// List<String> tempOutputList = new ArrayList<String>();
// Keep listening to the InputStream while connected
// while (mState == STATE_CONNECTED) {
// try {
// // Read from the InputStream
// //bytes = mmInStream.read(buffer);
// //Log.d(TAG, "bytes = " + bytes + ", buffer = " + buffer);
//
// while(true){
// bytes = mmInStream.read(buffer);
// out += new String(buffer, 0, bytes);
// if(bytes < 1024){
// break;
// }
// }
// tempOutputList = getTokens("[a-zA-Z._]+", out);
// HashSet<String> mSet = new HashSet<String>();
// for(String s : tempOutputList){
// if(!mSet.contains(s)){
// mSet.add(s);
// }
// }
//
// System.out.println("mSet = " + mSet);
//
//
// Log.d(TAG, "out = " + out + "size of out = " + out.length());
// Log.d(TAG, "tempOutputList = " + mSet.iterator().next());
//
//
// // Send the obtained bytes to the UI Activity
// mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
// .sendToTarget();
// } catch (IOException e) {
// Log.e(TAG, "disconnected", e);
// connectionLost();
// break;
// }
// }

// Keep listening to the InputStream while connected
boolean getSWString = false;
boolean getHWString = false;
boolean alreadyExecutedDisplay = false;

while (true) {
try {
// Read from the InputStream
Expand All @@ -550,11 +518,38 @@ public void run() {
Log.d(TAG, "out = " + out + "size of out = " + out.length() + ", bytes = " + bytes);
mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, out)
.sendToTarget();
// mEmulatorView.write(buffer, bytes);
// Send the obtained bytes to the UI Activity
//mHandler.obtainMessage(BlueTerm.MESSAGE_READ, bytes, -1, buffer).sendToTarget();

// Get the SW version once
if(out.contains("release-") && !getSWString) {
SWver += "Version: " + out.substring(8, 10);
getSWString = true;
}

// Get the HW version once
if(out.contains("RPI") && !getHWString){
HWver += "; " + out;
getHWString = true;
}

// Display SW/HW version after getting all the String from above.
if(!alreadyExecutedDisplay && getSWString && getHWString) {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
final ActionBar actionBar = mActivity.getActionBar();
if (null == actionBar) { return; }
Log.d(TAG, "actionBar.setSubtitle(subTitle) = " + SWver + HWver);
actionBar.setSubtitle(SWver + HWver);
}
});
//Set everything back to default state
alreadyExecutedDisplay = true;
}

} catch (IOException e) {
Log.e(TAG, "disconnected", e);
HWver = "";
SWver = "";
connectionLost();
break;
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/io/treehouses/remote/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ public interface Constants {
// Key names received from the BluetoothChatService Handler
String DEVICE_NAME = "device_name";
String TOAST = "toast";

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public void onShow(DialogInterface dialog) {
mSSIDEditText.setError(getString(R.string.error_ssid_empty));
}
});

}

protected AlertDialog getAlertDialog(View mView) {
Expand Down

0 comments on commit 5a58463

Please sign in to comment.