2011-04-02 120 views
1

我正在編寫Android設備2.2和藍牙RS232適配器之間的簡單通信程序。Android藍牙讀取btrs232適配器

我設法成功連接和發送文本,但是當從適配器讀取應用程序時會崩潰。

我很感激任何幫助和建議。 感謝

的main.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView 
android:id="@+id/text_messages" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 
<Button android:text="Button" android:id="@+id/button_listen" 
android:layout_width="wrap_content" android:layout_height="wrap_content"> 
</Button> 
</LinearLayout> 

MyActivity.java

package com.epostech.bt232test; 

import java.io.IOException; 
import java.io.OutputStream; 
import java.util.Set; 
import java.util.UUID; 
import java.util.Vector; 

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.os.Handler; 
//import android.widget.ArrayAdapter; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 

import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MyActivity extends Activity { 
    private static final int REQUEST_ENABLE_BT = 3; 
    private String mac = ""; 
    private static final UUID MY_UUID_INSECURE = UUID 
      .fromString("00001101-0000-1000-8000-00805F9B34FB"); 
    private BluetoothSocket clientSocket; 
    // private ArrayAdapter<String> mArrayAdapter; 
    private Vector<String> deviceMacs = new Vector<String>(); 
    private Vector<String> deviceNames = new Vector<String>(); 
    @SuppressWarnings("unused") 
    private Handler handler = new Handler(); 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     final TextView messageText = (TextView) findViewById(R.id.text_messages); 

     // messageText.setVisibility(View.VISIBLE); 
     setContentView(R.layout.main); 

     Button listenButton = (Button) findViewById(R.id.button_listen); 
     listenButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View view) { 
       // closeSocket(); 
       sendMessage(clientSocket, "P\r\n"); 
       // testing(mac); 
      } 
     }); 
     BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); 
     String toastText = ""; 
     /* 
     * if(bluetooth.isEnabled()){ String address= bluetooth.getAddress(); 
     * String name=bluetooth.getName(); toastText=name+" : "+address; 
     * 
     * } else 
     */ 
     if (!bluetooth.isEnabled()) { 
      toastText = "Bluetooth is not Enabled!"; 
      Toast.makeText(this, toastText, Toast.LENGTH_LONG).show(); 
      Intent enableBtIntent = new Intent(
        BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
      Toast.makeText(this, "BlueTooth Enabled", Toast.LENGTH_LONG).show(); 
     } 

     Set<BluetoothDevice> pairedDevices = bluetooth.getBondedDevices(); 
     // Toast.makeText(this,"Size="+pairedDevices.size(), 
     // Toast.LENGTH_LONG).show(); 

     // If there are paired devices 

     if (pairedDevices.size() > 0) { 
      // Loop through paired devices 
      for (BluetoothDevice device : pairedDevices) { 
       // Add the name and address to an array adapter to show in a 
       // ListView 
       deviceMacs.add(device.getAddress()); 
       deviceNames.add(device.getName()); 

      } 
     } else { 
      Toast.makeText(this, "Size=" + pairedDevices.size(), 
        Toast.LENGTH_LONG).show(); 
     } 

     mac = deviceMacs.get(deviceNames.indexOf("M7705B0125")); 
     BluetoothDevice device = bluetooth.getRemoteDevice(mac); 
     try { 
      clientSocket = device 
        .createRfcommSocketToServiceRecord(MY_UUID_INSECURE); 
      clientSocket.connect(); 
      // TODO Transfer data using the Bluetooth Socket 

     } catch (IOException e) { 
      Log.d("BLUETOOTH", e.getMessage()); 

     } 

     BluetoothSocketListener bsl = new BluetoothSocketListener(clientSocket, handler, messageText); 
     Thread messageListener = new Thread(bsl); 
     messageListener.start(); 
    } 

    private void closeSocket() { 
     try { 
      clientSocket.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    private void sendMessage(BluetoothSocket socket, String msg) { 
     OutputStream outStream; 
     try { 
      outStream = socket.getOutputStream(); 
      byte[] byteString = msg.getBytes(); 
      byteString[byteString.length - 1] = 0; 
      outStream.write(byteString); 
     } catch (IOException e) { 
      Log.d("BLUETOOTH_COMMS", e.getMessage()); 
     } 
    } 

    // The Handler that gets information back from the BluetoothChatService 

    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     closeSocket(); 
    } 

} 

BluetoothSocketListener.java

package com.epostech.bt232test; 

import java.io.IOException; 
import java.io.InputStream; 

import android.bluetooth.BluetoothSocket; 
import android.os.Handler; 
import android.util.Log; 
import android.widget.TextView; 


public class BluetoothSocketListener implements Runnable { 

     private BluetoothSocket socket; 
     private TextView textView; 
     private Handler handler; 

     public BluetoothSocketListener(BluetoothSocket socket, 
            Handler handler, TextView textView) { 
     this.socket = socket; 
     this.textView = textView; 
     this.handler = handler; 
     } 
     public void run() { 

      byte[] buffer = new byte[1024]; 
      int bytes; 
      InputStream instream=null; 
     try { 
      instream = socket.getInputStream(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
      String message = ""; 
      // Keep listening to the InputStream while connected 
      while (true) { 
       try { 
        // Read from the InputStream 
        bytes = instream.read(buffer); 
        message = message + new String(buffer, 0, bytes); 
        // Send the obtained bytes to the UI Activity 
        handler.post(new MessagePoster(textView, message)); 

       } catch (IOException e) { 

        break; 
       } 
      } 
     } 


     /* 
    public void run() { 
    int bufferSize = 256; 
    byte[] buffer = new byte[bufferSize];  
    try { 
     InputStream instream = socket.getInputStream(); 
     int bytesRead = -1; 
     String message = ""; 
     while (true) { 
     message = ""; 
     bytesRead = instream.read(buffer); 
     if (bytesRead != -1) { 
      while ((bytesRead==bufferSize)&&(buffer[bufferSize-1] != 0)) { 
      message = message + new String(buffer, 0, bytesRead); 
      bytesRead = instream.read(buffer); 
      } 
      message = message + new String(buffer, 0, bytesRead - 1); 

      handler.post(new MessagePoster(textView, message));    
      //socket.getInputStream(); 
     } 
     } 
    } catch (IOException e) { 
     Log.d("BLUETOOTH_COMMS", e.getMessage()); 
    } 
    } 
    */ 
} 

MessagePoster.java

package com.epostech.bt232test; 

import android.widget.TextView; 

public class MessagePoster implements Runnable { 
    private TextView textView; 
    private String message; 

    public MessagePoster(TextView textView, String message) { 
     this.textView = textView; 
     this.message = message; 
    } 

    public void run() { 
     textView.setText(message); 
    } 
} 

回答

1

問題解決 我可以讀取和寫入RS232並使用Handler在TextView上顯示結果以與UI和套接字線程進行通信。

這是很難excersise但我用簡單的程序做了如下:

/** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // messageText.setVisibility(View.VISIBLE); 
     setContentView(R.layout.main); 

     messageText = (TextView) findViewById(R.id.text_messages); 
     Button listenButton = (Button) findViewById(R.id.button_listen); 
     listenButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       // closeSocket(); 

       sendData(clientSocket, "P\r\n"); 

      } 
     }); 

     BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); 
     String toastText = ""; 
     /* 
     * if(bluetooth.isEnabled()){ String address= bluetooth.getAddress(); 
     * String name=bluetooth.getName(); toastText=name+" : "+address; 
     * 
     * } else 
     */ 
     if (!bluetooth.isEnabled()) { 
      toastText = "Bluetooth is not Enabled!"; 
      Toast.makeText(this, toastText, Toast.LENGTH_LONG).show(); 
      Intent enableBtIntent = new Intent(
        BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
      Toast.makeText(this, "BlueTooth Enabled", Toast.LENGTH_LONG).show(); 
     } 

     Set<BluetoothDevice> pairedDevices = bluetooth.getBondedDevices(); 
     // Toast.makeText(this,"Size="+pairedDevices.size(), 
     // Toast.LENGTH_LONG).show(); 

     // If there are paired devices 

     if (pairedDevices.size() > 0) { 
      // Loop through paired devices 
      for (BluetoothDevice device : pairedDevices) { 
       // Add the name and address to an array adapter to show in a 
       // ListView 
       deviceMacs.add(device.getAddress()); 
       deviceNames.add(device.getName()); 

      } 
     } else { 
      Toast.makeText(this, "Size=" + pairedDevices.size(), 
        Toast.LENGTH_LONG).show(); 
     } 

     mac = deviceMacs.get(deviceNames.indexOf("M7705B0125")); 
     BluetoothDevice device = bluetooth.getRemoteDevice(mac); 
     try { 
      clientSocket = device 
        .createRfcommSocketToServiceRecord(MY_UUID_INSECURE); 
      clientSocket.connect(); 

      // TODO Transfer data using the Bluetooth Socket 

     } catch (IOException e) { 
      Log.d("BLUETOOTH", e.getMessage()); 

     } 
     BluetoothSocketListener bsl = new BluetoothSocketListener(clientSocket, 
       handler, messageText); 
     Thread messageListener = new Thread(bsl); 
     messageListener.start(); 
    }// end of onCreate()code 
     // The Handler that gets information back from the BluetoothChatService 

    private final Handler mHandler = new Handler() { 

     @Override 
     public void handleMessage(Message msg) { 
      byte[] readBuf = (byte[]) msg.obj; 
      // construct a string from the valid bytes in the buffer 
      String readMessage = new String(readBuf, 0, msg.arg1); 
      messageText.setText(readMessage); 

     } 

    }; 

    private void closeSocket() { 
     try { 
      clientSocket.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    // The Handler that gets information back from the BluetoothChatService 

    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     closeSocket(); 
    } 

     private class BluetoothSocketListener implements Runnable { 

     private BluetoothSocket socket; 
     private TextView textView; 
     private Handler handler; 
     private InputStream inStream; 
     private OutputStream outStream; 

     public BluetoothSocketListener(BluetoothSocket socket, Handler handler, 
       TextView textView) { 
      this.socket = socket; 
      this.textView = textView; 
      this.handler = handler; 
      try { 
       outStream = socket.getOutputStream(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

     public void run() { 
      int bufferSize = 1024; 
      byte[] buffer = new byte[bufferSize]; 
      int data; 
      try { 
       inStream = socket.getInputStream(); 
       int bytesRead = -1; 
       String message = ""; 
       // Keep listening to the InputStream while connected 
       int len= 0; 



       while (true) { 
        try { 
         // Read from the InputStream 

         bytesRead = inStream.read(buffer); 
         message= message+new String(buffer,0,bytesRead); 
         // Send the obtained bytes to the UI Activity 
         byte[] byteString = message .getBytes(); 
         byteString[byteString.length - 1] = 0; 
         outStream.write(byteString); 
         handler.post(new MessagePoster(textView,"Text="+ message+" "+"Bytes read="+bytesRead)); 

        } catch (IOException e) { 
         Log.e(TAG, "disconnected", e); 

         break; 
        } 
       } 


      } catch (IOException e) { 
       Log.d("BLUETOOTH_COMMS", e.getMessage()); 
      } 
     } 
    } 

    private class MessagePoster implements Runnable { 
     private TextView textView; 
     private String message; 

     public MessagePoster(TextView textView, String message) { 
      this.textView = textView; 
      this.message = message; 
     } 

     public void run() { 
      textView.setText(message); 
     } 
    } 

    private void sendData(BluetoothSocket socket, String msg) { 
     OutputStream outStream; 
     try { 
      outStream = socket.getOutputStream(); 
      byte[] byteString = msg.getBytes(); 
      //byteString[byteString.length - 1] = 0; 
      outStream.write(byteString); 
     } catch (IOException e) { 
      Log.d("BLUETOOTH_COMMS", e.getMessage()); 
     } 
    } 
} 
0

不要忘記將許可添加到清單

<uses-permission 
    android:name="android.permission.BLUETOOTH" /> 
0

我使用串行到BT發現一件事適配器連接到微量控制器。 通常,發送到串口的數據是以[]結尾的字符串。

我在我的一個項目中使用了串口轉藍牙適配器,發送的數據以a結尾,發現使用掃描器而不是標準緩衝區處理數據確實效果更好。 修改後的藍牙聊天示例:

Scanner scan = new Scanner(new InputStreamReader(mmInStream)); 
     String line; 
     // Keep listening to the InputStream while connected 
     while (true) { 
      try { 
        // Read from the InputStream 
       //Send the obtained bytes to the UI Activity 
       line = scan.next(); 
       mHandler.obtainMessage(CalibrationS2PActivity.MESSAGE_READ, line.length(), -1, line).sendToTarget(); 
      } catch (Exception e) { 
       Log.e(TAG, "disconnected", e); 
       connectionLost(); 
       break; 
      } 
}