Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Electric_Boat_App/Electric_Boat_Application/app/src/main/java/example/botton/testapplication/MainActivity.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
169 lines (145 sloc)
5.53 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example.botton.testapplication; | |
import android.bluetooth.BluetoothAdapter; | |
import android.bluetooth.BluetoothSocket; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.os.ParcelUuid; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.SeekBar; | |
import android.widget.TextView; | |
import android.bluetooth.BluetoothDevice; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.util.Set; | |
public class MainActivity extends AppCompatActivity | |
{ | |
//declare variables | |
SeekBar seekbar1, seekbar2; //Your SeekBar | |
SeekBarChangeListener listener1, listener2; | |
TextView result; //The TextView which will display the result | |
BluetoothAdapter BA = BluetoothAdapter.getDefaultAdapter(); | |
BluetoothSocket robotSock; | |
OutputStream outputStream; | |
/** | |
* A listener for the seek bars | |
* Designed to be somewhat agnostic, so that the seekbar it corresponds to can represent either a motor or servo | |
* Thus to change between tank steering and servo steering, you should only need to modify the Arduino code | |
*/ | |
private class SeekBarChangeListener implements SeekBar.OnSeekBarChangeListener | |
{ | |
private int idNum, value; | |
private static final int RANGE = 7; //range of the control axis in one direction (e.g. if RANGE = 7, the range of possible values to send is -7 to 7) | |
public SeekBarChangeListener(int num) | |
{ | |
this.idNum = num; | |
this.value = 0; | |
} | |
public int getValue() | |
{ | |
return value; | |
} | |
@Override | |
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) | |
{ | |
//result.setText ("Value:"+ progress); | |
this.value = progress - RANGE; | |
seekBarChanged(); | |
} | |
@Override | |
public void onStartTrackingTouch(SeekBar seekBar){} | |
@Override | |
public void onStopTrackingTouch(SeekBar seekBar) | |
{ | |
seekBar.setProgress(RANGE); | |
} | |
} | |
public void exitMethod(){ | |
System.exit(0); | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
seekbar1 = (SeekBar)findViewById(R.id.motorBar1); | |
seekbar2 = (SeekBar)findViewById(R.id.motorBar2); | |
seekbar1.setProgress(SeekBarChangeListener.RANGE); | |
seekbar2.setProgress(SeekBarChangeListener.RANGE); | |
//result = (TextView)findViewById(R.id.tvResult); | |
//set change listener | |
listener1 = new SeekBarChangeListener(1); | |
listener2 = new SeekBarChangeListener(2); | |
seekbar1.setOnSeekBarChangeListener(listener1); | |
seekbar2.setOnSeekBarChangeListener(listener2); | |
if(!BA.isEnabled()){ | |
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); | |
startActivityForResult(turnOn, 0); | |
} | |
Set<BluetoothDevice> pairedDevices; | |
pairedDevices = BA.getBondedDevices(); | |
Log.d("some tag", "DOES IT WORK?"); | |
BluetoothDevice btModule = null; | |
for (BluetoothDevice bt : pairedDevices) | |
{ | |
Log.d("some tag", bt.getName()); | |
if ("HC-06".equals(bt.getName())) | |
btModule = bt; | |
} | |
try { | |
ParcelUuid[] uuids = btModule.getUuids(); | |
robotSock = btModule.createRfcommSocketToServiceRecord(uuids[0].getUuid()); | |
// UUID myUUID = UUID.fromString("00000000-0000-1000-8000-00805F9B34FB"); | |
// BluetoothSocket robotSock = btModule.createRfcommSocketToServiceRecord(myUUID); | |
Log.d("bt", "Created a connection with the JY-MCU"); | |
robotSock.connect(); | |
outputStream = robotSock.getOutputStream(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void seekBarChanged() | |
{ | |
byte toSend = (byte)(listener1.getValue()<<4 | (listener2.getValue() & 0xf)); | |
Log.d("seekbars", String.format("Motor value: %d\nServo value: %d", listener1.getValue(), listener2.getValue())); | |
//Log.d("bt", String.format("Sending value: %x", toSend)); | |
sendToRobot(toSend); | |
} | |
public void sendToRobot(byte msg) { | |
if (outputStream != null) | |
{ | |
try { | |
byte[] bytes = {msg}; | |
outputStream.write(bytes); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
else | |
{ | |
Log.d("bt", "output stream hasn't been created yet. Please make sure that a device is connected"); | |
} | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |