Answer the question
In order to leave comments, you need to log in
Why does the app crash with an error?
When calling the macsave function, the application crashes with an error. Help me please. I can't understand why.
PS I am a beginner
package com.example.steshenko.blueterm;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.sql.Struct;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_ENABLE_BT = 1;
public String macadress = "macadress.txt";
public EditText name;
public String mac;
public String named;
private String fileName = "macadress.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
if (bluetooth == null) {
finish();
}
if (!bluetooth.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
InputStream inputStream = null;
try {
inputStream = openFileInput(fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (inputStream != null) {
InputStreamReader isr = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(isr);
String line;
StringBuilder builder = new StringBuilder();
try {
while ((line = reader.readLine()) != null) {
builder.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
name.setText(builder.toString());
}
}
public void macsave (View view) {
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
name = (EditText) findViewById(R.id.name);
named = name.getText().toString();
Set<BluetoothDevice> pairedDevices = bluetooth.getBondedDevices();
if (pairedDevices.size() > 0) {
// There are paired devices. Get the name and address of each paired device.
for (BluetoothDevice device : pairedDevices) {
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
if(deviceName == named){
mac = deviceHardwareAddress;
}
}
}
OutputStream outputStream = null;
try {
outputStream = openFileOutput(fileName, 0);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
OutputStreamWriter osw = new OutputStreamWriter(outputStream);
try {
osw.write(mac);
} catch (IOException e) {
e.printStackTrace();
}
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
name.setText(mac.toString());
}
}
Answer the question
In order to leave comments, you need to log in
Throw off a broad gull (Logcat) errors.
UPD:
Cpper , looked at your code. Unfortunately, I don't have Bluetooth on my Genymotion to check.
These lines in the macsave method alerted me:
OutputStream outputStream = null;
try {
outputStream = openFileOutput(fileName, 0);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
OutputStreamWriter osw = new OutputStreamWriter(outputStream);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question