Android Bluetooth Control

I am currently trying to connect the HC-05 Bluetooth module with Android so that it sends bytes through the module to the Android device. I robbed some code almost directly from the Android developer site: http://developer.android.com/guide/topics/connectivity/bluetooth.html#ManagingAConnection

Here is the main one. The problem I am facing is that mhandler does not receive the MESSAGE_READ case, which means that I am not receiving data from my module. I was wondering what I need to do to send data to run this case MESSAGE_READ? While the program connects the device and sends "successfully connected" to my arduino.

It also asked a previously asked question by someone who probably articulated it better than I did not answer, so I think I'm not the only one.

https://stackoverflow.com/questions/20088856/no-data-buffered-from-bluetooth-module

The difference that I see in our code is basically that it started () its connectedThread (). Thanks for the help!

public class Main_Activity extends Activity implements OnItemClickListener {

ArrayAdapter<String> listAdapter;

ListView listView;
BluetoothAdapter btAdapter;
Set<BluetoothDevice> devicesArray;
ArrayList<String> pairedDevices;
ArrayList<BluetoothDevice> devices;
public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
protected static final int SUCCESS_CONNECT = 0;
protected static final int MESSAGE_READ = 1;
IntentFilter filter;
BroadcastReceiver receiver;
String tag = "debugging";
Handler mHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        Log.i(tag, "in handler");
        super.handleMessage(msg);
        switch(msg.what){
        case SUCCESS_CONNECT:
            // DO something
            ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
            //Toast.makeText(getApplicationContext(), "CONNECT", 0).show();
            String s = "successfully connected";
            connectedThread.write(s.getBytes());                
            Log.i(tag, "connected");

            break;
        case MESSAGE_READ:
            byte[] readBuf = (byte[])msg.obj;
            String string = new String(readBuf);
            Toast.makeText(getApplicationContext(), string, 0).show();              
            break;  

        }
    }

};

Here is the code the handler should send:

private class ConnectThread extends Thread {

        private final BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;

        public ConnectThread(BluetoothDevice device) {
            // Use a temporary object that is later assigned to mmSocket,
            // because mmSocket is final
            BluetoothSocket tmp = null;
            mmDevice = device;
            Log.i(tag, "construct");
            // Get a BluetoothSocket to connect with the given BluetoothDevice
            try {
                // MY_UUID is the app UUID string, also used by the server code
                tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
            } catch (IOException e) { 
                Log.i(tag, "get socket failed");

            }
            mmSocket = tmp;
        }

        public void run() {
            // Cancel discovery because it will slow down the connection
            btAdapter.cancelDiscovery();
            Log.i(tag, "connect - run");
            try {
                // Connect the device through the socket. This will block
                // until it succeeds or throws an exception
                mmSocket.connect();
                Log.i(tag, "connect - succeeded");
            } catch (IOException connectException) {    Log.i(tag, "connect failed");
                // Unable to connect; close the socket and get out
                try {
                    mmSocket.close();
                } catch (IOException closeException) { }
                return;
            }

            // Do work to manage the connection (in a separate thread)

            mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
        }



        /** Will cancel an in-progress connection, and close the socket */
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) { }
        }
    }

    private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;

        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;  // 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
                    buffer = new byte[1024];
                    bytes = mmInStream.read(buffer);
                    // Send the obtained bytes to the UI activity
                    mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();

                } catch (IOException e) {
                    break;
                }
            }
        }
+3
source share
3 answers

Your use of threads is simply incorrect.

Creating a thread object does not start a physical thread!

Therefore, placing, as you did, the potentially slow function of the socket-socket into the constructor of the stream object is again erroneous.

, () - , connect() !

- :

class ConnectThread extends Thread {

     public ConnectThread(BluetoothDevice device) {
            // nothing long running here!
     }

     public void run() {
         device.createRfcommSocketToServiceRecord(MY_UUID);

         // followed by connect() etc.
     }
}

,

  connectedThread.start();

.

+1

, . , , mHandler.sendToTarget. , mHandler.dispatchMessage( - - , ). .

0

, " " arduino? . bluetoothchat, android sdk.

:

1) , .

2) Definition of constants SUCCESS_CONNECT and MESSAGE_READ. They are private, but available from a different class. In the bluetoothchat source, they are defined as public.

In addition, referring to these constants from another class, we need to pass the class in which they are defined, i.e. refer to them as shown below:

Main_Activity.SUCCESS_CONNECT 
Main_Activity.MESSAGE_READ. 
0
source

All Articles