Skip to content

Commit

Permalink
Ajustes controle, visuais
Browse files Browse the repository at this point in the history
  • Loading branch information
fbvictorhugo committed Mar 15, 2019
1 parent 6523ec6 commit 3d4914a
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 83 deletions.
8 changes: 6 additions & 2 deletions .idea/misc.xml

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

18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
# joystick_bluetooth
Android Joystick Bluetooth for Arduino
# Android Joystick Bluetooth for Arduino

##### Command Mapping #####

Command | Char
------------- | -------------
BTN_LED_ON | 'a'
BTN_LED_OFF | 'b'
BTN_BUZZ | 'c'
AXIS_UP | 'd'
AXIS_DOWN | 'e'
AXIS_LEFT_UP | 'f'
AXIS_LEFT_DOWN | 'g'
AXIS_RIGHT_UP | 'h'
AXIS_RIGHT_DOWN | 'i'
AXIS_CENTERED | 'j'
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import android.app.Application;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;

import net.fbvictorhugo.joystickbt.controller.BluetoothModel;

import java.io.IOException;
import java.util.UUID;

Expand All @@ -15,7 +16,7 @@ public class AppApplication extends Application {

public BluetoothAdapter appBluetoothAdapter = null;
public BluetoothSocket appBluetoothSocket = null;
public BluetoothDevice appBluetoothDevice;
public BluetoothModel appBluetoothModel;

@Override
public void onCreate() {
Expand All @@ -30,7 +31,7 @@ public void disconectBluetooth() {
e.printStackTrace();
} finally {
appBluetoothSocket = null;
appBluetoothDevice = null;
appBluetoothModel = null;
}
}

Expand Down
20 changes: 7 additions & 13 deletions app/src/main/java/net/fbvictorhugo/joystickbt/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,21 @@ protected void showToast(String msg) {
protected void connectInDevice(BluetoothModel device, ConnectionBluetoothCallback connectionBluetoothListener) {
if (device != null && device.getAddress() != null) {
mConnectionBTListener = connectionBluetoothListener;
new ConnectBT().execute(device.getAddress());
new BluetoothConnectTask().execute(device.getAddress());
} else {
mConnectionBTListener.onFailed("BT Device is null!");
mConnectionBTListener.onFailed(String.valueOf(R.string.bluetooth_device_null));
}
}

private class ConnectBT extends AsyncTask<String, Integer, Boolean> // UI thread
private class BluetoothConnectTask extends AsyncTask<String, Integer, Boolean> // UI thread
{

@Override
protected void onPreExecute() {
//show a progress dialog
}

@Override
protected Boolean doInBackground(String... address) {
try {

application.appBluetoothDevice = application.appBluetoothAdapter.getRemoteDevice(address[0]);
application.appBluetoothSocket = application.appBluetoothDevice.createInsecureRfcommSocketToServiceRecord(AppApplication.MY_UUID);
application.appBluetoothModel = new BluetoothModel(application.appBluetoothAdapter.getRemoteDevice(address[0]));
application.appBluetoothSocket = application.appBluetoothModel.getBluetoothDevice().createInsecureRfcommSocketToServiceRecord(AppApplication.MY_UUID);
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
application.appBluetoothSocket.connect();
return true;
Expand All @@ -63,12 +58,11 @@ protected Boolean doInBackground(String... address) {
}

@Override
protected void onPostExecute(Boolean result) //after the doInBackground, it checks if everything went fine
{
protected void onPostExecute(Boolean result) {
if (result) {

if (mConnectionBTListener != null) {
mConnectionBTListener.onConnected(new BluetoothModel(application.appBluetoothDevice));
mConnectionBTListener.onConnected(application.appBluetoothModel);
}
} else {
if (mConnectionBTListener != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {

Expand Down Expand Up @@ -100,7 +99,7 @@ public void onClick(BluetoothModel device, int position) {
private void initializeBluetooth() {

if (application.appBluetoothAdapter == null) {
Toast.makeText(getApplicationContext(), "Bluetooth Device Not Available", Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), R.string.bluetooth_device_not_available, Toast.LENGTH_LONG).show();
} else if (!application.appBluetoothAdapter.isEnabled()) {
Intent intentEnableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intentEnableBT, AppApplication.REQUEST_ENABLE_BT);
Expand All @@ -121,7 +120,7 @@ private void pairedDevicesList() {
mPairedDevices.add(btModel);
}
} else {
showToast("No Paired Bluetooth Devices Found.");
showToast(getString(R.string.no_paired_bluetooth_devices_found));
}
recyclerAdapter.Update(mPairedDevices);
}
Expand All @@ -144,7 +143,8 @@ public void onConnected(BluetoothModel device) {
@Override
public void onFailed(String error) {
mProgressDialog.dismiss();
showToast("BT Connection Failed \"" + device.getAddress() + "\"\n:" + error);
String msg = String.format(getString(R.string.frmt_bluetooth_connection_failed), device.getAddress());
showToast(msg + "\"\n:" + error);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

public class BluetoothCmd {

public static final char BTN_A_ACTION_DOWN = 'a';
public static final char BTN_A_ACTION_UP = 'b';
public static final char BTN_LED_ON = 'a';
public static final char BTN_LED_OFF = 'b';

public static final char BTN_B_ACTION_DOWN = 'c';
public static final char BTN_B_ACTION_UP = 21;
public static final char BTN_BUZZ = 'c';

public static final char AXIS_UP = 'd';
public static final char AXIS_DOWN = 'e';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ public class BluetoothModel {
private String name;
private String address;
private boolean isBonded;

//
public BluetoothModel(String name, String address) {
this.name = name;
this.address = address;
}
private BluetoothDevice mDevice;

public BluetoothModel(BluetoothDevice device) {
mDevice = device;
this.name = device.getName();
this.address = device.getAddress();
}
Expand All @@ -24,18 +20,10 @@ public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public boolean isBonded() {
return isBonded;
}
Expand All @@ -44,6 +32,10 @@ public void setBonded(boolean bonded) {
isBonded = bonded;
}

public BluetoothDevice getBluetoothDevice() {
return mDevice;
}

//endregion

}
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
package net.fbvictorhugo.joystickbt.virtualjoy;

import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.TextView;

import net.fbvictorhugo.joystickbt.BaseActivity;
import net.fbvictorhugo.joystickbt.R;
import net.fbvictorhugo.joystickbt.controller.BluetoothCmd;

import io.github.controlwear.virtual.joystick.android.JoystickView;

import static net.fbvictorhugo.joystickbt.controller.BluetoothCmd.BTN_B_ACTION_DOWN;
import static net.fbvictorhugo.joystickbt.controller.BluetoothCmd.BTN_BUZZ;

public class JoyActivity extends BaseActivity {

private JoystickView joystick;
private Button buttonLed;
private Button buttonBuzz;
private JoystickView mJoystickView;
private SwitchCompat mSwitchLed;
private ImageButton mButtonBuzz;
private Toolbar mToolbar;

private boolean isLedOn;
private TextView mTextJoystickStatus;

private int mLastMoveCmd = -42;

Expand All @@ -39,7 +41,7 @@ protected void onCreate(Bundle savedInstanceState) {
configureButtonBuzz();

if (getSupportActionBar() != null) {
String nameBT = application.appBluetoothDevice.getName();
String nameBT = application.appBluetoothModel.getName();
getSupportActionBar().setSubtitle(String.format(getString(R.string.connected_on), nameBT));
}
}
Expand All @@ -53,30 +55,31 @@ protected void onDestroy() {
private void findViews() {
mToolbar = findViewById(R.id.toolbar);

joystick = findViewById(R.id.joystickView);
buttonLed = findViewById(R.id.btnLed);
buttonBuzz = findViewById(R.id.btnBuzz);
mJoystickView = findViewById(R.id.joystickView);
mSwitchLed = findViewById(R.id.switchLed);
mButtonBuzz = findViewById(R.id.btnBuzz);
mTextJoystickStatus = findViewById(R.id.joystick_tv_status);
}

private void configureButtonLed() {

buttonLed.setOnClickListener(new View.OnClickListener() {

mSwitchLed.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onClick(View v) {
if (isLedOn) {
turnOffLed();
isLedOn = false;
} else {
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {

if (checked) {
turnOnLed();
isLedOn = true;
} else {
turnOffLed();
}
}
});
}

private void configureButtonBuzz() {

buttonBuzz.setOnClickListener(new View.OnClickListener() {
mButtonBuzz.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buzz();
Expand All @@ -85,29 +88,38 @@ public void onClick(View v) {
}

private void configureJoystick() {
joystick.setOnMoveListener(mJoystickMoveLister);
joystick.setAutoReCenterButton(true);
mJoystickView.setOnMoveListener(mJoystickMoveLister);
mJoystickView.setAutoReCenterButton(true);
}

JoystickView.OnMoveListener mJoystickMoveLister = new JoystickView.OnMoveListener() {
@Override
public void onMove(int angle, int strength) {

String axis = "";
if (angle == 0) {
sendMoveBTCmd(BluetoothCmd.AXIS_CENTERED);
axis = "AXIS_CENTERED";
} else if (angle > 0 && angle <= 70) {
sendMoveBTCmd(BluetoothCmd.AXIS_RIGHT_UP);
axis = "AXIS_RIGHT_UP";
} else if (angle > 70 && angle <= 105) {
sendMoveBTCmd(BluetoothCmd.AXIS_UP);
axis = "AXIS_UP";
} else if (angle > 105 && angle <= 180) {
sendMoveBTCmd(BluetoothCmd.AXIS_LEFT_UP);
axis = "AXIS_LEFT_UP";
} else if (angle > 180 && angle <= 255) {
sendMoveBTCmd(BluetoothCmd.AXIS_LEFT_DOWN);
axis = "AXIS_LEFT_DOWN";
} else if (angle > 255 && angle <= 285) {
sendMoveBTCmd(BluetoothCmd.AXIS_DOWN);
axis = "AXIS_DOWN";
} else if (angle > 285 && angle <= 360) {
sendMoveBTCmd(BluetoothCmd.AXIS_RIGHT_DOWN);
axis = "AXIS_RIGHT_DOWN";
}
mTextJoystickStatus.setText(String.format(getString(R.string.angle), angle, axis));
}
};

Expand All @@ -120,16 +132,16 @@ private void sendMoveBTCmd(final char cmd) {
mLastMoveCmd = cmd;
}

private void turnOffLed() {
sendBluetoothCmd(BluetoothCmd.BTN_A_ACTION_DOWN);
private void turnOnLed() {
sendBluetoothCmd(BluetoothCmd.BTN_LED_ON);
}

private void turnOnLed() {
sendBluetoothCmd(BluetoothCmd.BTN_A_ACTION_UP);
private void turnOffLed() {
sendBluetoothCmd(BluetoothCmd.BTN_LED_OFF);
}

private void buzz() {
sendBluetoothCmd(BTN_B_ACTION_DOWN);
sendBluetoothCmd(BTN_BUZZ);
}

//endregion
Expand Down
Binary file added app/src/main/res/drawable-v24/bg_joystick.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-v24/finger_joystick.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions app/src/main/res/drawable/ic_horn.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<vector android:height="24dp" android:viewportHeight="44"
android:viewportWidth="72.131" android:width="39dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FFFFFF" android:pathData="M2.645,14.2661C2.645,16.27 4.248,18.754 6.251,18.754L45.282,18.594C58.346,17.311 68.284,8.976 68.284,0L72.131,0L72.131,44L68.284,44C68.284,36.867 61.953,30.135 52.736,27.009C54.499,28.532 55.541,30.215 55.541,32.138C55.541,38.23 44.962,42.8781 30.936,42.8781C16.911,42.8781 6.412,38.23 6.412,32.138C6.412,29.494 8.415,27.0891 11.862,25.246L6.251,25.246C4.248,25.246 2.645,27.73 2.645,29.734L0,29.734L0,14.2661L2.645,14.2661ZM11.381,32.138C11.381,34.944 18.995,38.951 31.016,38.951C42.958,38.951 50.652,34.944 50.652,32.138C50.652,29.574 44,25.807 33.341,25.326L29.494,25.326C18.353,25.647 11.381,29.494 11.381,32.138Z"/>
</vector>
Loading

0 comments on commit 3d4914a

Please sign in to comment.