Отправка простой строки через Bluetooth - PullRequest
0 голосов
/ 06 сентября 2018

Я пытаюсь отправить простую строку через Bluetooth из моего приложения для Android на сервер Bluetooth, работающий на другом телефоне.

Приложение может подключиться с помощью init () , но не может отправлять строки с помощью функции write () . У меня есть одна кнопка для подключения и другая кнопка для отправки в событии onclick . Код ниже:

private OutputStream outputStream;
private InputStream inStream;

private void init() throws IOException {
    BluetoothAdapter blueAdapter = BluetoothAdapter.getDefaultAdapter();
    if (blueAdapter != null) {
        if (blueAdapter.isEnabled()) {
            Set<BluetoothDevice> bondedDevices = blueAdapter.getBondedDevices();

            if(bondedDevices.size() > 0) {
                Object[] devices = (Object []) bondedDevices.toArray();
                BluetoothDevice device = (BluetoothDevice) devices[position];
                ParcelUuid[] uuids = device.getUuids();
                BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuids[0].getUuid());
                socket.connect();
                outputStream = socket.getOutputStream();
                inStream = socket.getInputStream();
            }

            Log.e("error", "No appropriate paired devices.");
        } else {
            Log.e("error", "Bluetooth is disabled.");
        }
    }
}

public void write(String s) throws IOException {
    outputStream.write(s.getBytes());
}


public void onClick(View v) {

    if (v.getId() == R.id.gumbBT) {
        // button1 action
        try {
            init();
        } catch (IOException e) {
            //textView1.setText("catch aktiviran");
            textView1.setText(e.toString());
            //e.printStackTrace();
        }
    } else if (v.getId() == R.id.send) {
        write("bolje");


    }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...