activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context="com.example.user.joysyickbb8.MainActivity">
<io.github.controlwear.virtual.joystick.android.JoystickView
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="@+id/joystickView"
app:JV_borderWidth="4dp"
app:JV_fixedCenter="false"
app:JV_buttonColor="#FF8C00"
app:JV_borderColor="#FF8C00"
app:JV_backgroundColor="#FFFFFF"
app:JV_buttonSizeRatio="20%"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kąt"
android:id="@+id/tv_angle"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="siła"
android:id="@+id/tv_strength"
android:layout_alignParentEnd="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kierunek"
android:id="@+id/id_arrow"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/id_bt_connect"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="@+id/id_arrow"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:text="Button" />
</RelativeLayout>
MainActivity.java
package com.example.user.joysyickbb8;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
import io.github.controlwear.virtual.joystick.android.JoystickView;
public class MainActivity extends AppCompatActivity {
private final String DEVICE_ADDRESS = "B8:27:BB:26:7D:77";
private final UUID PORT_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
private BluetoothDevice device;
private BluetoothSocket socket;
private OutputStream outputStream;
Button bluetooth_connect_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final JoystickView joystickView= (JoystickView) findViewById(R.id.joystickView);
final TextView tv_angle= (TextView) findViewById(R.id.tv_angle);
final TextView tv_strength= (TextView) findViewById(R.id.tv_strength);
final TextView id_arrow = (TextView) findViewById(R.id.id_arrow);
bluetooth_connect_btn=(Button) findViewById(R.id.id_bt_connect);
joystickView.setOnMoveListener(new JoystickView.OnMoveListener() {
@Override
public void onMove(int angle, int strength){
Object arrow;
tv_angle.setText("kąt: " + angle);
tv_strength.setText("siła: " + strength + "%");
if (strength == 0)
{
arrow = "x";
id_arrow.setText("kierunek: " + arrow);
try
{
outputStream.write(arrow.toString().getBytes());
}
catch (IOException e)
{
e.printStackTrace();
}
}
else if ((angle>315 || angle<=45) & strength !=0) {
arrow = "d";
id_arrow.setText("kierunek: " + arrow);
try
{
outputStream.write(arrow.toString().getBytes());
}
catch (IOException e) {
e.printStackTrace();
}
}
else if (angle > 45 & angle <=135 & strength !=0){
arrow = "w";
id_arrow.setText("kierunek: " + arrow);
try
{
outputStream.write(arrow.toString().getBytes());
}
catch (IOException e)
{
e.printStackTrace();
}
}
else if (angle > 135 & angle <= 225 & strength !=0){
arrow = "a";
id_arrow.setText("kierunek: " + arrow);
try
{
outputStream.write(arrow.toString().getBytes());
}
catch (IOException e)
{
e.printStackTrace();
}
}
else if (angle > 225 & angle<=315 & strength !=0){
arrow = "s";
id_arrow.setText("kierunek: " + arrow);
try
{
outputStream.write(arrow.toString().getBytes());
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
});
bluetooth_connect_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(BTinit()){
BTconnect();
}
}
});
}
public boolean BTinit()
{
boolean found =false;
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null)
{
Toast.makeText(getApplicationContext(),"Urządzenie nie wspiera bluetooth",Toast.LENGTH_SHORT).show();
}
if (!bluetoothAdapter.isEnabled())
{
Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableAdapter,0);
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
if(bondedDevices.isEmpty())
{
Toast.makeText(getApplicationContext(),"Sparuj urządzenia",Toast.LENGTH_SHORT).show();
}
else
{
for (BluetoothDevice interator:bondedDevices)
{
if (interator.getAddress().equals(DEVICE_ADDRESS))
{
device = interator;
found = true;
break;
}
}
}
return found;
}
public boolean BTconnect()
{
boolean connected = true;
try
{
socket = device.createRfcommSocketToServiceRecord(PORT_UUID);
socket.connect();
Toast.makeText(getApplicationContext(),"Połączono z bluetooth",Toast.LENGTH_SHORT).show();
}
catch(IOException e)
{
e.printStackTrace();
connected=false;
}
if (connected)
{
try
{
outputStream = socket.getOutputStream();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return connected;
}
@Override
protected void onStart()
{
super.onStart();
}
}