如何传感器数据直接从Android Wear通过蓝牙传送到PC [英] How to send sensor data directly to PC from Android Wear using bluetooth

查看:719
本文介绍了如何传感器数据直接从Android Wear通过蓝牙传送到PC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让说,我有我的电脑上蓝牙适配器。结果
我怎样才能将数据直接发送到我的电脑。结果
我不想用手机中间人。结果
(通过蓝牙调试穿的时候,它通常使用的)结果
我可以对Android Wear与PC直接,然后传输任何数据?

Let say I have bluetooth dongle on my PC.
How can I send data to my PC directly.
I don't want to use the phone a middle man.
(which is normally used when debugging Wear over bluetooth)
Can I pair Android Wear with PC directly, and then transmit any data?

推荐答案

我已经尝试过了,并证实,它适用于Android 5.1磨损(包括版本1.1和最新的1.3版本)。结果
证明: https://www.youtube.com/watch?v=yyGD8uSjXsI

我不知道,因为它似乎像一些漏洞,谷歌将很快修补这个。

I have tried it and confirmed it works on Android Wear 5.1 (both version 1.1 and latest version 1.3).
Proof: https://www.youtube.com/watch?v=yyGD8uSjXsI
I am not sure if Google will patch this soon because it seems like some loophole.

您需要具备WiFi如三星齿轮直播,Moto360,LG等文雅为了您的信息的Andr​​oid手表LG G手表没有WiFi的支持。
那么首先您需要将您的手表配对到您的手机,然后去你的手表,让WiFi和选择任何WiFi点。然后手表会问你在手机上插入您的密码。在此之后,它会连接到WiFi网络。

You need a android watch with WiFi such as Samsung Gear Live, Moto360, LG Urbane etc. For your information, LG G Watch has no WiFi support. Then first you need to pair your watch to your phone, then go to your watch, enable WiFi and select any WiFi spot. Then the watch will ask you to insert your password on the phone. After that, it will connect to the WiFi.

然后现在你应该去你的Andr​​oid手机,并解除配对的手表。这是因为我们想迫使手表直接使用WiFi连接。否则将始终通过手机作为中间人使用蓝牙。结果
您可以通过加倍运行Android Wear浏览器检​​查手表使用WiFi和浏览任何网页随意。

Then now you should go to your android phone, and UNPAIR the watch. This is because we want to force the watch to use WiFi connection directly. Or else it will always use bluetooth via the phone as middleman.
You can double check the watch is using WiFi by running an Android Wear browser and browse any random pages.

然后在你的code(服务器/客户端模式),只需使用插座正常。结果
例如,我用aysnctask了如下的网络连接。你应该寄存器听者获得传感器数据,有很多例子,所以我不张贴在这里。

Then in your code (server/client model), just use socket normally.
For example, I use aysnctask for network connection as below. You should get sensor data by register listener, there are many examples so I don't post here.

class ConnectServer  extends AsyncTask<Void, Void, Void> {

    public ConnectServer() {
        super();
    }

    @Override
    protected Void doInBackground(Void... params) {
        sendToServer();
        return null;
    }

    public void sendToServer(){
        Socket socket = null;
        BufferedWriter out = null;
        try
        {
            socket = new Socket(IP_ADDRESS, 5000); // IP address of your computer
            out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            while(isSending)
            {
                //x,y,z,w is from android sensor 
                out.write(String.format("%.3f",x)+
                    ","+String.format("%.3f",y)+
                    ","+String.format("%.3f",z)+
                    ","+String.format("%.3f",w)+"/");
                out.flush();
                Thread.sleep(INTERVAL); // 50ms is quite good
            }

            out.close();
            socket.close();
        }
        catch (Exception e)
        {
            Log.e("socket",""+e.toString());
            e.printStackTrace();
        }
        finally
        {
            if (socket != null)
            {
                try
                {
                    socket.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            if (out != null)
            {
                try
                {
                    out.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
}

有关服务器,我使用的内部团结.NET异步插座,但它应该是任何.NET应用程序是相同的。在code未优化或错误彻底检查,但你的想法。

For Server, I am using .net Async Socket inside Unity, but it should be the same for any .net applications. The code is not optimized or error checked thoroughly, but you get the idea.

using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Text;

public class AsyncServer : MonoBehaviour {
    private byte[] data = new byte[1024];
    private int size = 1024;
    private Socket server;
    private Socket client;
    float x, y, z, w;

    void Start () {
        server = new Socket(AddressFamily.InterNetwork,
                  SocketType.Stream, ProtocolType.Tcp);

        IPAddress ipAd = IPAddress.Parse("YOUR IP ADDRESS");
        //IPEndPoint iep = new IPEndPoint(IPAddress.Any, 5000);
        IPEndPoint iep = new IPEndPoint(ipAd, 5000);
        server.Bind(iep);
        server.Listen(5);
        server.BeginAccept(new AsyncCallback(AcceptConn), server);

    }

    void AcceptConn(IAsyncResult iar)
    {
        Socket oldserver = (Socket)iar.AsyncState;
         client = oldserver.EndAccept(iar);
        Debug.Log("Accepted client: " + client.ToString());
        client.BeginReceive(data, 0, size, SocketFlags.None,
            new AsyncCallback(ReceiveData), client);
    }

    void SendData(IAsyncResult iar)
    {
        Socket client = (Socket)iar.AsyncState;
        int sent = client.EndSend(iar);
        client.BeginReceive(data, 0, size, SocketFlags.None,
                    new AsyncCallback(ReceiveData), client);
    }

    void ReceiveData(IAsyncResult iar)
    {
        Socket client = (Socket)iar.AsyncState;
        int recv = client.EndReceive(iar);
        if (recv == 0)
        {
            client.Close();
            Debug.LogError("Waiting for client...");
            server.BeginAccept(new AsyncCallback(AcceptConn), server);
            return;
        }
        string receivedData = Encoding.ASCII.GetString(data, 0, recv);
        //Debug.Log("len: " + recv + ", " + receivedData);

        {
            var s = receivedData.Split(',');
            if (s.Length == 4)
            {
                float xx = float.Parse(s[0]);
                float yy = float.Parse(s[1]);
                float zz = float.Parse(s[2]);
                s[3] = s[3].Replace("/","");
                float ww = float.Parse(s[3]);
                Debug.Log("len: " + recv + ", " + xx + ", " + yy + ", " + zz + ", " + ww);
            } 
        }

        client.BeginReceive(data, 0, size, SocketFlags.None,
            new AsyncCallback(ReceiveData), client);
    }
}

这篇关于如何传感器数据直接从Android Wear通过蓝牙传送到PC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆