Android APP接收UDP数据出错
/**
 * 接收数据线程
 */
@Override
public void run() {
    // TODO Auto-generated method stub
    try {
    while(!isStart);
        DatagramPacket dPacket = new DatagramPacket(reBuf, LEN);
        //InetAddress LocalAddr = InetAddress.getLocalHost();
        //Log.e(TAG,"IP = "+LocalAddr);
        /*dSocket = new DatagramSocket(null);
        dSocket.setReuseAddress(true);
        dSocket.bind(new InetSocketAddress(LISTENPORT));*/

        Log.e(TAG, "server run");
    while(isReceiveRun){
        /**
         * udp 监听,当接收到数据则对数据进入处理后发送消息给UI线程,进行更新UI
         */
        Log.e(TAG,"receive");
        listenSocket.receive(dPacket);
        Log.e(TAG,"port = "+dPacket.getPort()+"");
        if(dPacket.getPort() == SOURPORT)
            processMessage(dPacket.getData());
        //for(int i=0;i<dPacket.getData().length;i++)
        //Log.e(TAG, dPacket.getData()[i]+"");
    }

    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }
    Log.e(TAG,"dSocket close");
}
/**
 * 对接收到的数据进行处理和分发
 * 数据处理后根据数据帧类型分别分发到          灯控查询数据:LightMap<Byte,boolean>
 *                           环境数据:EnvironmentList<String>
 *                           抄表数据:WatermeterList<String>
 * @param data
 */
private void processMessage(final byte[] data) {
    // TODO Auto-generated method stub
    Log.e(TAG,"data");
    byte C = data[0];
    int L = data[1]*256+data[2];
    int DI = 0;
    DI |= ((int)data[3]&0xff)<<8;
    DI |= (int)data[4];
    int SER = 0;
    SER |= (int)(data[5]&0xff)<<8;
    SER |= (int)data[6];
    byte[] DATA = new byte[L-4];
    for(int i=0; i<(L-4); i++)
        DATA[i] = data[i+7];
    byte CS = data[L+3];
    Log.e(TAG,"CS = "+((CS>>4)&0xf)+(CS&0xf)+" L = "+L+" DI = "+DI);

    if(CS != Check(data,L+4)){
        Log.e(TAG,"CS erro");
        return;
    }
    if(((byte)C != (byte)0xBB)&&((byte)C != (byte)0x3B)){
        if(((byte)C == (byte)0xFB)||((byte)C == (byte)0xE3)){//错误应答
            switch(DI){
            case CONTROL_DI:
                dataType = CONTROL_TYPE;

                break;
            case SEARCH_ENVIRONMENT_DI:
                break;
            case SEARCH_LIGHT_DI:
                break;
            case SEARCH_SCENE_DI:
                break;
            case SEARCH_WATERMETER_DI:
                break;
            default:
                Log.e(TAG,"error DI bit");
                return;
            }
            //return;
        }else{//此数据不符合协议
            Log.e(TAG,"error protocol");
            return;
        }
    }
    //处理正确应答数据
    switch(DI){
    case CONTROL_DI:
        dataType = CONTROL_TYPE;
        break;
    case SEARCH_ENVIRONMENT_DI:
        dataType = SEARCH_ENVIRONMENT_TYPE;

        //enable = 1;
        break;
    case SEARCH_LIGHT_DI:
        dataType = SEARCH_LIGHT_TYPE;
        int len = DATA.length;
        int nums = len/2;
        for(int i=0; i<nums; i++)
            LightMap.put(DATA[i*2], (DATA[i*2+1]==0)?false:true);
        break;
    case SEARCH_SCENE_DI:
        dataType = SEARCH_SCENE_TYPE;

        break;
    case SEARCH_WATERMETER_DI:
        dataType = SEARCH_WATERMETER_TYPE;

        break;
    default:
        Log.e(TAG,"error DI bit");
        return;
    }

对接受到的数据进行首字节判断,判断正确更改按键当前状态并改变发送UDP数据 ,判断失败按键状态不变发送UDP数据不变

目前打印不出接收到的数据,不知道哪里出问题了,有人能帮我看看吗?我是android菜鸟

1个回答

检查下网络通讯防火墙,调试你的程序。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问