J
jogimuc
Dauergast
- 313
Hallo hier mal ein einfaches Beispiel zum Senden über die serielle UUID.
wenn du die Mac Adresse des Senders weist kannst die auch direkt eingeben muss also kein gepaartes Gerät sein.
Welche UUID dein Server nutzt weiß ich nicht habe mal die serielle gewählt.
Das Beispiel ist noch ohne Threads.
Manifest Permission nicht vergessen
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
wenn du die Mac Adresse des Senders weist kannst die auch direkt eingeben muss also kein gepaartes Gerät sein.
Welche UUID dein Server nutzt weiß ich nicht habe mal die serielle gewählt.
Das Beispiel ist noch ohne Threads.
Code:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
public class ActivityBlue extends AppCompatActivity implements View.OnClickListener{
// UUID fuer Kommunikation mit Seriellen Modulen
private UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private static final String LOG_TAG = "BLueTest";
private BluetoothAdapter adapter = null;
private BluetoothSocket socket = null;
private OutputStream stream_out = null;
private InputStream stream_in = null;
private boolean is_connected = false;
private Set<BluetoothDevice> pairedDevices;
private ArrayList<BluetoothDevice> pairedListe;
private ListView lv;
private static String mac_adresse; // MAC Adresse des Bluetooth Adapters
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blue);
Log.d(LOG_TAG, "Bluetest: OnCreate");
((Button)findViewById(R.id.bt_on)).setOnClickListener(this);
((Button)findViewById(R.id.bt_trennen)).setOnClickListener(this);
((Button)findViewById(R.id.bt_verbinden)).setOnClickListener(this);
((Button)findViewById(R.id.bt_liste)).setOnClickListener(this);
((Button)findViewById(R.id.bt_senden)).setOnClickListener(this);
((TextView) findViewById(R.id.text_uuid)).setText("UUID: " + uuid);
pairedListe = new ArrayList<>();
// Verbindung mit Bluetooth-Adapter herstellen
adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null || !adapter.isEnabled()) {
Toast.makeText(this, "Bitte Bluetooth aktivieren oder auf On Klicken", Toast.LENGTH_LONG).show();
Log.d(LOG_TAG, "onCreate: Bluetooth Fehler: Deaktiviert oder nicht vorhanden");
} else {
Log.d(LOG_TAG, "onCreate: Bluetooth-Adapter ist bereit");
}
lv = (ListView)findViewById(R.id.paired_listview);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
BluetoothDevice mDevice = pairedListe.get(position);
mac_adresse = mDevice.getAddress();
((EditText) findViewById(R.id.text_adresse)).setText(mac_adresse);
}
});
}
public void bluetoothOn(){
if (!adapter.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
adapter = BluetoothAdapter.getDefaultAdapter();
Toast.makeText(getApplicationContext(), "Bluetooth ist On",Toast.LENGTH_LONG).show();
Log.d(LOG_TAG, "onCreate: Bluetooth-Adapter ist bereit");
} else {
Toast.makeText(this, "BT On", Toast.LENGTH_LONG).show();
Log.d(LOG_TAG,"onCreate: Bluetooth Fehler: Deaktiviert oder nicht vorhanden");
}
}
public void listeBTGeräte(){
pairedDevices = adapter.getBondedDevices();
ArrayList list = new ArrayList();
for(BluetoothDevice bt : pairedDevices) {
list.add(bt.getName());
pairedListe.add(bt);
}
Toast.makeText(this, "List Paired Devices",Toast.LENGTH_SHORT).show();
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
}
public void verbinden() {
mac_adresse = ((EditText) findViewById(R.id.text_adresse)).getText()
.toString();
Log.d(LOG_TAG, "Verbinde mit " + mac_adresse);
BluetoothDevice remote_device = adapter.getRemoteDevice(mac_adresse);
// Socket erstellen
try {
socket = remote_device
.createInsecureRfcommSocketToServiceRecord(uuid);
Log.d(LOG_TAG, "Socket erstellt");
} catch (Exception e) {
Log.e(LOG_TAG, "Socket Erstellung fehlgeschlagen: " + e.toString());
}
adapter.cancelDiscovery();
// Socket verbinden
try {
socket.connect();
Log.d(LOG_TAG, "Socket verbunden");
is_connected = true;
} catch (IOException e) {
is_connected = false;
Log.e(LOG_TAG, "Socket kann nicht verbinden: " + e.toString());
}
// Socket beenden, falls nicht verbunden werden konnte
if (!is_connected) {
try {
socket.close();
} catch (Exception e) {
Log.e(LOG_TAG,
"Socket kann nicht beendet werden: " + e.toString());
}
}
// Outputstream erstellen:
try {
stream_out = socket.getOutputStream();
Log.d(LOG_TAG, "OutputStream erstellt");
} catch (IOException e) {
Log.e(LOG_TAG, "OutputStream Fehler: " + e.toString());
is_connected = false;
}
// Inputstream erstellen
try {
stream_in = socket.getInputStream();
Log.d(LOG_TAG, "InputStream erstellt");
} catch (IOException e) {
Log.e(LOG_TAG, "InputStream Fehler: " + e.toString());
is_connected = false;
}
if (is_connected) {
Toast.makeText(this, "Verbunden mit " + mac_adresse,
Toast.LENGTH_LONG).show();
((Button) findViewById(R.id.bt_verbinden))
.setBackgroundColor(Color.GREEN);
} else {
Toast.makeText(this, "Verbindungsfehler mit " + mac_adresse,
Toast.LENGTH_LONG).show();
((Button) findViewById(R.id.bt_verbinden))
.setBackgroundColor(Color.RED);
}
}
public void senden() {
String message = ((EditText) findViewById(R.id.text_eingabe)).getText()
.toString();
byte[] msgBuffer = message.getBytes();
if (is_connected) {
Log.d(LOG_TAG, "Sende Nachricht: " + message);
try {
stream_out.write(msgBuffer);
} catch (IOException e) {
Log.e(LOG_TAG,
"Bluetest: Exception beim Senden: " + e.toString());
}
}
}
public void trennen() {
if (is_connected && stream_out != null) {
is_connected = false;
((Button) findViewById(R.id.bt_verbinden))
.setBackgroundColor(Color.RED);
Log.d(LOG_TAG, "Trennen: Beende Verbindung");
try {
stream_out.flush();
socket.close();
} catch (IOException e) {
Log.e(LOG_TAG,
"Fehler beim beenden des Streams und schliessen des Sockets: "
+ e.toString());
}
} else
Log.d(LOG_TAG, "Trennen: Keine Verbindung zum beenden");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(LOG_TAG, "onDestroy. Trenne Verbindung, falls vorhanden");
trennen();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_on:
bluetoothOn();
break;
case R.id.bt_verbinden:
verbinden();
break;
case R.id.bt_liste:
listeBTGeräte();
break;
case R.id.bt_trennen:
trennen();
break;
case R.id.bt_senden:
senden();
break;
}
}
}
Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linearLayout">
<TextView
android:id="@+id/text_uuid"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="UUID"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="MAC Adresse des Bluetooth Devices:"
android:id="@+id/textView" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/text_uuid"
app:layout_constraintEnd_toEndOf="parent"/>
<EditText
android:id="@+id/text_adresse"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:text="00:11:12:14:00:35"
app:layout_constraintTop_toTopOf="@+id/textView" app:layout_constraintStart_toStartOf="parent" android:layout_marginTop="18dp"
app:layout_constraintEnd_toEndOf="parent">
</EditText>
<Button
android:id="@+id/bt_verbinden"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Verbinden"
app:layout_constraintStart_toStartOf="@+id/text_adresse" app:layout_constraintTop_toBottomOf="@+id/text_adresse"/>
<Button
android:id="@+id/bt_trennen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Trennen"
app:layout_constraintStart_toStartOf="@+id/bt_verbinden" app:layout_constraintTop_toBottomOf="@+id/bt_verbinden"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Eingabe:"
android:id="@+id/textView3" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/bt_verbinden"
app:layout_constraintEnd_toEndOf="parent"/>
<EditText
android:id="@+id/text_eingabe"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:text="Test 123"
app:layout_constraintStart_toStartOf="parent" android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/bt_trennen">
<requestFocus android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</EditText>
<Button
android:id="@+id/bt_senden"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Senden"
app:layout_constraintStart_toStartOf="@+id/text_eingabe" app:layout_constraintTop_toBottomOf="@+id/text_eingabe" android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"/>
<Button
android:text="BT On"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/bt_on" app:layout_constraintStart_toEndOf="@+id/bt_trennen"
android:layout_marginLeft="8dp" android:layout_marginStart="8dp" app:layout_constraintEnd_toStartOf="@+id/bt_liste" android:layout_marginEnd="8dp"
android:layout_marginRight="8dp" app:layout_constraintTop_toBottomOf="@+id/text_adresse"/>
<Button
android:text="Liste"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/bt_liste"
app:layout_constraintTop_toBottomOf="@+id/text_adresse" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp" android:layout_marginRight="16dp"
/>
<ListView
android:layout_width="368dp"
android:layout_height="133dp" app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent"
android:id="@+id/paired_listview" app:layout_constraintHorizontal_bias="0.0"
android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/bt_senden"/>
</android.support.constraint.ConstraintLayout>
Manifest Permission nicht vergessen
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
Zuletzt bearbeitet: