Wysyłanie danych przez bluetooth

0

Witam, tworze program, który ma za zadanie połączyć się z modułem btm-222 podłączonym do ATmegi i sterować nią. Program na mikrokontrolerze jest napisany poprawnie, bo gdy przy pomocy programu BlueTerm wysyłam znak to odpowiada poprawnie. Program na telefon piszę w AndroidStudio. Jak na razie umiem się połączyć z modułem jako client, ale nie potrafię nic wysłać. Szukałem w internecie różnych rozwiązań ale nic nie pomaga. Czy mógłby mi ktoś podpowiedzieć jak stworzyć klasę lub funkcję wysyłającą dane (najlepiej w string lub int). Od razu przydałaby się funkcja do odbierania danych. Zaznaczam, że nie jestem biegły w programowaniu w Java. Przesyłam również klasę do łączenia się z modułem:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.UUID;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

public class ClientBluetooth extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ClientBluetooth(BluetoothDevice device) {
        BluetoothSocket tmp = null;
        mmDevice = device;
        try {
            UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
            tmp = device.createRfcommSocketToServiceRecord(uuid);
        } catch (Exception e) { }
        mmSocket = tmp;
    }

    public void run() {
        try {
            Log.d("INFO","proba polaczenia....");
            mmSocket.connect();
            Log.d("INFO","polaczono z serwerem!");
            BufferedReader in = new BufferedReader(new InputStreamReader(mmSocket.getInputStream()));
            String input = in.readLine();
            Log.d("INFO","Serwer mowi: "+input);
        } catch (Exception ce) {
            try {
                mmSocket.close();
            } catch (Exception cle) { }
        }

    }
} 

A to klasa za pomocą której próbowałem wysyłać dane:

public class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;
    //private final BluetoothDevice mmDevice;

        public ConnectedThread(BluetoothSocket socket) {
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the input and output streams, using temp objects because
            // member streams are final
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) { }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public void run() {
            byte[] buffer = new byte[1024];  // buffer store for the stream
            int bytes; // bytes returned from read()

            // Keep listening to the InputStream until an exception occurs
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);
                    // Send the obtained bytes to the UI activity
                    //mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                     //       .sendToTarget();
                } catch (IOException e) {
                    break;
                }
            }
        }

        /* Call this from the main activity to send data to the remote device */
        public void write(byte[] bytes) {
            try {
                mmOutStream.write(bytes);
            } catch (IOException e) { }
        }

        /* Call this from the main activity to shutdown the connection */
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) { }
        }
    } 
0

Klasa która na 100% działa bo jej używam



import android.bluetooth.BluetoothSocket;
import android.util.Log;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by w.maciejewski on 2014-08-20.
 */
 class ConnectedBlue extends Thread {
    private static final String TAG = "BluetoothCommandService";
    public static final int EXIT_CMD = -1;



    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;
 
    public ConnectedBlue(BluetoothSocket socket) {

        mmSocket = socket;

        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the BluetoothSocket input and output streams
        try {
            tmpIn =  mmSocket.getInputStream();
            tmpOut =  mmSocket.getOutputStream();
        } catch (IOException e) {

        }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {

        int readBufferPosition = 0;
        byte [] readBuffer = new byte[1024];


        while (true) {
            try {
                // Read from the InputStream
                int bytesAvailable = mmInStream.available();
                if(bytesAvailable > 0){
                    byte[] packetBytes = new byte[bytesAvailable];
                    int bytes = mmInStream.read();

                    byte[] encodedBytes = new byte[readBufferPosition];
                    System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                    final String data = new String(packetBytes, "US-ASCII");
                    readBufferPosition ++;

   
                }




            } catch (IOException e) {

                Log.e(TAG, "connection lost", e);
                break;
            }
        }
    }


    public void write(byte[] buffer) {
        try {
            mmOutStream.write(buffer);

        } catch (IOException e) {
            Log.e(TAG, "write(byte)  failed", e);
        }
    }

    public void write(int out) {
        try {
            mmOutStream.write(out);


        } catch (IOException e) {
            Log.e(TAG, "write(int)  failed", e);
        }
    }

    public void cancel() {
        try {
            mmOutStream.write(EXIT_CMD);
            mmSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of connect socket failed", e);
        }
    }
}
0

Dziękuję za kod, wszystko fajnie ale nie wysyła danych, dokładnie nie potrafi wykonać:
mmOutStream.write(bytes);
czy ktoś orientuje się co może być przyczyną??

0

a jakiś błąd wyrzuca? czy po prostu się nie wykonuje?

zapinałeś się debuggerem?

a może Twój byte jest pusty?

0

Próbowałem też z intem i to samo. Zapinałem debuggerem no i wyskakuje "write(int) failed". A urządzenia są na pewno połączone ze sobą.

0

poka log

1 użytkowników online, w tym zalogowanych: 0, gości: 1