Skip to content

Commit

Permalink
add debug guard, fix bluetooth connect uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
e8035669 committed May 30, 2017
1 parent 94009f8 commit e9ef462
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 111 deletions.
Binary file added app/app-release.apk
Binary file not shown.
7 changes: 5 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
apply plugin: 'com.android.application'

android {
signingConfigs {
}
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
Expand All @@ -18,14 +20,15 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})

compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:support-v4:25.3.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,10 @@
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Handler;
import android.provider.Settings;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.Scanner;
import java.util.UUID;

/**
Expand All @@ -30,6 +21,7 @@ public abstract class BluetoothConnectThread extends Thread {
public static final int DISCONNECT_ON_ERROR = 4;
public static final int MessageDelivered = 10;
private final Object statusMutex = new Object();
private final UUID SSPUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private BluetoothDevice device;
private BluetoothSocket socket = null;
private int status = BluetoothConnectThread.NONE;
Expand All @@ -46,13 +38,14 @@ public BluetoothConnectThread(BluetoothDevice device, Handler messageHandler, in
this.messageHandler = messageHandler;
setStatus(BluetoothConnectThread.CONNECTING);
try {
Method method = device.getClass().getMethod("createRfcommSocket", int.class);
socket = (BluetoothSocket) method.invoke(device, Integer.valueOf(1));
//Method method = device.getClass().getMethod("createRfcommSocket", int.class);
//socket = (BluetoothSocket) method.invoke(device, Integer.valueOf(1));
socket = device.createRfcommSocketToServiceRecord(SSPUUID);
socket.connect();
inputStream = socket.getInputStream();
outputWriter = new PrintWriter(socket.getOutputStream());
setStatus(BluetoothConnectThread.CONNECTED);
} catch (IOException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
} catch (IOException e) {
setStatusErr(DISCONNECT_ON_ERROR, e);
} catch (Exception ex) {
setStatusErr(DISCONNECT_ON_ERROR, ex);
Expand All @@ -71,7 +64,7 @@ public void run() {
int length = inputStream.read(buffer);
messageHandler.obtainMessage(MessageDelivered, length, -1, buffer).sendToTarget();
}
} catch (IOException e) {
} catch (IOException e) {
ex = e;
}

Expand All @@ -94,10 +87,10 @@ public void run() {
} catch (IOException e) {
e.printStackTrace();
}
if(ex == null){
if (ex == null) {
setStatus(BluetoothConnectThread.DISCONNECT);
}else {
setStatusErr(BluetoothConnectThread.DISCONNECT_ON_ERROR,ex);
} else {
setStatusErr(BluetoothConnectThread.DISCONNECT_ON_ERROR, ex);
}
}

Expand Down Expand Up @@ -127,11 +120,11 @@ public void setNeedStop(boolean needStop) {
isNeedStop = needStop;
}

public String getDeviceName(){
public String getDeviceName() {
return device.getName();
}

public String getDeviceAddress(){
public String getDeviceAddress() {
return device.getAddress();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,23 @@

import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TwoLineListItem;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
Expand Down Expand Up @@ -50,6 +52,10 @@ public class MainActivity extends AppCompatActivity {
// Note: 小車命令計算機
private WheelCommand wheelCommand = new WheelCommand();

//Note: Debug
private boolean debug = false;
private int debugCount = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -252,6 +258,41 @@ public void onStopTrackingTouch(SeekBar seekBar) {
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
int value = preferences.getInt("seekbar", 128);
seekBar.setProgress(value);

// Note: Debug 模式
debug = preferences.getBoolean("debug", false);
LinearLayout debugView = (LinearLayout) findViewById(R.id.debugView);
Button autoModeBtn = (Button) findViewById(R.id.autoModeBtn);
if (debug) {
debugView.setVisibility(View.VISIBLE);
autoModeBtn.setVisibility(View.VISIBLE);
} else {
debugView.setVisibility(View.GONE);
autoModeBtn.setVisibility(View.GONE);
}
Button button = (Button) findViewById(R.id.bluetoothConnectBtn);
button.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
debugCount++;
if (debugCount == 10) {
debugCount = 0;
debug = !debug;
LinearLayout debugView = (LinearLayout) findViewById(R.id.debugView);
Button autoModeBtn = (Button) findViewById(R.id.autoModeBtn);
if (debug) {
debugView.setVisibility(View.VISIBLE);
autoModeBtn.setVisibility(View.VISIBLE);
Toast.makeText(MainActivity.this, "恭喜你 現在可以debug了!", Toast.LENGTH_SHORT).show();
} else {
debugView.setVisibility(View.GONE);
autoModeBtn.setVisibility(View.GONE);
}
}
return false;
}
});

}

@Override
Expand Down Expand Up @@ -504,7 +545,9 @@ protected void onDestroy() {
int data = seekBar.getProgress();
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("seekbar", data);
editor.putBoolean("debug", debug);
editor.commit();
}


}
24 changes: 15 additions & 9 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
tools:layout_editor_absoluteY="8dp">

<Space
android:id="@+id/hiddenBtn1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
Expand All @@ -48,6 +49,7 @@
tools:layout_editor_absoluteY="16dp" />

<Space
android:id="@+id/hiddenBtn2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.99" />
Expand All @@ -72,6 +74,7 @@
tools:layout_editor_absoluteY="136dp" />

<Space
android:id="@+id/hiddenBtn3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
Expand All @@ -93,6 +96,7 @@
android:orientation="horizontal">

<Space
android:id="@+id/hiddenBtn4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
Expand All @@ -108,6 +112,7 @@
tools:layout_editor_absoluteY="256dp" />

<Space
android:id="@+id/hiddenBtn5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
Expand Down Expand Up @@ -188,28 +193,29 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_row="3"
android:layout_weight="1"
android:onClick="autoModeBtnOnClick"
android:text="Auto Mode"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp" />
app:layout_constraintRight_toRightOf="parent" />

<LinearLayout
android:id="@+id/debugView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_marginBottom="64dp"
android:layout_marginRight="8dp"
android:layout_row="0"
android:orientation="vertical"
tools:layout_constraintTop_creator="1"
tools:layout_constraintLeft_creator="1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="64dp"
android:layout_marginRight="8dp">
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1">

<TextView
android:id="@+id/bluetoothStatus"
Expand Down
73 changes: 0 additions & 73 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,76 +1,3 @@
<resources>
<string name="app_name">小車車遙控器</string>
<string name="title_activity_settings">Settings</string>

<!-- Strings related to Settings -->

<!-- Example General settings -->
<string name="pref_header_general">General</string>

<string name="pref_title_social_recommendations">Enable social recommendations</string>
<string name="pref_description_social_recommendations">Recommendations for people to contact
based on your message history
</string>

<string name="pref_title_display_name">Display name</string>
<string name="pref_default_display_name">John Smith</string>

<string name="pref_title_add_friends_to_messages">Add friends to messages</string>
<string-array name="pref_example_list_titles">
<item>Always</item>
<item>When possible</item>
<item>Never</item>
</string-array>
<string-array name="pref_example_list_values">
<item>1</item>
<item>0</item>
<item>-1</item>
</string-array>

<!-- Example settings for Data & Sync -->
<string name="pref_header_data_sync">Data &amp; sync</string>

<string name="pref_title_sync_frequency">Sync frequency</string>
<string-array name="pref_sync_frequency_titles">
<item>15 minutes</item>
<item>30 minutes</item>
<item>1 hour</item>
<item>3 hours</item>
<item>6 hours</item>
<item>Never</item>
</string-array>
<string-array name="pref_sync_frequency_values">
<item>15</item>
<item>30</item>
<item>60</item>
<item>180</item>
<item>360</item>
<item>-1</item>
</string-array>

<string-array name="list_preference_entries">
<item>Entry 1</item>
<item>Entry 2</item>
<item>Entry 3</item>
</string-array>

<string-array name="list_preference_entry_values">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>

<string-array name="multi_select_list_preference_default_value" />

<string name="pref_title_system_sync_settings">System sync settings</string>

<!-- Example settings for Notifications -->
<string name="pref_header_notifications">Notifications</string>

<string name="pref_title_new_message_notifications">New message notifications</string>

<string name="pref_title_ringtone">Ringtone</string>
<string name="pref_ringtone_silent">Silent</string>

<string name="pref_title_vibrate">Vibrate</string>
</resources>

0 comments on commit e9ef462

Please sign in to comment.