如何从Android Wear向Android手机发送数据(String)

时间:2018-10-20 09:12:17

标签: android android-studio android-wear-data-api

我目前在从我的Android设备向我的Android手机发送数据(字符串)时遇到问题。我已经为此做了一些代码。所以我将我的应用程序放在我的android手机上,它将从我也做过的android服装中接收来自该应用程序的数据。

我的Android手机应用程序可以接收android Wear应用程序发送的数据,但是它非常缓慢且延迟,仅5秒后,两个应用程序便崩溃了。请看看我的代码。我想我错过了一些东西,但我找不到。

我发送的手势是左,右,下,DownFast,已释放,向左旋转和向右旋转

我只是android开发的新手。请帮助我。

这是android wear应用的代码:

@Override
   public void onSensorChanged(SensorEvent event) {

    float[] rotMatrix = new float[9];
    float[] rotVals = new float[3];

    SensorManager.getRotationMatrixFromVector(rotMatrix, event.values);
    SensorManager.remapCoordinateSystem(rotMatrix,
            SensorManager.AXIS_X, SensorManager.AXIS_Y, rotMatrix);

    SensorManager.getOrientation(rotMatrix, rotVals);
    float azimuth = (float) Math.toDegrees(rotVals[0]);
    float pitch = (float) Math.toDegrees(rotVals[1]);
    float roll = (float) Math.toDegrees(rotVals[2]);

    if (azimuth < -15)
        {
            mData.setText("Left");

            sendMessage("Left");
        }

        else if (azimuth > 15)
            {
                mData.setText("Right");

                sendMessage("Right");
            }

        else if (roll > 20 && roll < 45)
            {

                mData.setText("Down");

                sendMessage("Down");
            }

        else if (roll > 45)
            {

                mData.setText("DownFast");

                sendMessage("DownFast");
            }

        else if (pitch > 40)
            {

                mData.setText("RotateLeft");

                sendMessage("RotateLeft");
            }

        else if (pitch <-40)
            {

                mData.setText("RotateRight");

                sendMessage("RotateRight");
            }

        else if (azimuth > -15 && azimuth < 15 && roll <20 && pitch > -40 && pitch < 40 )
            {

                mData.setText("Released");

                sendMessage("Released");
            }


}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {

}

private GoogleApiClient getGoogleApiClient(Context context) {
    return new GoogleApiClient.Builder(context)
            .addApi(Wearable.API)
            .build();
}

private void retrieveDeviceNode() {
    final GoogleApiClient client = getGoogleApiClient(this);
    new Thread(new Runnable() {
        @Override
        public void run() {
            client.blockingConnect(30000, TimeUnit.MILLISECONDS);
            NodeApi.GetConnectedNodesResult result =
                    Wearable.NodeApi.getConnectedNodes(client).await();
            List<Node> nodes = result.getNodes();
            if (nodes.size() > 0) {
                nodeId = nodes.get(0).getId();
            }
            client.disconnect();
        }
    }).start();
}

private void sendMessage(final String message) {
    final GoogleApiClient client = getGoogleApiClient(this);
    if (nodeId != null && nodeId != "") {
        new Thread(new Runnable() {
            @Override
            public void run() {
                client.blockingConnect(30000, TimeUnit.MILLISECONDS);
                Wearable.MessageApi.sendMessage(client, nodeId, "/message", message.getBytes());
                client.disconnect();
            }
        }).start();
    }
}

0 个答案:

没有答案