Android Bluetooth Yanlış Veri Okuma
03.03.2020 - 02:26
Herkese merhabalar. Android Studio'da bluetooth kullanarak veri okumaya çalışıyorum. 012 değerini gönderiyorum fakat gelen veriyi terminal ekranına yazdırdığımda verinin
0
12
şeklinde geldiğini görüyorum. İlgili text'ede 12 verisini yazıyor. Bu durumu düzeltmem için ne yapmam gerekiyor? Yardımlarınız için teşekkür ederim.
package com.example.temel;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
static final UUID mUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private InputStream mmInStream =null;
private final OutputStream mmOutStream =null;
byte[] readBuffer = new byte[1024];
int readBytes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
System.out.println(btAdapter.getBondedDevices());
BluetoothDevice hc05 = btAdapter.getRemoteDevice("00:18:E4:0A:00:01");
System.out.println(hc05.getName());
BluetoothSocket btSocket = null;
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);// while (!btSocket.isConnected() && counter < 3);
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = btSocket.getInputStream();
outputStream = btSocket.getOutputStream();
inputStream.skip(inputStream.available());
/*
while(true)
{
// readBytes = inputStream.read(readBuffer);
// String readMessage = new String(readBuffer, 0, readBytes);
// System.out.println(readMessage);
// outputStream.write(readMessage.toString().getBytes());
//byte b = (byte) inputStream.read();
readBytes = inputStream.read(readBuffer);
String s = new String(readBuffer,0,readBytes);
System.out.println(s);
// outputStream.write((char)b);
}
*/
} catch (IOException e) {
e.printStackTrace();
}
mmInStream = inputStream;
while (true)
{
try {
mmInStream.read(readBuffer);
readBytes = inputStream.read(readBuffer);
String s = new String(readBuffer,0,readBytes);
System.out.println(s);
} catch (IOException e) {
e.printStackTrace();
}
}
/*
try {
btSocket.close();
System.out.println(btSocket.isConnected());
} catch (IOException e) {
e.printStackTrace();
}
*/
}
}
63
Görüntülenme
0 Beğeni