Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.powerful.platformcontrol;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Handler;
- import android.os.Vibrator;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.CheckBox;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.io.IOException;
- import java.util.Objects;
- import java.util.Set;
- import java.util.Timer;
- import java.util.TimerTask;
- import java.util.UUID;
- public class MainScreen extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main_screen);
- final boolean stopWorker=false;
- Thread workerThread;
- final String data = null;
- final Handler handler = new Handler();
- workerThread = new Thread(new Runnable()
- {
- public void run()
- {
- if (found==true)
- while(!Thread.currentThread().isInterrupted() && !stopWorker)
- { Log.d("bt","Thread run");
- try {
- int bytesAvailable = mmInputStream.available();
- byte[] readBuffer = new byte[0];
- int readBufferPosition=0;
- byte[] packetBytes = new byte[0];
- //inits finished
- if (bytesAvailable > 0) {
- packetBytes = new byte[bytesAvailable];
- mmInputStream.read(packetBytes);
- Log.d("bt", "BT received");
- }
- for(int i=0;i<bytesAvailable;i++)
- {
- byte b = packetBytes[i];
- byte delimiter=10;
- if(b == delimiter)
- {
- byte[] encodedBytes = new byte[readBufferPosition];
- System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
- final String data = new String(encodedBytes, "US-ASCII");
- readBufferPosition = 0;
- //The variable data now contains our full command
- }
- else
- {
- readBuffer[readBufferPosition++] = b;
- }
- }
- handler.post(new Runnable()
- {
- public void run()
- {
- if (data=="T")
- {
- Vibrator v = (Vibrator) getSystemService(VIBRATOR_SERVICE);
- // Vibrate for 500 milliseconds
- v.vibrate(500);
- Log.d("bt", "vibrate command given");
- }
- }
- });
- } catch (IOException e) {
- e.printStackTrace();}
- }
- }
- });
- workerThread.start();
- }
- public boolean found=false;
- public int curSpeed=200;
- public int countdown=-1;
- public Timer timer= new Timer();
- public java.io.OutputStream mmOutputStream;
- public java.io.InputStream mmInputStream;
- public void CheckSyncButtonPress(View view) {
- int REQUEST_ENABLE_BT=1;
- //sync button pressed
- //check that we can use bluetooth
- BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- // Device supports bluetooth so let's try and enable it
- if (mBluetoothAdapter == null) {
- // Device does not support Bluetooth
- }
- else {
- if (!mBluetoothAdapter.isEnabled()) {
- //enable message popup
- Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
- }
- else
- {//if it IS enabled
- Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
- android.bluetooth.BluetoothDevice targetDevice = null; //variable for found device
- String targetName="HC-06";
- if (pairedDevices.size() > 0) {
- // If there are paired devices
- // Loop through paired devices
- for (BluetoothDevice device : pairedDevices) {
- if (Objects.equals(device.getName().trim(), targetName))
- {
- targetDevice=device;
- found=true;
- //found one that has the right name
- }
- }
- }
- if (found==true)
- {
- //we have a target device for connection
- UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
- try {
- android.bluetooth.BluetoothSocket mmSocket = targetDevice.createRfcommSocketToServiceRecord(uuid);
- mmSocket.connect();
- mmOutputStream = mmSocket.getOutputStream();
- mmInputStream = mmSocket.getInputStream();
- CheckBox checkBox = (CheckBox)findViewById(R.id.bxConnectStatus);
- checkBox.setChecked(true);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- public void EnableButtonPress(View view) {
- Log.d("Button","Enablepressed");
- if (found==true){
- try {
- mmOutputStream.write("N".getBytes());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public void DisableButtonPress(View view) {
- if (found==true){
- try {
- mmOutputStream.write("O".getBytes());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public void TrackingModeButtonPress(View view) {
- if (found==true){
- try {
- mmOutputStream.write("T".getBytes());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public void DriveModeButtonPress(View view) {
- if (found==true){
- try {
- mmOutputStream.write("F".getBytes());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public void ResetModeButtonPress(View view) {
- if (found==true){
- try {
- mmOutputStream.write("R".getBytes());
- /*timer.schedule(new TimerTask(){
- @Override
- public void run() {
- doStuff();
- }
- },0,120000 );*/
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public void SpeedUpButtonPress(View view) {
- if (found==true){
- try {
- mmOutputStream.write("+".getBytes());
- curSpeed+=1;
- TextView counttext= (TextView)findViewById(R.id.txCurSpeed);
- counttext.setText(Integer.toString(curSpeed));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public void SpeedDownButtonPress(View view) {
- if (found==true){
- try {
- mmOutputStream.write("-".getBytes());
- curSpeed-=1;
- TextView counttext= (TextView)findViewById(R.id.txCurSpeed);
- counttext.setText(Integer.toString(curSpeed));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public void SpeedResetButtonPress(View view) {
- if (found==true){
- try {
- mmOutputStream.write("S".getBytes());
- curSpeed=200;
- TextView counttext= (TextView)findViewById(R.id.txCurSpeed);
- counttext.setText(Integer.toString(curSpeed));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public void doStuff()
- {
- try {
- mmOutputStream.write("T".getBytes());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement