2

I'm creating an application to send accelerometer and gyroscope data from a Gear Live to a laptop. The two devices are on the same Wi-Fi network. I implemented a socket listener on the wearable, and a server on the laptop. The socket listener is running in a Thread.

Here is the socket listener code:

public class SocketListenThread implements Runnable
{

    @Override
    public void run()
    {
        try
        {
            Log.i(TAG, "trying to bind to the host");
            socket = new Socket( "192.168.43.184", dstPort );
            Log.i(TAG, "Connected..");
            outputStream = socket.getOutputStream();
            while( runThread )
            {
                if( dataObj == null )
                {
                    synchronized ( lock )
                    {
                        lock.wait();
                    }
                }
                synchronized ( lock )
                {

                    ObjectOutputStream os = new ObjectOutputStream( outputStream );
                    os.writeObject(1);
                    os.flush();
                    ConnectionUtil.close(os);

                    lock.notifyAll();
                }
                Thread.sleep( 1 );
            }
        }
        catch( InterruptedException e )
        {
            Log.e( SocketListenThread.class.getSimpleName() + "InterruptedException", e.toString() );
        }
        catch( Exception e )
        {
            Log.e( SocketListenThread.class.getSimpleName() + "Exception", e.toString() );
            e.printStackTrace();
        }
        finally
        {
            ConnectionUtil.close( outputStream );
            ConnectionUtil.close( socket );
            connected = false;
        }

    }
}

Here is the sensor data extracting method and notifying the socket to send the data:

@Override
public void onSensorChanged(SensorEvent event)
{
    List<Float> valList = new ArrayList<>();
    switch( event.sensor.getType() )
    {
        case Sensor.TYPE_HEART_RATE:
            valList.add(event.values[0]);
            sendData(new DataObj(TYPE_HEART_RATE,valList));
            break;
        case Sensor.TYPE_ACCELEROMETER:
            valList.add(event.values[0]);
            valList.add(event.values[1]);
            valList.add(event.values[2]);
            sendData(new DataObj(TYPE_ACCELEROMETER,valList));
            break;
        case Sensor.TYPE_GYROSCOPE:
            valList.add(event.values[0]);
            valList.add(event.values[1]);
            valList.add(event.values[2]);
            sendData(new DataObj(TYPE_GYROSCOPE,valList));
            break;
        default:
            break;
    }
}

public void sendData( DataObj dataObj )
{
    synchronized( lock )
    {
        if( dataObj != null)
        {
            this.dataObj = dataObj;
        }
        lock.notifyAll();
    }
}

The problem is, when I'm running this code, I don't get the sensor data. That is, I'm not getting any sensor data change values to the onSensorChanged(SensorEvent event) callback, although this code is working properly in the phone. If I remove the socket thread from the code, I receive the data.

So my questions are:

  • Is there a problem with this code
  • If so, what is the most optimal way of sending data from wear to a laptop (I need to see data in real time, with minimal data loss).
TofferJ
  • 4,678
  • 1
  • 37
  • 49
KTB
  • 1,499
  • 6
  • 27
  • 43
  • Might seem like a silly question, but are you calling .start() on your sensor listener thread? It's easy to forget. – TofferJ Sep 27 '18 at 20:53
  • @Toffer, I just put here the listener code, not the whole code including the listener start code. If you read the question correctly, it says "although this code is working properly in the phone. If I remove the socket thread from the code, I receive the data.". So yes, I always call the method .start() when running a thread. The doubt I'm having is, whether this locking mechanism is too much processor intensive for android wear. – KTB Sep 28 '18 at 03:29

0 Answers0