C
C
Cppper2018-01-09 21:39:56
Java
Cppper, 2018-01-09 21:39:56

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

2 answer(s)
A
Alex Marken, 2018-01-10
@Cpper

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);

1:
In order to open a file in Write mode, the corresponding permission in the Manifest is required. This is about doing outputStream = openFileOutput(fileName, 0) .
Next... Since you don't have permission to write data, outputStream will remain NULL . In this case, when creating a data writer stream " new OutputStreamWriter(outputStream) ", the application will throw an error ( outputStream is NULL ).
2:
Make sure the file you are opening exists ("macadress.txt"). Otherwise, it must be created (requires WRITE rights).
3:
Another point, the path to the file is incorrectly specified. If the file is in the application folder, the path is specified as follows: "//data//data//PACKAGE NAME//macadress.txt".

C
Cpper, 2018-01-10
@Cpper

Thank you all! The error was solved, the application shows all the names and poppy addresses.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question