public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{
private final static UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
protected VerticalSeekBar kierunek;
protected VerticalSeekBar predkosc;
protected TextView wyswietlPredkosc;
protected TextView wyswietlKierunek;
protected TextView wartoscNapiecia;
protected int wartoscKierunku=0;
protected int wartoscPredkosci=0;
protected BluetoothAdapter mojBluetooth;
protected ToggleButton polacz;
protected ToggleButton wysylaj;
protected ToggleButton spoczynekButton;
protected ToggleButton wsteczny;
protected ToggleButton odbieraj;
private static final int ENABLE_BT_REQUEST_CODE = 1;
protected int wysylanyKierunek=0;//przechowuje zmienne dotyczące zmiany kierunku jazdy
protected int wysylanaPredkosc=0;//przechowuje zmienne dotyczące zmiany prędkości
protected int wstecznyBoolean=0;//ustala czy samochód ma jechać w przód czy w tył
protected int R1=2120;//rezystancja opornika 1
protected int R2=4550;//rezystancja opornika 2
private BluetoothSocket bluetoothSocket;
private BluetoothDevice bluetoothDevice;
private OutputStream outputstream;//obiekt umożliwiający wysyłanie danych
private InputStream inputstream;//obiekt umożliwiający odbiór danych
protected Handler mHandler;//obiekt odpowiedzialny za przechwyt odpowiednich danych z wątku
private int SPOCZYNEK_STAN=0;//zmienna stanu
private int ODBIOR_STAN=1;//zmienna stanu
private int WYSYLAJ_STAN=0;//zmienna stanu
private int CZAS_STAN=0;//zmienna stanu
private int ODBIOR_WATEK_STAN=0;//zmienna stanu
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
kierunek=(VerticalSeekBar)findViewById(R.id.KIERUNEK);
predkosc=(VerticalSeekBar)findViewById(R.id.PREDKOSC);
wyswietlPredkosc=(TextView)findViewById(R.id.wyswietl_predkosc);
wyswietlKierunek=(TextView)findViewById(R.id.wyswietl_kierunek);
polacz=(ToggleButton)findViewById(R.id.POLACZ);
wysylaj=(ToggleButton)findViewById(R.id.WYSYLAJ);
spoczynekButton=(ToggleButton)findViewById(R.id.SPOCZYNEK);
wsteczny=(ToggleButton)findViewById(R.id.WSTECZNY);
odbieraj= (ToggleButton) findViewById(R.id.ODBIERAJ);
wartoscNapiecia= (TextView) findViewById(R.id.WARTOSC_NAPIECIA);
final Timer czas;
czas=new Timer();
final MyTimerTask myTimerTask;
myTimerTask = new MyTimerTask();
wlaczanieBluetooth();
polacz.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
BluetoothDevice bluetoothDevice = mojBluetooth.getRemoteDevice("98:D3:33:80:66:65");
ConnectingThread t = new ConnectingThread(bluetoothDevice);
t.start();
while(bluetoothSocket.isConnected()==false)
{
}
Toast toast2 = Toast.makeText(getApplicationContext(), "Połączono", Toast.LENGTH_LONG);
toast2.show();
//SprawdzeniePolaczenia sprawdzeniePolaczenia=new SprawdzeniePolaczenia();
//sprawdzeniePolaczenia.start();
} else {
if(bluetoothSocket.isConnected()==true) {
try {
bluetoothSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
wysylaj.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b)
{
if(bluetoothSocket.isConnected()==true) {
ODBIOR_STAN = 0;
WYSYLAJ_STAN = 1;
odbieraj.setChecked(false);
if (CZAS_STAN == 0) {
czas.schedule(myTimerTask, 0, 50);
CZAS_STAN = 1;
}
if (ODBIOR_STAN == 0) {
wartoscNapiecia.setText("Wyłącz wysyłanie, aby wyświetlić napięcie");
Toast toast = Toast.makeText(getApplicationContext(), "Wyłącz wysyłanie, aby odebrać napięcie", Toast.LENGTH_SHORT);
toast.show();
}
}
else
{
Toast toast = Toast.makeText(getApplicationContext(), "Brak połączenia", Toast.LENGTH_SHORT);
toast.show();
wysylaj.setChecked(false);
}
}
else
{
WYSYLAJ_STAN=0;
}
}
});
spoczynekButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b)
{
predkosc.setProgressAndThumb(0);
kierunek.setProgressAndThumb(kierunek.getMax()/2);
SPOCZYNEK_STAN=1;
}
else
{
SPOCZYNEK_STAN=0;
}
}
});
wsteczny.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b)
{
wstecznyBoolean=50;
predkosc.setProgressAndThumb(0);
}
else
{
wstecznyBoolean=0;
predkosc.setProgressAndThumb(0);
}
}
});
odbieraj.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
if(bluetoothSocket.isConnected()==true)
{
if(WYSYLAJ_STAN==0)
{
ODBIOR_STAN=1;
}
else
{
odbieraj.setChecked(false);
Toast toast = Toast.makeText(getApplicationContext(), "Wyłącz wysyłanie, aby odebrać napięcie", Toast.LENGTH_SHORT);
toast.show();
}
if(ODBIOR_WATEK_STAN==0){
ODBIOR_WATEK_STAN=1;
WatekOdbierania watekOdbierania=new WatekOdbierania();
watekOdbierania.start();
}
}
else
{
Toast toast = Toast.makeText(getApplicationContext(), "Brak połączenia", Toast.LENGTH_SHORT);
toast.show();
odbieraj.setChecked(false);
}
}
else
{
ODBIOR_STAN=0;
}
}
});
mHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
wyswietlOdebraneDane(msg, R1, R2);
}
};
zmianaKierunku();
zmianaPredkosci();
}
private void wlaczanieBluetooth() {
mojBluetooth=BluetoothAdapter.getDefaultAdapter();
if(mojBluetooth==null)
{
Toast.makeText(getApplicationContext(),"Brak modułu bluetooth!",Toast.LENGTH_LONG).show();
}
else
{
if(!mojBluetooth.isEnabled())
{
Intent intent=new Intent(mojBluetooth.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, ENABLE_BT_REQUEST_CODE);
}
}
}
protected void zmianaKierunku()
{
kierunek.setOnSeekBarChangeListener(
new VerticalSeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
wartoscKierunku = 2 * (kierunek.getProgress() - 45);
wyswietlKierunek.setText("" + wartoscKierunku);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
}
protected void zmianaPredkosci() {
predkosc.setOnSeekBarChangeListener(
new VerticalSeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
wartoscPredkosci = 2 * (predkosc.getProgress() - 50);
wyswietlPredkosc.setText("" + wartoscPredkosci);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
};
private class ConnectingThread extends Thread {
public ConnectingThread(BluetoothDevice device) {
BluetoothSocket temp = null;
bluetoothDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
temp = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
e.printStackTrace();
}
bluetoothSocket = temp;
}
public void run() {
try {
bluetoothSocket.connect();
outputstream=bluetoothSocket.getOutputStream();
inputstream=bluetoothSocket.getInputStream();
} catch (IOException connectException) {
connectException.printStackTrace();
try {
bluetoothSocket.close();
} catch (IOException closeException) {
closeException.printStackTrace();
}
}
}
public void cancel() {
try {
bluetoothSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
protected void wyslij(int s) throws IOException {
outputstream.write(s);
}