Bluetoothübertragung funktioniert nicht

  • Antworten:7
Timur
  • Forum-Beiträge: 18

07.04.2014, 10:48:02 via Website

Moin,

ich sitze momentan daran, Daten über Bluetooth zu Senden/Empfangen.
Nun habe ich diesen Code geschrieben:
1package com.example.blue_final;
2
3import java.io.IOException;
4import java.io.InputStream;
5import java.io.OutputStream;
6import java.lang.reflect.InvocationTargetException;
7import java.lang.reflect.Method;
8
9import android.app.Activity;
10import android.bluetooth.BluetoothAdapter;
11import android.bluetooth.BluetoothDevice;
12import android.bluetooth.BluetoothSocket;
13import android.content.Intent;
14import android.os.Bundle;
15import android.view.View;
16import android.view.View.OnClickListener;
17import android.widget.Button;
18
19public class MainActivity extends Activity{
20
21 private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
22 private ConnectedThread connected;
23 private boolean isConnected = false;
24 //private Button verbinden;
25
26
27 @Override
28 protected void onCreate(Bundle savedInstanceState) {
29 super.onCreate(savedInstanceState);
30 setContentView(R.layout.activity_main);
31
32 //verbinden = (Button)findViewById(R.id.BtnVerbinden);
33 }
34
35
36 //Button ruft die Methode Verbindung auf!!!!
37 public void verbinden(View view){
38 Verbindung(view);
39 }
40
41 //Verbindung wird ausgeführt
42 private void Verbindung(final View verbindung){ //Muss mit Button aufgerufen werden
43 String address = "HIER STEHT DIE ADRESSE DES GERÄTES"; //Adresse vom Dongle
44 BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
45 ConnectThread connect = new ConnectThread(device);
46 connect.start();
47 connected.write(null);
48 }
49
50
51
52 private class ConnectThread extends Thread {
53 private final BluetoothSocket mmSocket;
54 private final BluetoothDevice mmDevice;
55
56 public ConnectThread(BluetoothDevice device) {
57 // Use a temporary object that is later assigned to mmSocket,
58 // because mmSocket is final
59 BluetoothSocket tmp = null;
60 mmDevice = device;
61
62 // Get a BluetoothSocket to connect with the given BluetoothDevice
63 try {
64 // MY_UUID is the app's UUID string, also used by the server code
65 Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
66 tmp = (BluetoothSocket) m.invoke(device, 1);
67 } catch (NoSuchMethodException e) {
68 // TODO Auto-generated catch block
69 e.printStackTrace();
70 } catch (IllegalAccessException e) {
71 // TODO Auto-generated catch block
72 e.printStackTrace();
73 } catch (IllegalArgumentException e) {
74 // TODO Auto-generated catch block
75 e.printStackTrace();
76 } catch (InvocationTargetException e) {
77 // TODO Auto-generated catch block
78 e.printStackTrace();
79 }
80 mmSocket = tmp;
81 }
82
83 public void run() {
84 // Cancel discovery because it will slow down the connection
85 mBluetoothAdapter.cancelDiscovery();
86
87 try {
88 // Connect the device through the socket. This will block
89 // until it succeeds or throws an exception
90 mmSocket.connect();
91 isConnected = true;
92 } catch (IOException connectException) {
93 // Unable to connect; close the socket and get out
94 try {
95 mmSocket.close();
96 } catch (IOException closeException) { }
97 return;
98 }
99
100 connected = new ConnectedThread(mmSocket);
101 connected.run();
102 }
103
104 /** Will cancel an in-progress connection, and close the socket */
105 public void cancel() {
106 try {
107 mmSocket.close();
108 } catch (IOException e) { }
109 }
110 }
111
112 private class ConnectedThread extends Thread {
113 private final BluetoothSocket mmSocket;
114 private final InputStream mmInStream;
115 private final OutputStream mmOutStream;
116
117 public ConnectedThread(BluetoothSocket socket) {
118 mmSocket = socket;
119 InputStream tmpIn = null;
120 OutputStream tmpOut = null;
121
122 // Get the input and output streams, using temp objects because
123 // member streams are final
124 try {
125 tmpIn = socket.getInputStream();
126 tmpOut = socket.getOutputStream();
127 } catch (IOException e) { }
128
129 mmInStream = tmpIn;
130 mmOutStream = tmpOut;
131 }
132
133 public void run() {
134 byte[] buffer = new byte[1024]; // buffer store for the stream
135 int bytes; // bytes returned from read()
136
137 // Keep listening to the InputStream until an exception occurs
138 while (true) {
139 try {
140 // Read from the InputStream
141 bytes = mmInStream.read(buffer);
142 // Send the obtained bytes to the UI activity
143 //mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
144 // .sendToTarget();
145 } catch (IOException e) {
146 break;
147 }
148 }
149 }
150
151 /* Call this from the main activity to send data to the remote device */
152 public void write(byte[] bytes) {
153 try {
154 mmOutStream.write(bytes);
155 } catch (IOException e) { }
156 }
157
158 /* Call this from the main activity to shutdown the connection */
159 public void cancel() {
160 try {
161 mmSocket.close();
162 } catch (IOException e) { }
163 }
164 }

google Developer sei dank.
Nun habe ich auch noch ein passendes GUI geschrieben, dass einen Senden Button ein Textfeld und ein Verbinden Button enthält.
Der Verbinden Button soll jetzt nach möglichkeit die verbinden Methode aufrufen.
Dafür habe ich dem Button mit android:OnClick="Verbidnung" in der Main eine Methode geschrieben (siehe oben).
In der möchte ich nun die Verbinden Methode aufrufen.
Eclipse meckert nicht.
Wenn ich die App aber auf dem HTC starte, sagt das Handy App wurde leider beendet.

Kann jemand helfen?!

Hier noch mal das GUI
1<RelativeLayout xmlns:android="htp://schemas.android.com/apk/res/android"
2 xmlns:tools="htp://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:paddingBottom="@dimen/activity_vertical_margin"
6 android:paddingLeft="@dimen/activity_horizontal_margin"
7 android:paddingRight="@dimen/activity_horizontal_margin"
8 android:paddingTop="@dimen/activity_vertical_margin"
9 tools:context="com.example.blue_final.MainActivity$PlaceholderFragment" >
10
11 <Button
12 android:id="@+id/BtnVerbinden"
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content"
15 android:layout_alignParentLeft="true"
16 android:layout_alignParentRight="true"
17 android:layout_alignParentTop="true"
18 android:onClick="verbinden"
19 android:text="verbinden" />
20
21 <Button
22 android:id="@+id/BtnSenden"
23 android:layout_width="wrap_content"
24 android:layout_height="wrap_content"
25 android:layout_alignLeft="@+id/BtnVerbinden"
26 android:layout_alignParentBottom="true"
27 android:layout_alignRight="@+id/BtnVerbinden"
28 android:layout_marginBottom="17dp"
29 android:text="Senden" />
30
31 <EditText
32 android:id="@+id/TFKonsole"
33 android:layout_width="wrap_content"
34 android:layout_height="wrap_content"
35 android:layout_above="@+id/BtnSenden"
36 android:layout_alignLeft="@+id/BtnVerbinden"
37 android:layout_alignRight="@+id/BtnVerbinden"
38 android:layout_below="@+id/BtnVerbinden"
39 android:ems="10" />
40
41</RelativeLayout>

Im Manifest habe ich auch die entsprechenden Permissions gesetzt!

— geändert am 07.04.2014, 12:05:50

Antworten
Gelöschter Account
  • Forum-Beiträge: 33

07.04.2014, 10:52:08 via App

Was sagt den das LogCat? Welcher Fehler wird ausgegeben? Kannst du das mal posten?

gruss

Antworten
Klaus
  • Blogger
  • Forum-Beiträge: 19.172

07.04.2014, 10:52:10 via App

Hallo

Bitte bearbeite noch mal deinen Threadtitel gemäß unserer Regeln, damit auch klar wird worum es überhaupt geht.
Um den Threadtitel zu ändern musst du im ersten Posting auf "Bearbeiten" klicken.
Danke.!

Klaus
Das AndroidPIT Moderatoren und Administratoren Team

| LG Klaus |
| Google Nexus 6P - Dirty Unicorns | Google Nexus 6 - Dirty Unicorns |
| Das AndroidPITiden-Buch | Die Androiden-Toolbox | AndroidPIT-Regeln |

Antworten
SvenDD
  • Forum-Beiträge: 272

07.04.2014, 10:53:33 via Website

David Schaffner
Was sagt den das LogCat? Welcher Fehler wird ausgegeben? Kannst du das mal posten?

gruss

Und vorher in alle catch Blöcke printStackTrace reinschreiben, sonst sieht man auch keine Fehler, voran das liegt.

Antworten
Timur
  • Forum-Beiträge: 18

07.04.2014, 12:06:45 via Website

Hier die LogCat, er hat anscheinend probleme den Button zu verarbeiten.

104-07 11:57:25.203: D/AndroidRuntime(7352): Shutting down VM
204-07 11:57:25.203: W/dalvikvm(7352): threadid=1: thread exiting with uncaught exception (group=0x40ace300)
304-07 11:57:25.213: V/AudioHardwareMSM72XX(5964): open driver
404-07 11:57:25.213: V/AudioHardwareMSM72XX(5964): get config
504-07 11:57:25.213: V/AudioHardwareMSM72XX(5964): set config
604-07 11:57:25.213: V/AudioHardwareMSM72XX(5964): buffer_size: 4800
704-07 11:57:25.213: V/AudioHardwareMSM72XX(5964): buffer_count: 2
804-07 11:57:25.213: V/AudioHardwareMSM72XX(5964): channel_count: 2
904-07 11:57:25.213: V/AudioHardwareMSM72XX(5964): sample_rate: 44100
1004-07 11:57:25.223: E/AndroidRuntime(7352): FATAL EXCEPTION: main
1104-07 11:57:25.223: E/AndroidRuntime(7352): java.lang.IllegalStateException: Could not execute method of the activity
1204-07 11:57:25.223: E/AndroidRuntime(7352): at android.view.View$1.onClick(View.java:3591)
1304-07 11:57:25.223: E/AndroidRuntime(7352): at android.view.View.performClick(View.java:4084)
1404-07 11:57:25.223: E/AndroidRuntime(7352): at android.view.View$PerformClick.run(View.java:16966)
1504-07 11:57:25.223: E/AndroidRuntime(7352): at android.os.Handler.handleCallback(Handler.java:615)
1604-07 11:57:25.223: E/AndroidRuntime(7352): at android.os.Handler.dispatchMessage(Handler.java:92)
1704-07 11:57:25.223: E/AndroidRuntime(7352): at android.os.Looper.loop(Looper.java:137)
1804-07 11:57:25.223: E/AndroidRuntime(7352): at android.app.ActivityThread.main(ActivityThread.java:4931)
1904-07 11:57:25.223: E/AndroidRuntime(7352): at java.lang.reflect.Method.invokeNative(Native Method)
2004-07 11:57:25.223: E/AndroidRuntime(7352): at java.lang.reflect.Method.invoke(Method.java:511)
2104-07 11:57:25.223: E/AndroidRuntime(7352): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
2204-07 11:57:25.223: E/AndroidRuntime(7352): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
2304-07 11:57:25.223: E/AndroidRuntime(7352): at dalvik.system.NativeStart.main(Native Method)
2404-07 11:57:25.223: E/AndroidRuntime(7352): Caused by: java.lang.reflect.InvocationTargetException
2504-07 11:57:25.223: E/AndroidRuntime(7352): at java.lang.reflect.Method.invokeNative(Native Method)
2604-07 11:57:25.223: E/AndroidRuntime(7352): at java.lang.reflect.Method.invoke(Method.java:511)
2704-07 11:57:25.223: E/AndroidRuntime(7352): at android.view.View$1.onClick(View.java:3586)
2804-07 11:57:25.223: E/AndroidRuntime(7352): ... 11 more
2904-07 11:57:25.223: E/AndroidRuntime(7352): Caused by: java.lang.NullPointerException
3004-07 11:57:25.223: E/AndroidRuntime(7352): at com.example.blue_final.MainActivity.Verbindung(MainActivity.java:47)
3104-07 11:57:25.223: E/AndroidRuntime(7352): at com.example.blue_final.MainActivity.verbinden(MainActivity.java:38)
3204-07 11:57:25.223: E/AndroidRuntime(7352): ... 14 more
3304-07 11:57:25.223: W/ActivityManager(6085): Force finishing activity com.example.blue_final/.MainActivity
3404-07 11:57:25.363: V/AudioHardwareMSM72XX(5964): AudioStreamOutMSM72xx::getParameters() voip_flag=
3504-07 11:57:25.373: V/AudioHardwareMSM72XX(5964): AudioStreamOutMSM72xx::getParameters() voip_flag=
3604-07 11:57:25.383: D/dalvikvm(6085): GC_FOR_ALLOC freed 340K, 27% free 8141K/11015K, paused 115ms, total 118ms
3704-07 11:57:25.383: I/dalvikvm-heap(6085): Grow heap (frag case) to 16.467MB for 281896-byte allocation
3804-07 11:57:25.383: V/AudioHardwareMSM72XX(5964): AudioStreamOutMSM72xx::getParameters() voip_flag=
3904-07 11:57:25.393: V/AudioHardwareMSM72XX(5964): AudioStreamOutMSM72xx::getParameters() voip_flag=
4004-07 11:57:25.413: V/AudioHardwareMSM72XX(5964): AudioStreamOutMSM72xx::getParameters() voip_flag=
4104-07 11:57:25.753: W/ActivityManager(6085): Activity pause timeout for ActivityRecord{40e32a10 com.example.blue_final/.MainActivity}
4204-07 11:57:26.604: I/ActivityManager(6085): No longer want com.google.android.apps.uploader (pid 6887): hidden #16
4304-07 11:57:36.624: W/ActivityManager(6085): Activity destroy timeout for ActivityRecord{40e32a10 com.example.blue_final/.MainActivity}

Antworten
impjor
  • Forum-Beiträge: 1.793

07.04.2014, 13:16:39 via App

Sieh dir doch bitte deinen Code genau an:
Timur
Caused by: java.lang.NullPointerException
04-07 11:57:25.223: E/AndroidRuntime(7352): at com.example.blue_final.MainActivity.Verbindung(MainActivity.java:47)

Liebe Grüße impjor.

Für ein gutes Miteinander: Unsere Regeln
Apps für jeden Einsatzzweck
Stellt eure App vor!

Antworten
Timur
  • Forum-Beiträge: 18

07.04.2014, 18:26:16 via Website

Hast recht. Schreiben, bevor er verbunden ist, kommt nicht so gut.

Naja ich habe das jetzt abgeändert.
Wenn ich mich jetzt verbinden will, stürtzt die App zumindest nicht mehr ab.
Damit ich auch sehen kann, ob ich verbunden bin, habe ich mal eine einfach true/false abfrage gemacht.
Es wird natürlich false zurückgeliefert. Mehr sagt die LogCat auch nicht. Hier nochmal der abgeänderte Code:

1import java.io.IOException;
2import java.io.InputStream;
3import java.io.OutputStream;
4import java.lang.reflect.InvocationTargetException;
5import java.lang.reflect.Method;
6
7import android.app.Activity;
8import android.bluetooth.BluetoothAdapter;
9import android.bluetooth.BluetoothDevice;
10import android.bluetooth.BluetoothSocket;
11import android.content.Intent;
12import android.os.Bundle;
13import android.view.View;
14import android.view.View.OnClickListener;
15import android.widget.Button;
16import android.widget.EditText;
17
18public class MainActivity extends Activity{
19
20 private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
21 private ConnectedThread connected;
22 private boolean isConnected = false;
23
24
25
26 @Override
27 protected void onCreate(Bundle savedInstanceState) {
28 super.onCreate(savedInstanceState);
29 setContentView(R.layout.activity_main);
30 }
31
32
33 //Button ruft die Methode verbindung auf!!!!
34 public void verbinden(View view){
35 verbindung();
36 }
37
38
39 //Verbindung wird ausgeführt
40 private void verbindung(){ //Muss mit Button aufgerufen werden
41 String address = "HIER MUSS DIE NUMMER REIN"; //Adresse vom Dongle
42 BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
43 ConnectThread connect = new ConnectThread(device);
44 connect.start();
45 //connected.write(null);
46 }
47
48
49
50 private class ConnectThread extends Thread {
51 private final BluetoothSocket mmSocket;
52 private final BluetoothDevice mmDevice;
53
54 public ConnectThread(BluetoothDevice device) {
55 // Use a temporary object that is later assigned to mmSocket,
56 // because mmSocket is final
57 BluetoothSocket tmp = null;
58 mmDevice = device;
59
60 // Get a BluetoothSocket to connect with the given BluetoothDevice
61 try {
62 // MY_UUID is the app's UUID string, also used by the server code
63 Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
64 tmp = (BluetoothSocket) m.invoke(device, 1);
65 } catch (NoSuchMethodException e) {
66 // TODO Auto-generated catch block
67 e.printStackTrace();
68 } catch (IllegalAccessException e) {
69 // TODO Auto-generated catch block
70 e.printStackTrace();
71 } catch (IllegalArgumentException e) {
72 // TODO Auto-generated catch block
73 e.printStackTrace();
74 } catch (InvocationTargetException e) {
75 // TODO Auto-generated catch block
76 e.printStackTrace();
77 }
78 mmSocket = tmp;
79 }
80
81 public void run() {
82 // Cancel discovery because it will slow down the connection
83 mBluetoothAdapter.cancelDiscovery();
84
85 try {
86 // Connect the device through the socket. This will block
87 // until it succeeds or throws an exception
88 boolean b = mmSocket.isConnected(); System.out.println(b);
89 mmSocket.connect();
90 isConnected = true;
91 } catch (IOException connectException) {
92 // Unable to connect; close the socket and get out
93 System.out.println("Ich bin eine Exception.");
94 try {
95 mmSocket.close();
96 } catch (IOException closeException) { }
97 return;
98 }
99 connected = new ConnectedThread(mmSocket);
100 connected.run();
101 }

1<RelativeLayout xmlns:android="htp://schemas.android.com/apk/res/android"
2 xmlns:tools="ttp://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:paddingBottom="@dimen/activity_vertical_margin"
6 android:paddingLeft="@dimen/activity_horizontal_margin"
7 android:paddingRight="@dimen/activity_horizontal_margin"
8 android:paddingTop="@dimen/activity_vertical_margin"
9 tools:context="com.example.blue_final.MainActivity$PlaceholderFragment" >
10
11 <Button
12 android:id="@+id/BtnVerbinden"
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content"
15 android:layout_alignParentLeft="true"
16 android:layout_alignParentRight="true"
17 android:layout_alignParentTop="true"
18 android:onClick="verbinden"
19 android:text="verbinden" />
20
21 <Button
22 android:id="@+id/BtnSenden"
23 android:layout_width="wrap_content"
24 android:layout_height="wrap_content"
25 android:layout_alignLeft="@+id/BtnVerbinden"
26 android:layout_alignParentBottom="true"
27 android:layout_alignRight="@+id/BtnVerbinden"
28 android:layout_marginBottom="17dp"
29 android:onClick="senden"
30 android:text="Senden" />
31
32 <EditText
33 android:id="@+id/TFKonsole"
34 android:layout_width="wrap_content"
35 android:layout_height="wrap_content"
36 android:layout_above="@+id/BtnSenden"
37 android:layout_alignLeft="@+id/BtnVerbinden"
38 android:layout_alignRight="@+id/BtnVerbinden"
39 android:layout_below="@+id/BtnVerbinden"
40 android:ems="10" />
41
42</RelativeLayout>

— geändert am 07.04.2014, 18:31:35

Antworten
Timur
  • Forum-Beiträge: 18

07.04.2014, 18:32:06 via Website

Hier nochmal der Rest aus der Main Datei. Den wollte er vorhin nicht haben


1/** Will cancel an in-progress connection, and close the socket */
2 public void cancel() {
3 try {
4 mmSocket.close();
5 } catch (IOException e) { }
6 }
7 }
8
9 private class ConnectedThread extends Thread {
10 private final BluetoothSocket mmSocket;
11 private final InputStream mmInStream;
12 private final OutputStream mmOutStream;
13
14 public ConnectedThread(BluetoothSocket socket) {
15 mmSocket = socket;
16 InputStream tmpIn = null;
17 OutputStream tmpOut = null;
18
19 // Get the input and output streams, using temp objects because
20 // member streams are final
21 try {
22 tmpIn = socket.getInputStream();
23 tmpOut = socket.getOutputStream();
24 } catch (IOException e) { }
25
26 mmInStream = tmpIn;
27 mmOutStream = tmpOut;
28 }
29
30 public void run() {
31 byte[] buffer = new byte[1024]; // buffer store for the stream
32 int bytes; // bytes returned from read()
33
34 // Keep listening to the InputStream until an exception occurs
35 while (true) {
36 try {
37 // Read from the InputStream
38 bytes = mmInStream.read(buffer);
39 // Send the obtained bytes to the UI activity
40 //mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
41 // .sendToTarget();
42 } catch (IOException e) {
43 break;
44 }
45 }
46 }
47
48 /* Call this from the main activity to send data to the remote device */
49 public void write(byte[] bytes) {
50 try {
51 mmOutStream.write(bytes);
52 connected.write(null);
53 } catch (IOException e) {}
54 }
55
56 /* Call this from the main activity to shutdown the connection */
57 public void cancel() {
58 try {
59 mmSocket.close();
60 } catch (IOException e) { }
61 }
62 }
63}

Antworten