Hallo,
Ich habe das Problem das meine App die eine Verbindung über Bluetooth zu einem Arduino herstellen soll beim verbinden abstürzt.
Hier der Code:
Ich habe das Problem das meine App die eine Verbindung über Bluetooth zu einem Arduino herstellen soll beim verbinden abstürzt.
Hier der Code:
Java:
public class MainActivity extends AppCompatActivity {
TextView textfeld;
Button verbinden;
Button trennen;
EditText macadresse;
TextView LOG;
TextView textfeld6;
BluetoothAdapter myBluetooth;
private boolean is_Connected = false;
private BluetoothSocket socket = null;
private UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private OutputStream stream_out = null;
Handler handler;
Runnable runnable;
private Set pairedDevice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// tv_battery = (TextView) findViewById(R.id.tv_batery);
runnable = new Runnable() {
@Override
public void run() {
int level = (int) batteryLevel();
// tv_battery.setText("BATTERY: "+ level + "%");
handler.postDelayed(runnable,0);
}
};
handler = new Handler();
handler.postDelayed(runnable,0);
textfeld6 = (TextView) findViewById(R.id.textView6);
LOG = (TextView) findViewById(R.id.textView5);
textfeld = (TextView) findViewById(R.id.textView4);
textfeld.setBackgroundColor(Color.RED);
final Button trennen = (Button) findViewById(R.id.button4);
macadresse = (EditText) findViewById(R.id.editText2);
//String mac_adresse = ((EditText) findViewById(R.id.editText2)).getText().toString();
final Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
myBluetooth = BluetoothAdapter.getDefaultAdapter();
if(myBluetooth == null){
LOG.setText("Bitte Bluetooth aktivieren");
}else{
LOG.setText("Bluetooth Adapter bereit");
}
// Mac adresse 1 00:21:13:00:c0:49
// Mac adresse 2 00:21:13:00:c3:85
verbindung(v);
}
});
trennen.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(is_Connected && stream_out != null){
is_Connected = false;
try{
socket.close();
stream_out.flush();
}catch(Exception e){
LOG.append("Fehler beim beim trennen "+ e.toString());
}
}else{
LOG.append("Fehler: nichts zum trennen vorhaben");
}
textfeld.setText("Nicht Verbunden");
textfeld.setBackgroundColor(Color.RED);
}
});
}
public void verbindung(View v){
String mac_adresse = ((EditText) findViewById(R.id.editText2)).getText().toString();
LOG.append("Verbinde mit "+mac_adresse);
BluetoothDevice remotedevice = myBluetooth.getRemoteDevice(mac_adresse); //Ab hier schmiert die App ab.ö
try{
wait(2000);
}catch (Exception e){
}
try{
socket = remotedevice.createInsecureRfcommSocketToServiceRecord(uuid);
LOG.append("Socket erstellt");
textfeld.setText("Verbunden");
textfeld.setBackgroundColor(Color.GREEN);
}catch (Exception e){
LOG.append("Socket erstellen fehlgeschlagen" + e.toString());
}
myBluetooth.cancelDiscovery();
try{
socket.connect();
LOG.append("Socket verbunden");
is_Connected = true;
}catch(Exception e){
LOG.append("Socket erstellung fehlgeschlagen" + e.toString());
is_Connected = false;
}
if(!is_Connected){
try{
socket.close();
}catch (Exception e){
LOG.append("Socket kann nicht beendet werden "+ e.toString());
}
}
try{
stream_out = socket.getOutputStream();
LOG.append("Outputstream erstellt");
}catch(Exception e){
LOG.append("Outputstram erstellen fehler "+ e.toString());
is_Connected = false;
}
}
public void sendung(){
int message = 0;
//byte[] msgbuffer = message.getBytes();
if(is_Connected){
LOG.append("Sende Nachricht: " + message);
try{
int level = (int) batteryLevel();
while(true){
if(level >95){
message = 0;
//byte[] msgbuffer = message.getBytes();
stream_out.write(message);
}else{
message = 1;
//byte[] msgbuffer = message.getBytes();
stream_out.write(message);
}
}
}catch (Exception e){
LOG.append("Senden fehlgeschlagen "+ e.toString());
}
}
}
public float batteryLevel(){
Intent batteryIntent = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE,-1);
if(level == -1|| scale == 1){
return 50.0f;
}
return ((float) level / (float) scale)* 100.0f;
}
}
Zuletzt bearbeitet von einem Moderator: