2013-02-28 29 views
0

我被困在這個好幾天了。我想出了一個代碼來發送數據字節到我的藍牙模塊。我沒有收到任何錯誤,但我不確定數據是否實際發送。我用示波器檢查了模塊上的RX和TX引腳,但我什麼也沒有。任何人都知道什麼是錯的?這是我的代碼:閱讀藍牙模塊上的android數據表格

import java.io.IOException; 
import java.io.OutputStream; 
import java.util.Set; 
import java.util.UUID; 
import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothSocket; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class Modes extends Activity { 
    Button gaming; 
    Button graphics; 
    Button sound; 
    Button text; 
    BluetoothDevice mmDevice; 
    BluetoothSocket mmSocket; 
    OutputStream mmOutputStream; 
    BluetoothAdapter mBluetoothAdapter; 
    byte val; 
    private final UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.modes); 
     setUp(); 
     gaming.setOnClickListener(new View.OnClickListener() { 
       @Override 
      public void onClick(View v) { 
       System.out.println("gkghfgh"); 
       val = 0; 
       try{ 
        connect(); 
        senData(); 
       }catch(IOException ex){} 

      } 
     }); 

     graphics.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        val = 1; 
       try{ 
         connect(); 
        senData(); 
       }catch(IOException ex){} 

      } 
     }); 

     sound.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       val = 8; 
       try{ 
        connect(); 
        senData(); 
       }catch(IOException ex){} 
      } 
     }); 

     text.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       val = 9; 
       try{ 
        connect(); 
        senData(); 
       }catch(IOException ex){} 
       startActivity(new Intent(Modes.this,Text.class)); 
      } 
     }); 
    } 

    private void setUp() { 
     gaming = (Button) findViewById(R.id.button1); 
     graphics = (Button) findViewById(R.id.button2); 
     sound = (Button) findViewById(R.id.button3); 
     text = (Button) findViewById(R.id.button4); 
     gaming.setText("Gaming"); 
     graphics.setText("Graphics"); 
     sound.setText("Sound"); 
     text.setText("Text"); 
    } 

    void connect() throws IOException{ 
     mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
     if(pairedDevices.size() > 0) { 
      for(BluetoothDevice device : pairedDevices) { 
       if(device.getName().equals("RN42-58C1")) { 
        mmDevice = device; 
        break; 
       } 
      } 
     } 
     mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);  
     mmSocket.connect(); 

    } 

    void senData() throws IOException{ 
     mmOutputStream.write(val); 
     System.out.println("Data Sent"); 
    } 
} 

回答

0

我認爲問題出在您的藍牙設備名稱。你需要寫一個12位數字的藍牙ID。具有字母數字值。

希望工程。祝你好運。

0

嘗試調用「mmOutputStream.flush();」在「mmOutputStream.write(val);」之後。