XBee和Android之间的批量转移

时间:2015-01-05 21:59:46

标签: android serial-port xbee zigbee

我现在已经好几天都在挣扎,并且有一种潜在的怀疑,这是显而易见的事情。

我最近收购了一些XBee Xstick ZB(http://www.digi.com/products/wireless-modems-peripherals/wireless-range-extenders-peripherals/xstick)并尝试将它们连接到Android手机进行网状网络连接。在它们设置正确后,在计算机上使用非常简单 - 只需打开终端窗口并读取/写入串行数据到端口。我想我会在Android应用中使用相同的方法。不幸的是,即使我的应用程序显示连接成功,我也没有收到另一方的任何数据(我的计算机使用另一个XBee棒)。我对Android很新,所以请看一下,让我知道我可能缺少什么。

package com.spawar.dale.teminal2;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbRequest;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.nio.ByteBuffer;


public class term extends Activity implements View.OnClickListener, Runnable{

    private static final String TAG = "TerminalActivity";

    private Button send;
    private TextView output;
    private EditText input;
    private UsbManager mUsbManager;
    private UsbDevice mDevice;
    private UsbDeviceConnection mConnection;
    private UsbInterface mInterface;
    private UsbEndpoint mEndpointIn;
    private UsbEndpoint mEndpointOut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_term);
        send = (Button)findViewById(R.id.send);
        output = (TextView) findViewById(R.id.output);
        input = (EditText) findViewById(R.id.input);

        mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
    }

    public void onClick(View view)
    {
        char c;
        int i=0;
        while (input.getText().length() > i) {
            c=input.getText().charAt(i);
            sendMessage(c);
            i++;
        }
        output.append(input.getText());
    }

    @Override
    public void onPause() {
        super.onPause();
    }


    @Override
    public void onResume(){
        super.onResume();
        Intent intent = getIntent();
        Log.d(TAG, "intent: " + intent);
        String action = intent.getAction();

        if (UsbManager.ACTION_USB_DEVICE_ATTACHED!=null) {
            //String list = mUsbManager.getDeviceList().toString();
            //Log.d(TAG,list);
            //output.append(list.toString());
            UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            if(device!=null){
                setDevice(device);
            } else {
                Log.d(TAG,"no device found");
            }
        }


        /*

        UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
        Log.d(TAG,"reached point 1");
        output.append("reached point 1");
        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            setDevice(device);
            output.append("reached point a /n");
        } else if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            if (mDevice != null && mDevice.equals(device)) {
                output.append("reached point b");
                setDevice(null);
            }
        }
        */
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private  void setDevice(UsbDevice device) {
        Log.d(TAG, "setDevice " + device);
        //output.append(" setDevice "+ device);
        output.append("Interface count is: "+device.getInterfaceCount()+"\n");
        if (device.getInterfaceCount() != 1) {
            Log.e(TAG, "could not find interface");
            output.append(" could not find interface");
            return;
        }

        UsbInterface intf = device.getInterface(0);
        output.append("Endpoint count is: "+intf.getEndpointCount()+"\n");
        //Endpoint 0, direction IN
        //Endpoint 1, direction OUT
        UsbEndpoint endIn = intf.getEndpoint(0);
        output.append("Endpoint 1: \n");
        output.append("Direction: " +endIn.getDirection()+ "\n");
        output.append("Type: "+endIn.getType()+ "\n");
        output.append("Endpoint 2: \n");
        UsbEndpoint endOut = intf.getEndpoint(1);
        output.append("Direction: " + endOut.getDirection()+ "\n");
        output.append("Type: " + endOut.getType() + "\n");


       /* if (intf.getEndpointCount() != 1) {
            Log.e(TAG, "could not find endpoint");
            output.append(" could not find endpoint");
            return;
        } */

        //endpoint should be of type interrupt
        //UsbEndpoint ep = intf.getEndpoint(0);

        mDevice = device;
        mInterface = intf;
        mEndpointIn = endIn;
        mEndpointOut = endOut;
        if (device != null) {
            UsbDeviceConnection connection = mUsbManager.openDevice(device);
            if (connection != null && connection.claimInterface(intf, true)) {
                Log.d(TAG, "open success!");
                output.append(" open Success!!");
                mConnection = connection;
                Thread thread = new Thread(this);
                thread.start();
            }
            else {
                Log.d(TAG,"open fail");
                output.append(" open fail");
                mConnection = null;
            }
        }
    }

    private void sendMessage(char messageChar) {
        Log.d(TAG, "sendMessage " + messageChar);
        if (mConnection != null) {
            byte[] message = new byte[5];
            String test = new String("hello");
            message=test.getBytes();
            ByteBuffer buf = ByteBuffer.wrap(message);

            Log.d(TAG, "buffer is "+buf.toString());

            UsbRequest requestOut = new UsbRequest();
            output.append("\nLength: " +message.length);
            Boolean success = mConnection.claimInterface(mInterface, true);
            mConnection.bulkTransfer(mEndpointOut,message,message.length,500);

            requestOut.initialize(mConnection,mEndpointOut);

            //Boolean success = requestOut.queue(buf,5);

            output.append("Mission "+success.toString());

            requestOut.queue(buf,message.length);
            output.append("Request: " + mConnection.requestWait().toString());

            //mConnection.controlTransfer(0x21, 0x9, 0x200, 0, message, message.length, 0);
        }
    }

    @Override
    public void run() {
        ByteBuffer buffer = ByteBuffer.allocate(1);
        UsbRequest requestIn = new UsbRequest();
        requestIn.initialize(mConnection,mEndpointIn);

        byte status = -1;
        while(true) {
            //Queue a request on the interrupt endpoint
            requestIn.queue(buffer, 1);
            //Send poll status command
            //Wait for status event
            if (mConnection.requestWait() == requestIn) {
                byte newStatus = buffer.get(0);
                if (newStatus != status) {
                    Log.d(TAG, "got status "+ newStatus);
                    status = newStatus;
                    output.append(Byte.toString(status));
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
            } else {
                Log.e(TAG, "requestWait failed, exiting");
                break;
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_term, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

我手机屏幕上的当前输出显示:     接口数为:1     端点计数为:2     终点1:     方向:128     类型:2     终点2:     方向:0     类型:2     打开成功!! 81     长度5使命真实....

************************************ EDIT ********** *********

我在这方面取得了一些进展。通过将ByteBuffer缓冲区更改为Byte []缓冲区,然后将整个run()函数内容移动到onResume(),我得到一些数据。出于某种原因,变量的作用就像它们尚未设置,所以程序在run()中突然崩溃。

这是新问题。无论我通过另一方推送什么,唯一读入的数据是8196.例如,在我的计算机上输入' abc',但在设备上它读取' 8196 8196 8196' ;。起初我想也许我正在从错误的USB端点读取,但只有两个,第二个用于输出。有什么想法吗?

0 个答案:

没有答案