Ich habe eine App geschrieben, mit der ich mich per button mit dem Pi verbinden will. Um dies zu Testen wird zudem noch eine Nachricht an das Pi gesendet, welches daraufhin eine Nachricht an die App Senden soll.
Beim debuggen tritt dann immer wieder die folgende Fehlermeldung auf:
"read failed, socket might closed or timeout, read ret: -1"
Die Konsole gibt folgendes aus:
D/HwuiUseBinaryProgram: Key: 68719476744 has not found in mBinaryEntries, Compile it.
I/HwSecImmHelper: mSecurityInputMethodService is null
I/HwSecImmHelper: mSecurityInputMethodService is null
V/AudioManager: playSoundEffect effectType: 0
V/AudioManager: querySoundEffectsEnabled...
D/MessageThread: Versuch Test Nachricht zu senden
W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
W/System.err: at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:700)
W/System.err: at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:712)
W/System.err: at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:381)
W/System.err: at com.example.marcel_v.app2.MessageThread.run(MessageThread.java:66)
Beim debuggen tritt dann immer wieder die folgende Fehlermeldung auf:
"read failed, socket might closed or timeout, read ret: -1"
Die Konsole gibt folgendes aus:
D/HwuiUseBinaryProgram: Key: 68719476744 has not found in mBinaryEntries, Compile it.
I/HwSecImmHelper: mSecurityInputMethodService is null
I/HwSecImmHelper: mSecurityInputMethodService is null
V/AudioManager: playSoundEffect effectType: 0
V/AudioManager: querySoundEffectsEnabled...
D/MessageThread: Versuch Test Nachricht zu senden
W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
W/System.err: at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:700)
W/System.err: at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:712)
W/System.err: at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:381)
W/System.err: at com.example.marcel_v.app2.MessageThread.run(MessageThread.java:66)
Java:
package com.example.marcel_v.app2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.invoke.MethodHandles;
import java.util.UUID;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;
/**
* Created by marcel_v on 05.02.18.
*/
public class MessageThread extends Thread {
private final static String TAG="MessageThread";
private final static String ma ="d4f0fd64-ad9d-4cfd-aa76-8d3541fbf008";
private BluetoothSocket mSocket=null;
private String mMessage;
public MessageThread(BluetoothDevice mBluetoothDevice, String message) {
Log.d(TAG, "Versuch Test Nachricht zu senden");
this.mMessage = message;
try{
UUID uuid = UUID.fromString(ma);
mSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(uuid);
}
catch (IOException e){
Log.d(TAG, "" + e);
}
}
private void manageConnectedSocket(BluetoothSocket socket) throws IOException {
try{
Log.d(TAG,"Connection successful");
OutputStream os=socket.getOutputStream();
PrintStream sender = new PrintStream(os);
sender.print(mMessage);
Log.d(TAG,"Message sent");
InputStream is=socket.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
Log.d(TAG,"Received: " + reader.readLine());
if (reader.equals("erfolgreich")){
Log.d(TAG,"Verbindung klappt" + reader);
}
}
catch (IOException e){
Log.d(TAG,"Fehler" + e);
}
}
public void run() {
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
try {
mSocket.connect();
manageConnectedSocket(mSocket);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.marcel_v.app2">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".AuswahlActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Steuerung"
android:label="@string/title_activity_steuerung"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
Java:
package com.example.marcel_v.app2;
import android.app.Activity;
import android.bluetooth.*;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.io.IOException;
import java.util.Set;
import android.widget.Button;
public class AuswahlActivity extends Activity {
public final static String TAG ="AuswaglActivity";
private BluetoothAdapter mBluetoothAdapter;
private BluetoothDevice mBluetoothDevice;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auswahl);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
findRaspberry();
if(mBluetoothDevice == null)
Log.d(TAG, "Pi wurde nicht gefunden");
}
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"))
this.mBluetoothDevice = device;
}
}
public void onSend(View view){
String message = "test";
new MessageThread(mBluetoothDevice, message).start();
}
}
Code:
import bluetooth
def empfangeNachricht():
server_sock= bluetooth.BluetoothSocket(bluetooth.RFCOMM)
port = 1200
server_sock.bind(("",port))
server_sock.listen(1)
client_sock,address = server_sock.accept()
print "Akzeptiere Verbindung von " + str(address)
data = client_sock.recv(1024)
print "Empfangen [%s]" % data
client_sock.close()
server_sock.close()
def sendeNachricht(targetBluetoothMacAddress):
port = 1200
sock=bluetooth.Bluetooth(bluetooth.RFCOMM)
sock.connect((targetBluetoothMacAddress, port))
sock.close()