4

我正在用NXT和Android APP做一些事情,但这并不容易。

我成功连接NXJ和Android设备,但是数据没有发送。

我正在尝试将数据从 Android 发送到 NXT。

首先,我得到了 Android App 的代码:

public void writeMessage(byte msg, String nxt) throws InterruptedException{ 
    BluetoothSocket connSock;

    //Swith nxt socket 
    if(nxt.equals("nxt1")){
        connSock=socket_nxt1;
    }else{
        connSock=null; 
        Log.d("write","Err");
    } 
    if(connSock!=null){
        try { 
            OutputStreamWriter out = new OutputStreamWriter(connSock.getOutputStream());
            out.write(msg);
            out.flush();
            Thread.sleep(1000);
        } catch (Exception e) { 
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }else{
        //Error
        Log.d("write","Err");
    } 
}  

这是针对 NXT 的:

while(true){
    LCD.drawString(waiting,0,0);
    LCD.refresh();

    BTConnection btc = Bluetooth.waitForConnection();

    LCD.clear();
    LCD.drawString(connected,0,0);
    LCD.refresh();  

    DataInputStream dis = btc.openDataInputStream();
    DataOutputStream dos = btc.openDataOutputStream();

    for(int i=0;i<1;i++) {
        byte n = dis.readByte();

        LCD.drawInt(n,0,1);
        LCD.refresh();

        dos.writeInt(-n);
        dos.flush();
    }

    dis.close();
    dos.close();

    Thread.sleep(1000); // wait for data to drain

    LCD.clear();
    LCD.drawString(closing,0,0);
    LCD.refresh();

    btc.close();
    LCD.clear();
}
4

0 回答 0