- Forum-Beiträge: 18
07.04.2014, 10:48:02 via Website
07.04.2014 10:48:02 via Website
ich sitze momentan daran, Daten über Bluetooth zu Senden/Empfangen.
Nun habe ich diesen Code geschrieben:
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 androidnClick="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
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
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.