Send sensor data from Wear OS to laptop in realtime

360 Views Asked by At

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).
0

There are 0 best solutions below