Skip to content

Commit

Permalink
display RPi internet status (fixes #24) (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
TerrenceEJ authored and xyb994 committed Apr 5, 2018
1 parent 154fd4f commit 6e836fd
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 11 deletions.
69 changes: 63 additions & 6 deletions app/src/main/java/io/treehouses/remote/BluetoothChatFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
Expand Down Expand Up @@ -85,6 +86,7 @@ public class BluetoothChatFragment extends android.support.v4.app.Fragment {
private Button Tbutton;
private Button Dbutton;
private Button Vbutton;
private Button Pbutton;
private Button HNbutton;
private Button CPbutton;
private Button EFbutton;
Expand Down Expand Up @@ -127,6 +129,17 @@ public void onCreate( Bundle savedInstanceState) {
// Get local Bluetooth adapter
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

//start pinging for wifi check
final Handler h = new Handler();
final int delay = 20000;
h.postDelayed(new Runnable(){
public void run(){
String ping = "ping -c 1 google.com";
sendPing(ping);
h.postDelayed(this, delay);
}
}, delay);

// If the adapter is null, then Bluetooth is not supported
if (mBluetoothAdapter == null) {
FragmentActivity activity = getActivity();
Expand Down Expand Up @@ -164,6 +177,7 @@ public void onDestroy() {
if (mChatService.getState() == BluetoothChatService.STATE_NONE) {
// Start the Bluetooth chat services
mChatService.start();
mIdle();
}
}
}
Expand All @@ -181,6 +195,7 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
Tbutton = (Button) view.findViewById(R.id.TB);
Dbutton = (Button)view.findViewById(R.id.DB);
Vbutton = (Button)view.findViewById(R.id.VB);
Pbutton = (Button)view.findViewById(R.id.PING);
HNbutton = (Button)view.findViewById(R.id.HN);
CPbutton = (Button) view.findViewById(R.id.CP);
EFbutton = (Button)view.findViewById(R.id.EF);
Expand Down Expand Up @@ -377,6 +392,7 @@ private void sendMessage(String message) {
// Check that we're actually connected before trying anything
if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
Toast.makeText(getActivity(), R.string.not_connected, Toast.LENGTH_SHORT).show();
mIdle();
return;
}

Expand All @@ -393,6 +409,13 @@ private void sendMessage(String message) {

}

private void sendPing(String ping) {
// Get the message bytes and tell the BluetoothChatService to write
byte[] pSend = ping.getBytes();
mChatService.write(pSend);

mOutStringBuffer.setLength(0);
}

private void sendMessage(String SSID, String PWD) {
// Check that we're actually connected before trying anything
Expand Down Expand Up @@ -512,6 +535,7 @@ public void handleMessage(Message msg) {
case BluetoothChatService.STATE_LISTEN:
case BluetoothChatService.STATE_NONE:
setStatus(R.string.title_not_connected);
mIdle();
break;
}
break;
Expand All @@ -520,9 +544,10 @@ public void handleMessage(Message msg) {
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);

if(!writeMessage.contains("google.com")) {
Log.d(TAG, "writeMessage = " + writeMessage);
mConversationArrayAdapter.add("Command: " + writeMessage);
}
break;
case Constants.MESSAGE_READ:
isRead = true;
Expand Down Expand Up @@ -551,9 +576,20 @@ public void handleMessage(Message msg) {
//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);
}

//check if ping was successful
if(readMessage.contains("1 packets")){
mConnect();
}
if(readMessage.contains("Unreachable") || readMessage.contains("failure")){
mOffline();
}
//make it so text doesn't show on chat (need a better way to check multiple strings since mConversationArrayAdapter only takes messages line by line)
if (!readMessage.contains("1 packets") && !readMessage.contains("64 bytes") && !readMessage.contains("google.com") &&
!readMessage.contains("rtt") && !readMessage.trim().isEmpty()){
mConversationArrayAdapter.add(readMessage);
}
}
break;
case Constants.MESSAGE_DEVICE_NAME:
// save the connected device's name
Expand Down Expand Up @@ -741,6 +777,7 @@ public void showHotspotDialog(){
hDialogFragment.setTargetFragment(this,REQUEST_DIALOG_FRAGMENT_HOTSPOT);
hDialogFragment.show(getFragmentManager().beginTransaction(),"hDialog");


}

public boolean isJson(String str) {
Expand Down Expand Up @@ -783,4 +820,24 @@ public void handleCallback(String str){
}

}
}

public void mOffline(){
Pbutton.setBackgroundResource((R.drawable.circle));
GradientDrawable bgShape = (GradientDrawable)Pbutton.getBackground();
bgShape.setColor(Color.RED);
}

public void mIdle(){
Pbutton.setBackgroundResource((R.drawable.circle));
GradientDrawable bgShape = (GradientDrawable)Pbutton.getBackground();
bgShape.setColor(Color.GRAY);
}

public void mConnect(){
Pbutton.setBackgroundResource((R.drawable.circle));
GradientDrawable bgShape = (GradientDrawable)Pbutton.getBackground();
bgShape.setColor(Color.GREEN);
}
}


8 changes: 8 additions & 0 deletions app/src/main/res/drawable/circle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#909090" />
<stroke
android:width="2dp"
android:color="#444444"
android:enabled="false" />
</shape>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ limitations under the License.
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/PING"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/circle" />

<ListView
android:id="@id/in"
android:layout_width="match_parent"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout-large/fragment_bluetooth_chat.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ limitations under the License.
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/PING"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/circle" />

<ListView
android:id="@id/in"
android:layout_width="match_parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ limitations under the License.
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/PING"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/circle" />

<ListView
android:id="@id/in"
android:layout_width="match_parent"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout-small/fragment_bluetooth_chat.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ limitations under the License.
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/PING"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/circle" />

<ListView
android:id="@id/in"
android:layout_width="match_parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ limitations under the License.
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/PING"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/circle" />

<ListView
android:id="@id/in"
android:layout_width="match_parent"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout-xlarge/fragment_bluetooth_chat.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ limitations under the License.
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/PING"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/circle" />

<ListView
android:id="@id/in"
android:layout_width="match_parent"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
android:layout_height="1dp"
android:background="@android:color/darker_gray" />


<FrameLayout
android:id="@+id/sample_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent" >
</FrameLayout>

<!--android:layout_weight="2"-->

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/fragment_bluetooth_chat.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/PING"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/circle" />

<ListView
android:id="@+id/in"
android:layout_width="match_parent"
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/res/menu/bluetooth_chat.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> <!--added so I could add other options into the menu-->

<item
Expand All @@ -40,7 +41,7 @@
android:title="@string/discoverable"/>
<item
android:id="@+id/hotspot_configuration"
app:showAsAction="ifRoom"
android:title="@string/create_hotspot"/>
android:title="@string/create_hotspot"
app:showAsAction="ifRoom" />

</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<string name="TestButton">PIRATESHIP</string>
<string name="DockerButton">DOCKER PS</string>
<string name="VersionButton">PIRATESHIP DETECTRPI</string>
<string name="PingWIFI">PIRATESHIP PING</string>
<!-- ProgressDialog -->
<string name="progress_dialog_title">Configuring RPi\'s Wi-Fi setting</string>
<string name="progress_dialog_title_hotspot">Configuring RPi as a Hotspot</string>
Expand Down
2 changes: 2 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#Thu Mar 29 02:17:42 EDT 2018
#Mon Jan 08 22:57:09 EST 2018
#Mon Apr 02 14:47:19 CDT 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
Expand Down

0 comments on commit 6e836fd

Please sign in to comment.