diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9c4de58 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser new file mode 100644 index 0000000..ee79f7e Binary files /dev/null and b/.idea/caches/build_file_checksums.ser differ diff --git a/.idea/caches/gradle_models.ser b/.idea/caches/gradle_models.ser new file mode 100644 index 0000000..22e670f Binary files /dev/null and b/.idea/caches/gradle_models.ser differ diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..681f41a --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,116 @@ + + + + + + + +
+ + + + xmlns:android + + ^$ + + + +
+
+ + + + xmlns:.* + + ^$ + + + BY_NAME + +
+
+ + + + .*:id + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + .*:name + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + name + + ^$ + + + +
+
+ + + + style + + ^$ + + + +
+
+ + + + .* + + ^$ + + + BY_NAME + +
+
+ + + + .* + + http://schemas.android.com/apk/res/android + + + ANDROID_ATTRIBUTE_ORDER + +
+
+ + + + .* + + .* + + + BY_NAME + +
+
+
+
+
+
\ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..15a15b2 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..f43d428 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,21 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..51fa3e5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..b72ef99 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/Bluetooth-Terminal-master.iml b/Bluetooth-Terminal-master.iml new file mode 100644 index 0000000..531ae38 --- /dev/null +++ b/Bluetooth-Terminal-master.iml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/BluetoothTerminal.iml b/BluetoothTerminal.iml new file mode 100644 index 0000000..0dbc091 --- /dev/null +++ b/BluetoothTerminal.iml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/RobotArm.iml b/RobotArm.iml new file mode 100644 index 0000000..049511e --- /dev/null +++ b/RobotArm.iml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/app.iml b/app/app.iml new file mode 100644 index 0000000..0673f76 --- /dev/null +++ b/app/app.iml @@ -0,0 +1,100 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..0f33ea5 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,28 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 23 + buildToolsVersion '28.0.3' + + defaultConfig { + applicationId "me.tunca.robotarm" + minSdkVersion 21 + targetSdkVersion 23 + versionCode 12 + versionName "3.5" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation fileTree(include: ['*.jar'], dir: 'libs') + implementation 'com.android.support:appcompat-v7:23.4.0' + + implementation 'me.aflak.libraries:bluetooth:1.2.4' + implementation 'me.aflak.libraries:pulltorefresh:1.0' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..4af40f9 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in E:\Users\Omar\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/app/src/androidTest/java/me/tunca/robotarm/ApplicationTest.java b/app/src/androidTest/java/me/tunca/robotarm/ApplicationTest.java new file mode 100644 index 0000000..6b9af5b --- /dev/null +++ b/app/src/androidTest/java/me/tunca/robotarm/ApplicationTest.java @@ -0,0 +1,15 @@ +package me.tunca.robotarm; + +import android.app.Application; +import android.test.ApplicationTestCase; + +/** + * Testing Fundamentals + */ +public class ApplicationTest extends ApplicationTestCase +{ + public ApplicationTest() + { + super(Application.class); + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..cebcfb4 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/me/tunca/robotarm/About.java b/app/src/main/java/me/tunca/robotarm/About.java new file mode 100644 index 0000000..403d89a --- /dev/null +++ b/app/src/main/java/me/tunca/robotarm/About.java @@ -0,0 +1,35 @@ +package me.tunca.robotarm; +/** + * Created by Tunca on 05/01/2019. + */ +import android.app.Activity; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.view.View; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.TextView; + +public class About extends Activity +{ + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.about); + LinearLayout aboutLayout = (LinearLayout) findViewById(R.id.ll_about); + View childAbout = getLayoutInflater().inflate(R.layout.about, null); + aboutLayout.addView(childAbout); + + TextView textTeamInfo = (TextView) findViewById(R.id.teamInfo); + textTeamInfo.setTextColor(getResources().getColor(R.color.colorBlack)); + textTeamInfo.setText(getResources().getString(R.string.about_string)); + + int newHeight=288; + int newWidth=288; + ImageView imageView = (ImageView) findViewById(R.id.robotImage); + imageView.setImageResource(R.mipmap.ic_launcher_robot); + imageView.getLayoutParams().height=newHeight; + imageView.getLayoutParams().width=newWidth; + } +} \ No newline at end of file diff --git a/app/src/main/java/me/tunca/robotarm/MainActivity.java b/app/src/main/java/me/tunca/robotarm/MainActivity.java new file mode 100644 index 0000000..df25e11 --- /dev/null +++ b/app/src/main/java/me/tunca/robotarm/MainActivity.java @@ -0,0 +1,825 @@ +package me.tunca.robotarm; +/** + * Created by Tunca on 05/01/2019. + */ +import android.annotation.SuppressLint; +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.Bundle; +import android.os.Handler; +import android.support.v7.app.AppCompatActivity; +import android.text.Spannable; +import android.text.SpannableString; +import android.text.method.ScrollingMovementMethod; +import android.text.style.ForegroundColorSpan; +import android.util.Log; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.MotionEvent; +import android.view.View; +import android.widget.Button; +import android.widget.ImageButton; +import android.widget.LinearLayout; +import android.widget.ProgressBar; +import android.widget.TextView; +import android.widget.Toast; +import java.util.Locale; +import java.util.Objects; +import me.aflak.bluetooth.Bluetooth; + + +public class MainActivity extends AppCompatActivity implements Bluetooth.CommunicationCallback +{ + public static Locale locale = Locale.getDefault(); + ImageButton buttonUp, buttonDown, buttonForward, buttonBack, buttonRight, buttonLeft, buttonClmpOn, buttonClmpOff; + private Bluetooth btDevice; + private TextView display; + private boolean registered = false; + public Runnable mAction; + private Handler mHandler; + int delayMilis = 16; + int postDelayed = 16; + String comingData; + ProgressBar progressBar; + TextView textView; + + @SuppressLint("ClickableViewAccessibility") + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + display = (TextView) findViewById(R.id.display); + display.setMovementMethod(new ScrollingMovementMethod()); + + btDevice = new Bluetooth(this); + btDevice.enableBluetooth(); + + btDevice.setCommunicationCallback(this); + + int pos = Objects.requireNonNull(getIntent().getExtras()).getInt("pos"); + + Display(getResources().getString(R.string.connect)); + Display(getResources().getString(R.string.info_btn)); + btDevice.connectToDevice(btDevice.getPairedDevices().get(pos)); + + // Battery + LinearLayout battery = (LinearLayout) findViewById(R.id.battery); + @SuppressLint("InflateParams") View childBattery = getLayoutInflater().inflate(R.layout.battery, null); + assert battery != null; + battery.addView(childBattery); + progressBar = (ProgressBar) findViewById(R.id.prgBar); + final TextView textViewPercent = (TextView) findViewById(R.id.prgBarText_percent); + textView = (TextView) findViewById(R.id.prgBarText); + if (!btDevice.isConnected()) + { + progressBar.setProgress(0); + textView.setText(getResources().getString(R.string.data_wait)); + } + + assert textViewPercent != null; + textViewPercent.setText(progressBar.getProgress() + "%"); + if (progressBar.getProgress() >= 75 && progressBar.getProgress() <= 100) + { + progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.battery_bar_green)); + } + else if (progressBar.getProgress() < 75 && progressBar.getProgress() >= 50) + { + progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.battery_bar_yellow)); + } + else if (progressBar.getProgress() < 50 && progressBar.getProgress() >= 25) + { + progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.battery_bar_orange)); + } + else if (progressBar.getProgress() < 25 && progressBar.getProgress() >= 0) + { + progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.battery_bar_red)); + } + + + // Control page of servo motors, button assignments and image insertion + LinearLayout servo = (LinearLayout) findViewById(R.id.buttons); + @SuppressLint("InflateParams") View childServos = getLayoutInflater().inflate(R.layout.buttons, null); + if (servo != null) + { + servo.addView(childServos); + } + else + { + Log.e("TAG-MA", "Error adding image cannot insert servo control image"); + } + buttonUp = (ImageButton) childServos.findViewById(R.id.button_up); + + buttonUp.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View view) + { + btDevice.send("1"); + Display(getResources().getString(R.string.move_up)); + Log.e("TAG-MA", "up : 1"); + } + }); + + buttonUp.setOnTouchListener(new View.OnTouchListener() + { + @Override + public boolean onTouch(View v, MotionEvent event) + { + switch (event.getAction()) + { + case MotionEvent.ACTION_DOWN: + buttonClickable(1, true); + sendDataAction("1", 1); + Log.e("Data :", "Veri : 1"); + mHandler = new Handler(); + mHandler.postDelayed(mAction, postDelayed); + break; + + case MotionEvent.ACTION_UP: + if (mHandler == null) + { + return true; + } + mHandler.removeCallbacks(mAction); + mHandler = null; + buttonClickable(0, true); + break; + } + return false; + } + }); + + buttonDown = (ImageButton) childServos.findViewById(R.id.button_down); + + buttonDown.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View view) + { + btDevice.send("2"); + Display(getResources().getString(R.string.move_down)); + Log.e("TAG-MA", "down : 2"); + } + }); + + buttonDown.setOnTouchListener(new View.OnTouchListener() + { + @Override + public boolean onTouch(View v, MotionEvent event) + { + switch (event.getAction()) + { + case MotionEvent.ACTION_DOWN: + buttonClickable(2, true); + sendDataAction("2", 2); + mHandler = new Handler(); + mHandler.postDelayed(mAction, postDelayed); + break; + case MotionEvent.ACTION_UP: + if (mHandler == null) + { + return true; + } + mHandler.removeCallbacks(mAction); + mHandler = null; + buttonClickable(0, true); + break; + + } + return false; + } + }); + + + buttonForward = (ImageButton) childServos.findViewById(R.id.button_forward); + + buttonForward.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View view) + { + btDevice.send("3"); + Display(getResources().getString(R.string.move_forward)); + Log.e("TAG-MA", "forward : 3"); + } + }); + + buttonForward.setOnTouchListener(new View.OnTouchListener() + { + @Override + public boolean onTouch(View v, MotionEvent event) + { + switch (event.getAction()) + { + case MotionEvent.ACTION_DOWN: + + buttonClickable(3, true); + sendDataAction("3", 3); + mHandler = new Handler(); + mHandler.postDelayed(mAction, postDelayed); + break; + case MotionEvent.ACTION_UP: + if (mHandler == null) + { + return true; + } + mHandler.removeCallbacks(mAction); + mHandler = null; + buttonClickable(0, true); + break; + + } + return false; + } + }); + + buttonBack = (ImageButton) childServos.findViewById(R.id.button_back); + + buttonBack.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View view) + { + btDevice.send("4"); + Display(getResources().getString(R.string.move_backward)); + Log.e("TAG-MA", "back : 4"); + } + }); + + buttonBack.setOnTouchListener(new View.OnTouchListener() + { + @Override + public boolean onTouch(View v, MotionEvent event) + { + switch (event.getAction()) + { + case MotionEvent.ACTION_DOWN: + buttonClickable(4, true); + sendDataAction("4", 4); + mHandler = new Handler(); + mHandler.postDelayed(mAction, postDelayed); + break; + case MotionEvent.ACTION_UP: + if (mHandler == null) + { + return true; + } + mHandler.removeCallbacks(mAction); + mHandler = null; + buttonClickable(0, true); + break; + } + return false; + } + }); + + buttonRight = (ImageButton) childServos.findViewById(R.id.button_right); + + buttonRight.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View view) + { + btDevice.send("5"); + Display(getResources().getString(R.string.turn_right)); + Log.e("TAG-MA", "right : 5"); + } + }); + + buttonRight.setOnTouchListener(new View.OnTouchListener() + { + @Override + public boolean onTouch(View v, MotionEvent event) + { + switch (event.getAction()) + { + case MotionEvent.ACTION_DOWN: + buttonClickable(5, true); + sendDataAction("5", 5); + mHandler = new Handler(); + mHandler.postDelayed(mAction, postDelayed); + break; + case MotionEvent.ACTION_UP: + if (mHandler == null) + { + return true; + } + mHandler.removeCallbacks(mAction); + mHandler = null; + buttonClickable(0, true); + break; + } + return false; + } + }); + + buttonLeft = (ImageButton) childServos.findViewById(R.id.button_left); + + buttonLeft.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View view) + { + btDevice.send("6"); + Display(getResources().getString(R.string.turn_left)); + Log.e("TAG-MA", "left : 6"); + } + }); + + buttonLeft.setOnTouchListener(new View.OnTouchListener() + { + @Override + public boolean onTouch(View v, MotionEvent event) + { + switch (event.getAction()) + { + case MotionEvent.ACTION_DOWN: + + buttonClickable(6, true); + sendDataAction("6", 6); + mHandler = new Handler(); + mHandler.postDelayed(mAction, postDelayed); + break; + case MotionEvent.ACTION_UP: + if (mHandler == null) + { + return true; + } + mHandler.removeCallbacks(mAction); + mHandler = null; + buttonClickable(0, true); + break; + + } + return false; + } + }); + + buttonClmpOn = (ImageButton) childServos.findViewById(R.id.button_clampon); + buttonClmpOn.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View view) + { + btDevice.send("7"); + Display(getResources().getString(R.string.clamp_on)); + Log.e("TAG-MA", "clmpOn : 7"); + // operation =-1; + + } + }); + + buttonClmpOn.setOnTouchListener(new View.OnTouchListener() + { + @Override + public boolean onTouch(View v, MotionEvent event) + { + switch (event.getAction()) + { + case MotionEvent.ACTION_DOWN: + buttonClickable(7, true); + sendDataAction("7", 7); + + mHandler = new Handler(); + mHandler.postDelayed(mAction, postDelayed); + break; + case MotionEvent.ACTION_UP: + if (mHandler == null) + { + return true; + } + mHandler.removeCallbacks(mAction); + mHandler = null; + buttonClickable(0, true); + break; + + } + return false; + } + }); + + buttonClmpOff = (ImageButton) childServos.findViewById(R.id.button_clampoff); + buttonClmpOff.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View view) + { + btDevice.send("8"); + Display(getResources().getString(R.string.clamp_off)); + Log.e("TAG-MA", "clmpOff : 8"); + + } + }); + + buttonClmpOff.setOnTouchListener(new View.OnTouchListener() + { + @Override + public boolean onTouch(View v, MotionEvent event) + { + switch (event.getAction()) + { + case MotionEvent.ACTION_DOWN: + + buttonClickable(8, true); + sendDataAction("8", 8); + mHandler = new Handler(); + mHandler.postDelayed(mAction, postDelayed); + break; + case MotionEvent.ACTION_UP: + if (mHandler == null) + { + return true; + } + mHandler.removeCallbacks(mAction); + mHandler = null; + buttonClickable(0, true); + break; + } + return false; + } + }); + + Button clearButton = (Button) findViewById(R.id.button_clear); + assert clearButton != null; + clearButton.setText(getResources().getString(R.string.clear_feedback)); + clearButton.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View view) + { + display.setText(""); + Toast.makeText(getApplicationContext(), getResources().getString(R.string.clear_info_toast), Toast.LENGTH_SHORT).show(); + } + }); + buttonClickable(0, false); + IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); + registerReceiver(mReceiver, filter); + registered = true; + } + + + @Override + public void onDestroy() + { + super.onDestroy(); + if (registered) + { + unregisterReceiver(mReceiver); + registered = false; + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) + { + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.menu_main, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) + { + // Handle item selection + switch (item.getItemId()) + { + case R.id.close: + btDevice.removeCommunicationCallback(); + btDevice.disconnect(); + Intent intentSelect = new Intent(this, Select.class); + startActivity(intentSelect); + finish(); + return true; + case R.id.about: + Intent intentAbout = new Intent(this, About.class); + startActivity(intentAbout); + return true; + default: + return super.onOptionsItemSelected(item); + } + } + + public void Display(final String s) + { + this.runOnUiThread(new Runnable() + { + @Override + public void run() + { + display.append("\n"); + display.append(s); + } + }); + } + + + // 0 Black (Default) + // 1 Green (for Device İnfo) + // 2 Red (for Error) + // 3 Mavi (for Callback) + public void Display(final String s, final int color, final int start) + { + this.runOnUiThread(new Runnable() + { + @Override + public void run() + { + SpannableString ss = new SpannableString(s); + if (color == 1) + { + ss.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorGreen)), start, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + } + else if (color == 2) + { + ss.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorRed)), start, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + } + else if (color == 3) + { + ss.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorBlue)), start, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + } + else + { + ss.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorBlack)), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + } + + display.append("\n"); + display.append(ss); + } + }); + } + + @Override + public void onConnect(BluetoothDevice device) + { + String s = getResources().getString(R.string.connect_device) + device.getName() + " - " + device.getAddress(); + Display(s, 1, 19); + this.runOnUiThread(new Runnable() + { + @Override + public void run() + { + buttonClickable(0, true); + } + }); + } + + + @Override + public void onDisconnect(BluetoothDevice device, String message) + { + Display(getResources().getString(R.string.disconnect)); + Display(getResources().getString(R.string.reconnect)); + Display(getResources().getString(R.string.btn_inactive)); + btDevice.connectToDevice(device); + textView.setText(getResources().getString(R.string.connect_wait)); + Log.e("TAG-Message", "onDisconnect Message : " + message); + this.runOnUiThread(new Runnable() + { + @Override + public void run() + { + buttonClickable(0, false); + } + }); + + } + + @Override + public void onMessage(String message) + { + comingData = message; + // As soon as the message arrives, the data belongs to the battery. + progressBar.setProgress(Integer.parseInt(comingData)); + textView.setText(getResources().getString(R.string.data_process)); + Log.e("TAG-Message", "onMassage Message : " + message); + } + + @Override + public void onError(String message) + { + Display(getResources().getString(R.string.error) + message); + } + + @Override + public void onConnectError(final BluetoothDevice device, String message) + { + String s = getResources().getString(R.string.error) + message; + Display(s, 2, 6); + Display(getResources().getString(R.string.retrying)); + runOnUiThread(new Runnable() + { + @Override + public void run() + { + Handler handler = new Handler(); + handler.postDelayed(new Runnable() + { + @Override + public void run() + { + btDevice.connectToDevice(device); + } + }, 2000); + } + }); + } + + private final BroadcastReceiver mReceiver = new BroadcastReceiver() + { + @Override + public void onReceive(Context context, Intent intent) + { + final String action = intent.getAction(); + + assert action != null; + if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) + { + final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); + Intent intent1 = new Intent(MainActivity.this, Select.class); + + switch (state) + { + case BluetoothAdapter.STATE_OFF: + if (registered) + { + unregisterReceiver(mReceiver); + registered = false; + } + startActivity(intent1); + finish(); + break; + case BluetoothAdapter.STATE_TURNING_OFF: + if (registered) + { + unregisterReceiver(mReceiver); + registered = false; + } + startActivity(intent1); + finish(); + break; + } + } + } + }; + + public void sendDataAction(final String s, final int pos) + { + mAction = new Runnable() + { + @Override + public void run() + { + btDevice.send(s); + displayData(pos); + mHandler.postDelayed(this, delayMilis); + } + }; + } + + public void displayData(int s) + { + switch (s) + { + case 1: + Display(getResources().getString(R.string.move_up)); + break; + case 2: + Display(getResources().getString(R.string.move_down)); + break; + case 3: + Display(getResources().getString(R.string.move_forward)); + break; + case 4: + Display(getResources().getString(R.string.move_backward)); + break; + case 5: + Display(getResources().getString(R.string.turn_right)); + break; + case 6: + Display(getResources().getString(R.string.turn_left)); + break; + case 7: + Display(getResources().getString(R.string.clamp_on)); + break; + case 8: + Display(getResources().getString(R.string.clamp_off)); + break; + } + } + + public void buttonClickable(int pos, boolean bool) + { + + if (0 < pos && pos > 8) + { + pos = 0; + } + else if (pos == 0) + { + buttonUp.setEnabled(bool); + buttonDown.setEnabled(bool); + buttonForward.setEnabled(bool); + buttonBack.setEnabled(bool); + buttonRight.setEnabled(bool); + buttonLeft.setEnabled(bool); + buttonClmpOn.setEnabled(bool); + buttonClmpOff.setEnabled(bool); + } + else + { + switch (pos) + { + case 1: + buttonUp.setEnabled(bool); + buttonDown.setEnabled(false); + buttonForward.setEnabled(false); + buttonBack.setEnabled(false); + buttonRight.setEnabled(false); + buttonLeft.setEnabled(false); + buttonClmpOn.setEnabled(false); + buttonClmpOff.setEnabled(false); + break; + case 2: + buttonUp.setEnabled(false); + buttonDown.setEnabled(bool); + buttonForward.setEnabled(false); + buttonBack.setEnabled(false); + buttonRight.setEnabled(false); + buttonLeft.setEnabled(false); + buttonClmpOn.setEnabled(false); + buttonClmpOff.setEnabled(false); + break; + case 3: + buttonUp.setEnabled(false); + buttonDown.setEnabled(false); + buttonForward.setEnabled(bool); + buttonBack.setEnabled(false); + buttonRight.setEnabled(false); + buttonLeft.setEnabled(false); + buttonClmpOn.setEnabled(false); + buttonClmpOff.setEnabled(false); + break; + case 4: + buttonUp.setEnabled(false); + buttonDown.setEnabled(false); + buttonForward.setEnabled(false); + buttonBack.setEnabled(bool); + buttonRight.setEnabled(false); + buttonLeft.setEnabled(false); + buttonClmpOn.setEnabled(false); + buttonClmpOff.setEnabled(false); + break; + case 5: + buttonUp.setEnabled(false); + buttonDown.setEnabled(false); + buttonForward.setEnabled(false); + buttonBack.setEnabled(false); + buttonRight.setEnabled(bool); + buttonLeft.setEnabled(false); + buttonClmpOn.setEnabled(false); + buttonClmpOff.setEnabled(false); + break; + case 6: + buttonUp.setEnabled(false); + buttonDown.setEnabled(false); + buttonForward.setEnabled(false); + buttonBack.setEnabled(false); + buttonRight.setEnabled(false); + buttonLeft.setEnabled(bool); + buttonClmpOn.setEnabled(false); + buttonClmpOff.setEnabled(false); + break; + case 7: + buttonUp.setEnabled(false); + buttonDown.setEnabled(false); + buttonForward.setEnabled(false); + buttonBack.setEnabled(false); + buttonRight.setEnabled(false); + buttonLeft.setEnabled(false); + buttonClmpOn.setEnabled(bool); + buttonClmpOff.setEnabled(false); + break; + case 8: + buttonUp.setEnabled(false); + buttonDown.setEnabled(false); + buttonForward.setEnabled(false); + buttonBack.setEnabled(false); + buttonRight.setEnabled(false); + buttonLeft.setEnabled(false); + buttonClmpOn.setEnabled(false); + buttonClmpOff.setEnabled(bool); + break; + } + } + + } + + +} diff --git a/app/src/main/java/me/tunca/robotarm/Scan.java b/app/src/main/java/me/tunca/robotarm/Scan.java new file mode 100644 index 0000000..b604fe7 --- /dev/null +++ b/app/src/main/java/me/tunca/robotarm/Scan.java @@ -0,0 +1,168 @@ +package me.tunca.robotarm; +/** + * Created by Tunca on 05/01/2019. + */ +import android.app.Activity; +import android.bluetooth.BluetoothDevice; +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.ListView; +import android.widget.ProgressBar; +import android.widget.TextView; +import java.util.ArrayList; +import java.util.List; +import me.aflak.bluetooth.Bluetooth; + +public class Scan extends Activity implements Bluetooth.DiscoveryCallback, AdapterView.OnItemClickListener +{ + + private Bluetooth bluetooth; + private ListView listView; + private ArrayAdapter adapter; + private TextView state; + private ProgressBar progress; + private Button scan; + private List devices; + + @Override + protected void onCreate(Bundle savedInstanceState) + { + + super.onCreate(savedInstanceState); + setContentView(R.layout.scan); + + listView = (ListView) findViewById(R.id.scan_list); + state = (TextView) findViewById(R.id.scan_state); + progress = (ProgressBar) findViewById(R.id.scan_progress); + scan = (Button) findViewById(R.id.scan_scan_again); + + adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new ArrayList()); + listView.setAdapter(adapter); + listView.setOnItemClickListener(this); + + bluetooth = new Bluetooth(this); + bluetooth.setDiscoveryCallback(this); + + bluetooth.scanDevices(); + progress.setVisibility(View.VISIBLE); + state.setText(getResources().getString(R.string.scan)); + listView.setEnabled(false); + + scan.setEnabled(false); + devices = new ArrayList<>(); + + scan.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + runOnUiThread(new Runnable() + { + @Override + public void run() + { + adapter.clear(); + scan.setEnabled(false); + } + }); + + devices = new ArrayList<>(); + progress.setVisibility(View.VISIBLE); + state.setText(getResources().getString(R.string.scan)); + bluetooth.scanDevices(); + } + }); + } + + private void setText(final String txt) + { + runOnUiThread(new Runnable() + { + @Override + public void run() + { + state.setText(txt); + } + }); + } + + private void setProgressVisibility(final int id) + { + runOnUiThread(new Runnable() + { + @Override + public void run() + { + progress.setVisibility(id); + } + }); + } + + @Override + public void onFinish() + { + setProgressVisibility(View.INVISIBLE); + setText(getResources().getString(R.string.scan_finish)); + runOnUiThread(new Runnable() + { + @Override + public void run() + { + scan.setEnabled(true); + listView.setEnabled(true); + } + }); + } + + @Override + public void onDevice(final BluetoothDevice device) + { + final BluetoothDevice tmp = device; + devices.add(device); + + runOnUiThread(new Runnable() + { + @Override + public void run() + { + adapter.add(tmp.getAddress() + " - " + tmp.getName()); + } + }); + } + + @Override + public void onPair(BluetoothDevice device) + { + setProgressVisibility(View.INVISIBLE); + setText(getResources().getString(R.string.paired)); + Intent i = new Intent(Scan.this, Select.class); + startActivity(i); + finish(); + } + + @Override + public void onUnpair(BluetoothDevice device) + { + setProgressVisibility(View.INVISIBLE); + setText(getResources().getString(R.string.paired)); + } + + @Override + public void onError(String message) + { + setProgressVisibility(View.INVISIBLE); + setText(getResources().getString(R.string.error) + message); + } + + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) + { + setProgressVisibility(View.VISIBLE); + setText(getResources().getString(R.string.pairing)); + bluetooth.pair(devices.get(position)); + } +} diff --git a/app/src/main/java/me/tunca/robotarm/Select.java b/app/src/main/java/me/tunca/robotarm/Select.java new file mode 100644 index 0000000..6af41c0 --- /dev/null +++ b/app/src/main/java/me/tunca/robotarm/Select.java @@ -0,0 +1,180 @@ +package me.tunca.robotarm; +/** + * Created by Tunca on 05/01/2019. + */ +import android.app.Activity; +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.Bundle; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.ListView; +import android.widget.Toast; +import java.util.ArrayList; +import java.util.List; +import me.aflak.bluetooth.Bluetooth; +import me.aflak.pulltorefresh.PullToRefresh; + + +public class Select extends Activity implements PullToRefresh.OnRefreshListener +{ + private Bluetooth bt; + private ListView listView; + private Button not_found; + private List paired; + private PullToRefresh pull_to_refresh; + private boolean registered = false; + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.select); + + IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); + registerReceiver(mReceiver, filter); + registered = true; + + bt = new Bluetooth(this); + bt.enableBluetooth(); + + pull_to_refresh = (PullToRefresh) findViewById(R.id.pull_to_refresh); + listView = (ListView) findViewById(R.id.list); + not_found = (Button) findViewById(R.id.not_in_list); + + pull_to_refresh.setListView(listView); + pull_to_refresh.setOnRefreshListener(this); + pull_to_refresh.setSlide(500); + + listView.setOnItemClickListener(new AdapterView.OnItemClickListener() + { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) + { + Intent i = new Intent(Select.this, MainActivity.class); + i.putExtra("pos", position); + if (registered) + { + unregisterReceiver(mReceiver); + registered = false; + } + startActivity(i); + finish(); + } + }); + + not_found.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + Intent i = new Intent(Select.this, Scan.class); + startActivity(i); + } + }); + + addDevicesToList(); + } + + @Override + public void onRefresh() + { + List names = new ArrayList<>(); + for (BluetoothDevice d : bt.getPairedDevices()) + { + names.add(d.getName()); + } + + String[] array = names.toArray(new String[names.size()]); + final ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, array); + + this.runOnUiThread(new Runnable() + { + @Override + public void run() + { + listView.removeViews(0, listView.getCount()); + listView.setAdapter(adapter); + paired = bt.getPairedDevices(); + } + }); + pull_to_refresh.refreshComplete(); + } + + @Override + public void onDestroy() + { + super.onDestroy(); + if (registered) + { + unregisterReceiver(mReceiver); + registered = false; + } + } + + private void addDevicesToList() + { + paired = bt.getPairedDevices(); + + List names = new ArrayList<>(); + for (BluetoothDevice d : paired) + { + names.add(d.getName()); + } + + String[] array = names.toArray(new String[names.size()]); + + ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, array); + + listView.setAdapter(adapter); + + not_found.setEnabled(true); + } + + private final BroadcastReceiver mReceiver = new BroadcastReceiver() + { + @Override + public void onReceive(Context context, Intent intent) + { + final String action = intent.getAction(); + + if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) + { + final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); + + switch (state) + { + case BluetoothAdapter.STATE_OFF: + runOnUiThread(new Runnable() + { + @Override + public void run() + { + listView.setEnabled(false); + } + }); + Toast.makeText(Select.this, getResources().getString(R.string.bluetooth_on), Toast.LENGTH_LONG).show(); + break; + case BluetoothAdapter.STATE_ON: + runOnUiThread(new Runnable() + { + @Override + public void run() + { + addDevicesToList(); + listView.setEnabled(true); + } + }); + break; + } + } + } + }; +} + diff --git a/app/src/main/res/drawable/arrow_down.png b/app/src/main/res/drawable/arrow_down.png new file mode 100644 index 0000000..e175cd7 Binary files /dev/null and b/app/src/main/res/drawable/arrow_down.png differ diff --git a/app/src/main/res/drawable/arrow_left.png b/app/src/main/res/drawable/arrow_left.png new file mode 100644 index 0000000..b53d5d1 Binary files /dev/null and b/app/src/main/res/drawable/arrow_left.png differ diff --git a/app/src/main/res/drawable/arrow_right.png b/app/src/main/res/drawable/arrow_right.png new file mode 100644 index 0000000..fc566e4 Binary files /dev/null and b/app/src/main/res/drawable/arrow_right.png differ diff --git a/app/src/main/res/drawable/arrow_up.png b/app/src/main/res/drawable/arrow_up.png new file mode 100644 index 0000000..0108111 Binary files /dev/null and b/app/src/main/res/drawable/arrow_up.png differ diff --git a/app/src/main/res/drawable/battery_bar_green.xml b/app/src/main/res/drawable/battery_bar_green.xml new file mode 100644 index 0000000..a79bfd8 --- /dev/null +++ b/app/src/main/res/drawable/battery_bar_green.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/battery_bar_orange.xml b/app/src/main/res/drawable/battery_bar_orange.xml new file mode 100644 index 0000000..b596db0 --- /dev/null +++ b/app/src/main/res/drawable/battery_bar_orange.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/battery_bar_red.xml b/app/src/main/res/drawable/battery_bar_red.xml new file mode 100644 index 0000000..111f374 --- /dev/null +++ b/app/src/main/res/drawable/battery_bar_red.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/battery_bar_yellow.xml b/app/src/main/res/drawable/battery_bar_yellow.xml new file mode 100644 index 0000000..dcbf931 --- /dev/null +++ b/app/src/main/res/drawable/battery_bar_yellow.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/button_selector.xml b/app/src/main/res/drawable/button_selector.xml new file mode 100644 index 0000000..0efae77 --- /dev/null +++ b/app/src/main/res/drawable/button_selector.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + diff --git a/app/src/main/res/drawable/clamp_off.png b/app/src/main/res/drawable/clamp_off.png new file mode 100644 index 0000000..3020d47 Binary files /dev/null and b/app/src/main/res/drawable/clamp_off.png differ diff --git a/app/src/main/res/drawable/clamp_on.png b/app/src/main/res/drawable/clamp_on.png new file mode 100644 index 0000000..c776c07 Binary files /dev/null and b/app/src/main/res/drawable/clamp_on.png differ diff --git a/app/src/main/res/drawable/customborder.xml b/app/src/main/res/drawable/customborder.xml new file mode 100644 index 0000000..b7fbe76 --- /dev/null +++ b/app/src/main/res/drawable/customborder.xml @@ -0,0 +1,12 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/flag_eng.png b/app/src/main/res/drawable/flag_eng.png new file mode 100644 index 0000000..3835dbf Binary files /dev/null and b/app/src/main/res/drawable/flag_eng.png differ diff --git a/app/src/main/res/drawable/flag_turk.png b/app/src/main/res/drawable/flag_turk.png new file mode 100644 index 0000000..762eec1 Binary files /dev/null and b/app/src/main/res/drawable/flag_turk.png differ diff --git a/app/src/main/res/drawable/turn_left.png b/app/src/main/res/drawable/turn_left.png new file mode 100644 index 0000000..6484818 Binary files /dev/null and b/app/src/main/res/drawable/turn_left.png differ diff --git a/app/src/main/res/drawable/turn_right.png b/app/src/main/res/drawable/turn_right.png new file mode 100644 index 0000000..b01fe83 Binary files /dev/null and b/app/src/main/res/drawable/turn_right.png differ diff --git a/app/src/main/res/layout/about.xml b/app/src/main/res/layout/about.xml new file mode 100644 index 0000000..670ea59 --- /dev/null +++ b/app/src/main/res/layout/about.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..ae14e4d --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,60 @@ + + + + + + + + + + + + +