Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.shpp_admin.bluetooth;
- import android.app.Activity;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- import android.bluetooth.BluetoothSocket;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import com.zerokol.views.JoystickView;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.HashMap;
- import java.util.Set;
- import java.util.Timer;
- import java.util.TimerTask;
- import java.util.UUID;
- public class MainActivity extends Activity implements View.OnClickListener {
- private static final UUID MY_UUID_SECURE =
- UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
- BluetoothSocket socket;
- EditText text;
- OutputStream stream;
- Timer timer;
- private JoystickView joystick;
- public static int spinnerPosition = 60;
- public static int moveForwardPosition = 410;
- public static int moveUpPosition = 310;
- public static boolean catched = false;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ((JoystickView) findViewById(R.id.joystickview)).setOnJoystickMoveListener(new JoystickView.OnJoystickMoveListener() {
- @Override
- public void onValueChanged(int angle, int power, int direction) {
- // TODO Auto-generated method stub
- switch (direction) {
- case JoystickView.FRONT:
- sendCommand((byte) 7);
- break;
- case JoystickView.FRONT_RIGHT:
- // directionTextView.setText(R.string.front_right_lab);
- break;
- case JoystickView.RIGHT:
- sendCommand((byte) 3);
- break;
- case JoystickView.RIGHT_BOTTOM:
- // directionTextView.setText(R.string.right_bottom_lab);
- break;
- case JoystickView.BOTTOM:
- sendCommand((byte) 8);
- break;
- case JoystickView.BOTTOM_LEFT:
- // directionTextView.setText(R.string.bottom_left_lab);
- break;
- case JoystickView.LEFT:
- sendCommand((byte) 4);
- break;
- case JoystickView.LEFT_FRONT:
- // directionTextView.setText(R.string.left_front_lab);
- break;
- default:
- // directionTextView.setText(R.string.center_lab);
- }
- }
- }, JoystickView.DEFAULT_LOOP_INTERVAL);
- findViewById(R.id.catchButton).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- sendCommand((byte) 1);
- }
- });
- findViewById(R.id.upButton).setOnClickListener(this);
- findViewById(R.id.downButton).setOnClickListener(this);
- timer = new Timer();
- HashMap<String, BluetoothDevice> deviceMap = new HashMap<>();
- BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
- for (BluetoothDevice bt : pairedDevices) {
- deviceMap.put(bt.getName(), bt);
- }
- BluetoothDevice device = deviceMap.get("Tinyos");
- try {
- socket = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);
- } catch (Exception io) {
- io.printStackTrace();
- }
- try {
- // Connect the device through the socket. This will block
- // until it succeeds or throws an exception
- socket.connect();
- } catch (IOException connectException) {
- Log.i("Errror", "Unable to connect");
- try {
- socket.close();
- } catch (IOException closeException) {
- closeException.printStackTrace();
- }
- }
- try {
- stream = socket.getOutputStream();
- } catch (Exception e) {
- }
- }
- public void sendCommand(byte message) {
- try {
- stream.write(message);
- } catch (IOException e) {
- Log.i("ERROR", "Error sending info");
- }
- }
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.downButton:
- sendCommand((byte) 6);
- break;
- case R.id.upButton:
- sendCommand((byte) 5);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement