2011-11-05 209 views
2

我不確定這是什麼最好的方式,但基本上我想能夠從Android手機發送一個字符到插入Arduino的藍牙模塊。我對Arduino結束沒有任何問題,但是我在Android上遇到了問題。通過藍牙發送字符

到目前爲止,我只有在圖像上單擊事件,當我點擊的形象我想送一個字符,例如,1

這裏是我當前的代碼:

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 

public class ToggleLightsActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

    public void imageClick(View view) 
    { 

    } 
} 

我應該指出我是Android的初學者,正如您可能已經猜到的一樣!

在此先感謝。爲Android藍牙代碼

+0

那麼你發現這個? –

回答

1
import android.app.Activity; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.UUID; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothSocket; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.Toast; 

public class Bluetooth extends Activity{ 

    private static final String TAG = "THINBTCLIENT"; 
    private static final boolean D = true; 
    private BluetoothAdapter mBluetoothAdapter = null; 
    private static String address = "00:06:66:45:0E:93"; 
    private static final UUID MY_UUID =UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
    private BluetoothSocket btSocket = null; 
    private OutputStream outStream = null; 
    public InputStream ins=null; 
    public static String status; 
    static int count_speech=0; 
    static String ipadd=""; 
    static int count_ip=0; 
    static String inst_data=""; 
    static char ret; 
    static String Data=""; 
    static int count=0; 
    Reading rd; 
    static int ch; 

    public void onCreate(Bundle savedInstanceState) 
    { 
      super.onCreate(savedInstanceState); 

      // setContentView(R.layout.bluetooth); 

      Bundle extras = getIntent().getExtras(); 
      status=extras.getString("status"); 
     // Data=String.valueOf(a); 

      if(extras !=null) 
      { 
       show("Inside Bt Activity"); 

       Log.e(TAG, Data); 

       Toast.makeText(getApplicationContext(), "the value is " + Data, Toast.LENGTH_SHORT).show(); 

       mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

       if (mBluetoothAdapter == null) 
       { 
        finish(); 
        return; 
       } 

       if (!mBluetoothAdapter.isEnabled()) 
       { 
        Toast.makeText(this, "Please enable your BT and re-runprogram.", Toast.LENGTH_LONG).show(); 
        finish(); 
        return; 
       } 

       BluetoothDevice device =mBluetoothAdapter.getRemoteDevice(address); 

       try 
       { 

        btSocket =device.createRfcommSocketToServiceRecord(MY_UUID); 

       } 
       catch (Exception e) 
       { 
        Log.e(TAG, "ON RESUME: Socket creation failed.", e); 

       } 
       try 
       { 
        btSocket.connect(); 
        Log.e(TAG, "ON RESUME: BT connection established, datatransfer link open."); 
        Toast.makeText(this, "Blutooth_socket is created", Toast.LENGTH_LONG).show(); 
       } 
       catch (Exception e) 
       {    
        try 
        { 
         btSocket.close(); 
        } 
        catch (IOException e2) 
        { 
         Log.e(TAG, "ON RESUME: Unable to close socket duringconnection failure", e2); 
        } 
       } 
      if(status.equals("something")) 
      { 
       try 
       { 
        char c='whatever character u want'; 
        writeSucess(c); 

       /* outStream=btSocket.getOutputStream(); 
        ins=btSocket.getInputStream(); 
        // Thread.sleep(5000); 
        outStream.write(1); 
        Thread.sleep(5000); 
        outStream.write('2'); 
        //rd=new Reading(); 
        //new Thread(rd). start(); 
        //new Thread(Writing). start(); 
        */ 
       } 
       catch (Exception e) 
       { 
        Log.e(TAG, "ON RESUME: Output stream creation failed.",e); 
       } 

THR = E的事情。創建一個方法並使用這些行forwriteSuccess

+0

請提供一些解釋以及您發佈的代碼。 –