Ich habe das Problem das wenn ich meine App debugge der socket ein timeout hat
Code:
E/HAL: load: id=gralloc != hmi->id=gralloc
I/OpenGLRenderer: Initialized EGL, version 1.4
I/HwSecImmHelper: mSecurityInputMethodService is null
I/HwSecImmHelper: mSecurityInputMethodService is null
W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
E/MainActivity: Socket x kann nicht verbinden: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
Java:
package com.example.marcel_v.app2;
import android.bluetooth.*;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import java.io.IOException;
import java.io_OutputStream;
import java.util.Set;
import java.util.UUID;
import android.widget.Button;
import java.io.InputStream;
public class AuswahlActivity extends AppCompatActivity {
private static String mac_adresse = "B8:27:EB:46:CE:3A";
private final static String TAG = "MainActivity";
private BluetoothAdapter mBluetoothAdapter;
private BluetoothDevice mDevice;
private Button verbinden;
private boolean is_connected = false;
private UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private BluetoothSocket socket = null;
String test = "erfolgreich";
private OutputStream outputStream = null;
private InputStream inputStream = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auswahl);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
findRaspberry();
}
public void Steuerung(View view){
Intent i = new Intent(this, Steuerung.class);
startActivity(i);
}
private void findRaspberry(){
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
for(BluetoothDevice device : pairedDevices){
if(device.getName().equals("raspberrypi-0"))this.mDevice = device;
}
}
public void verbinden(View view){
Log.d(TAG, "Verbinde mit " + mac_adresse);
BluetoothDevice remote_device = mBluetoothAdapter.getRemoteDevice(mac_adresse);
// Socket erstellen
try {
socket = remote_device
.createInsecureRfcommSocketToServiceRecord(uuid);
Log.d(TAG, "Socket erstellt");
} catch (Exception e) {
Log.e(TAG, "Socket Erstellung fehlgeschlagen: " + e.toString());
}
mBluetoothAdapter.cancelDiscovery();
// Socket verbinden
try {
socket.connect();
Log.d(TAG, "Socket verbunden");
is_connected = true;
((Button) findViewById(R.id.verbinden))
.setBackgroundColor(Color.GREEN);
} catch (IOException e) {
is_connected = false;
Log.e(TAG, "Socket x kann nicht verbinden: " + e.toString());
((Button)findViewById(R.id.verbinden))
.setBackgroundColor(Color.RED);
}
// Socket beenden, falls nicht verbunden werden konnte
if (!is_connected) {
try {
socket.close();
} catch (Exception e) {
Log.e(TAG,
"Socket kann nicht beendet werden: " + e.toString());
}
}
// Outputstream erstellen:
try {
outputStream = socket.getOutputStream();
Log.d(TAG, "OutputStream erstellt");
} catch (IOException e) {
Log.e(TAG, "OutputStream Fehler: " + e.toString());
is_connected = false;
}
// Inputstream erstellen
try {
inputStream = socket.getInputStream();
Log.d(TAG, "InputStream erstellt");
} catch (IOException e) {
Log.e(TAG, "InputStream Fehler: " + e.toString());
is_connected = false;
}
//test nachricht senden
byte[] msgBuffer = test.getBytes();
if (is_connected) {
Log.d(TAG, "Sende Nachricht: " + test);
try {
outputStream.write(msgBuffer);
} catch (IOException e) {
Log.e(TAG,
"Bluetest: Exception beim Senden: " + e.toString());
}
}
//test nachricht empfangen
}
}
Zuletzt bearbeitet von einem Moderator: