Yorumlar
Deneyeceğim teşekkür ederim
Çok teşekkür ederim, ben de başvurayım o zaman. Eğer sistemi aktif ederlerse, bilgi paylaşımına katkımız bulunur.
merhaba, çok teşekkür ederim
package com.example.trial;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
public class MainActivity2 extends AppCompatActivity {
private static final String TAG = "MainActivity2";
Button buttonBack, buttonForward, buttonBackward, buttonLeft, buttonRight,denemeButton;
static final UUID mUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
BluetoothSocket btSocket = null;
BluetoothAdapter btAdapter;
BluetoothDevice hc05;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
buttonBack = (Button) findViewById(R.id.back);
buttonForward = (Button) findViewById(R.id.forward);
buttonBackward = (Button) findViewById(R.id.backward);
buttonLeft = (Button) findViewById(R.id.left);
buttonRight = (Button) findViewById(R.id.right);
denemeButton = (Button) findViewById(R.id.deneme);
backMethod();
//forward();
//backward();
//left();
//right();
zero();
buttonSit();
btAdapter = BluetoothAdapter.getDefaultAdapter();
hc05 = btAdapter.getRemoteDevice("00:20:10:09:07:AA");
btSocketMethod();
InputStream inputStream = null;
try {
inputStream = btSocket.getInputStream();
inputStream.skip(inputStream.available());
for (int i = 0; i < 10; i++) {
byte b = (byte) inputStream.read();
System.out.println((char) b);
}
} catch (IOException e) {
e.printStackTrace();
}
denemeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InputStream inputStream = null;
try {
inputStream = btSocket.getInputStream();
inputStream.skip(inputStream.available());
for (int i = 0; i < 30; i++) {
byte b = (byte) inputStream.read();
System.out.println((char) b);
}
}catch (IOException e){
e.printStackTrace();
}
}
});
}
private void btSocketMethod() {
int counter = 0;
do {
try {
btSocket = hc05.createRfcommSocketToServiceRecord(mUUID);
System.out.println(btSocket);
btSocket.connect();
System.out.println(btSocket.isConnected());
} catch (IOException e) {
e.printStackTrace();
}
counter++;
} while (!btSocket.isConnected() && counter < 3);
}
private void buttonSit() {
buttonForward.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
try {
OutputStream outputStream = btSocket.getOutputStream();
outputStream.write(1);
} catch (IOException e) {
e.printStackTrace();
}
}
else if(event.getAction() == MotionEvent.ACTION_UP){
try {
OutputStream outputStream = btSocket.getOutputStream();
outputStream.write(0);
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
});
buttonBackward.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
try {
OutputStream outputStream = btSocket.getOutputStream();
outputStream.write(4);
} catch (IOException e) {
e.printStackTrace();
}
}
else if(event.getAction() == MotionEvent.ACTION_UP){
try {
OutputStream outputStream = btSocket.getOutputStream();
outputStream.write(0);
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
});
buttonRight.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
try {
OutputStream outputStream = btSocket.getOutputStream();
outputStream.write(2);
} catch (IOException e) {
e.printStackTrace();
}
}
else if(event.getAction() == MotionEvent.ACTION_UP){
try {
OutputStream outputStream = btSocket.getOutputStream();
outputStream.write(0);
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
});
buttonLeft.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
try {
OutputStream outputStream = btSocket.getOutputStream();
outputStream.write(3);
} catch (IOException e) {
e.printStackTrace();
}
}
else if(event.getAction() == MotionEvent.ACTION_UP){
try {
OutputStream outputStream = btSocket.getOutputStream();
outputStream.write(0);
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
});
}
/*private BroadcastReceiver mBroadcastReceiver1 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Log.d(TAG, "onReceive: ACTION FOUND.");
if (action.equals(BluetoothDevice.ACTION_FOUND)){
device = intent.getParcelableExtra (BluetoothDevice.EXTRA_DEVICE);
}
}
};*/
private void zero() {
denemeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InputStream inputStream = null;
try {
inputStream = btSocket.getInputStream();
inputStream.skip(inputStream.available());
for (int i = 0; i < 26; i++) {
byte b = (byte) inputStream.read();
System.out.println((char) b);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
/*private void forward() {
buttonForward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
OutputStream outputStream = btSocket.getOutputStream();
outputStream.write(1);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void backward() {
buttonBackward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
OutputStream outputStream = btSocket.getOutputStream();
outputStream.write(4);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void left() {
buttonLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
OutputStream outputStream = btSocket.getOutputStream();
outputStream.write(2);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void right() {
buttonRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
OutputStream outputStream = btSocket.getOutputStream();
outputStream.write(3);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}*/
private void backMethod() {
buttonBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
}
}
Merhaba kodları incelediniz mi? Bir fikriniz var mı?