(Android)通过蓝牙发送字节到μController

时间:2013-01-09 13:36:04

标签: java android bluetooth

所以,我希望我的Android手机(4.2)通过蓝牙向我的μController发送一个字节数组。为了实现这一点,我在SDK中使用了Google提供的蓝牙聊天示例并对其进行了一些修改。我有两段代码。一个是工作,一个不是。为什么代码1工作而代码2不工作? (澄清一下:使用代码1,我可以将字节[]发送到μController)

按下代码2中的发送按钮时,应用程序崩溃。

当然我可以使用代码1,但我想知道为什么代码2无法避免将来的项目出现此错误。

代码的不同之处如下:

  • 当toggleButton更改其状态时,代码1发送byte []
  • 代码2在按下SendButton时发送字节[]

//代码1

private void setup() {
    Log.d(TAG, "setup()");

    final Context context = getApplicationContext();
    final ToggleButton TBut1 = (ToggleButton)findViewById(R.id.toggleButton1);
    final ToggleButton TBut2 = (ToggleButton)findViewById(R.id.toggleButton2);
    final ToggleButton TBut3 = (ToggleButton)findViewById(R.id.toggleButton3);

    final TextView txt1 = (TextView)findViewById(R.id.textView2);


    final byte[] daten = new byte[1];        

    TBut1.setChecked(false);
    TBut2.setChecked(false);
    TBut3.setChecked(false);
    daten[0]=0;
    TBut1.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            if ((TBut1.isChecked() == true)){                    
                daten[0]= (byte) (daten[0] | 4);
                mService.write(daten);                    
                }else{
                    daten[0]= (byte) (daten[0] & ~4);
                    mService.write(daten);
                }   
        }   
    });

    TBut2.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if ((TBut2.isChecked() == true)){                    
                daten[0]= (byte) (daten[0] | 2);
                mService.write(daten);                    
                }else{
                    daten[0]= (byte) (daten[0] & ~2);
                    mService.write(daten);
                }                   
        }   
    });

    TBut3.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if ((TBut3.isChecked() == true)){                  
                daten[0]= (byte) (daten[0] | 1);
                mService.write(daten);                  
                }else{                  
                    daten[0]= (byte) (daten[0] & ~1);
                    mService.write(daten);
                }                   
        }   
    });

    // Initialize the BluetoothChatService to perform bluetooth connections
    mService = new BTService(this, mHandler);

    // Initialize the buffer for outgoing messages
    mOutStringBuffer = new StringBuffer("");

}

//代码2

 private void setup() {
    Log.d(TAG, "setup()"); 

    final ToggleButton TBut1 = (ToggleButton) findViewById(R.id.toggleButton1);
    final ToggleButton TBut2 = (ToggleButton) findViewById(R.id.toggleButton2);
    final ToggleButton TBut3 = (ToggleButton) findViewById(R.id.toggleButton3);

    final Button sendButton = (Button) findViewById(R.id.button1);

    sendButton.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            daten[0] = 0;
            if(TBut1.isChecked()) {
                daten[0] = (byte) (daten[0] | 1);
            } else {
                daten[0]= (byte) (daten[0] & ~1);
            }
            if(TBut2.isChecked()) {
                daten[0] = (byte) (daten[0] | 2);
            } else {
                daten[0]= (byte) (daten[0] & ~2);
            }
            if(TBut3.isChecked()) {
                daten[0] = (byte) (daten[0] | 4);
            } else {
                daten[0]= (byte) (daten[0] & ~4);
            }


            if (mService.getState() == BTService.STATE_CONNECTED) {
                 mService.write(daten);
            }
        }   
    });

    // Initialize the BluetoothChatService to perform bluetooth connections
    mService = new BTService(this, mHandler);

    // Initialize the buffer for outgoing messages
    mOutStringBuffer = new StringBuffer("");

}

0 个答案:

没有答案