[英]how to read from the InputStream of a bluetooth on Android
我正在嘗試測試PC和Android手機之間的藍牙通信示例 。 我的SPP客戶端恰好就是那里的那個,並且工作正常。 我是Android的新手,我不希望使其在單獨的線程中運行,因為我不知道如何操作,因此我只是在onCreate()
方法中進行了所有操作。 如果這不是最佳方法,請隨時為我提供更好的方法,但這不是我的主要問題。
問題是我想將通過藍牙接收的文本顯示在textView
,但我不知道如何從InputStream
讀取。 當代碼這樣保留時,它顯示類似java.io.DataInputStream@41b0cb68
我在這里嘗試了它不顯示任何內容,也不知道正在使用什么編碼。
這是我的android應用程序的代碼:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.*;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
//based on java.util.UUID
private static UUID MY_UUID = UUID.fromString("446118f0-8b1e-11e2-9e96-0800200c9a66");
// The local server socket
private BluetoothServerSocket mmServerSocket;
// based on android.bluetooth.BluetoothAdapter
private BluetoothAdapter mAdapter;
private BluetoothDevice remoteDevice;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.textView_Text);
BluetoothSocket socket = null;
mAdapter = BluetoothAdapter.getDefaultAdapter();
// Listen to the server socket if we're not connected
// while (true) {
try {
// Create a new listening server socket
Log.d((String) this.getTitle(), ".....Initializing RFCOMM SERVER....");
// MY_UUID is the UUID you want to use for communication
mmServerSocket = mAdapter.listenUsingRfcommWithServiceRecord("MyService", MY_UUID);
//mmServerSocket = mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME, MY_UUID); // you can also try using In Secure connection...
// This is a blocking call and will only return on a
// successful connection or an exception
socket = mmServerSocket.accept();
} catch (Exception e) {
}
try {
Log.d((String) this.getTitle(), "Closing Server Socket.....");
mmServerSocket.close();
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
DataInputStream mmInStream = new DataInputStream(tmpIn);
DataOutputStream mmOutStream = new DataOutputStream(tmpOut);
// here you can use the Input Stream to take the string from the client whoever is connecting
//similarly use the output stream to send the data to the client
text.setText(mmInStream.toString());
} catch (Exception e) {
//catch your exception here
}
// }
}
}
我注釋了while(true)
循環,因為我認為它在調用onPause()
時使我的應用程序崩潰。 我知道這不是最好的實現,但是我真的很想從藍牙中讀到我覺得自己很親近的:),之后將處理其他方面(例如使用線程等)。
我終於設法在TextView
正確顯示了從PC發送的字符串(“來自SPP Client的測試字符串\\ r \\ n”)。
我使用了這個問題 ,即這段代碼,就在DataOutputStream mmOutStream = new DataOutputStream(tmpOut);
:
// Read from the InputStream
bytes = mmInStream.read(buffer);
String readMessage = new String(buffer, 0, bytes);
// Send the obtained bytes to the UI Activity
這是一個非常基本的示例,僅用於顯示如何在設備的屏幕上顯示通過藍牙接收的字符串。 它不是在單獨的線程中完成的,接收到字符串之后,您必須關閉應用程序並再次重新啟動它,但是實現了應用程序的主要目的(正如我在問這個問題時所說的那樣)。 我真正想要的是從PC接收一個字符串並將其顯示在屏幕上。
這是我完整的MainActivity
,如果有人要我發布更完整的方法(例如使用單獨的線程),則在完成后將其發布到此處。
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.*;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
//based on java.util.UUID
private static UUID MY_UUID = UUID.fromString("446118f0-8b1e-11e2-9e96-0800200c9a66");
// The local server socket
private BluetoothServerSocket mmServerSocket;
// based on android.bluetooth.BluetoothAdapter
private BluetoothAdapter mAdapter;
private BluetoothDevice remoteDevice;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.textView_Text);
BluetoothSocket socket = null;
mAdapter = BluetoothAdapter.getDefaultAdapter();
// Listen to the server socket if we're not connected
// while (true) {
try {
// Create a new listening server socket
Log.d((String) this.getTitle(), ".....Initializing RFCOMM SERVER....");
// MY_UUID is the UUID you want to use for communication
mmServerSocket = mAdapter.listenUsingRfcommWithServiceRecord("MyService", MY_UUID);
//mmServerSocket = mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME, MY_UUID); // you can also try using In Secure connection...
// This is a blocking call and will only return on a
// successful connection or an exception
socket = mmServerSocket.accept();
} catch (Exception e) {
}
byte[] buffer = new byte[256]; // buffer store for the stream
int bytes; // bytes returned from read()
try {
Log.d((String) this.getTitle(), "Closing Server Socket.....");
mmServerSocket.close();
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
DataInputStream mmInStream = new DataInputStream(tmpIn);
DataOutputStream mmOutStream = new DataOutputStream(tmpOut);
// here you can use the Input Stream to take the string from the client whoever is connecting
//similarly use the output stream to send the data to the client
// Read from the InputStream
bytes = mmInStream.read(buffer);
String readMessage = new String(buffer, 0, bytes);
// Send the obtained bytes to the UI Activity
text.setText(readMessage);
} catch (Exception e) {
//catch your exception here
}
// }
}
}
任何問題? :)
基本上,您需要將數據從一種設備發送的方式與另一種設備接收的方式進行匹配。
SPP基於流,並傳輸數據字節。 因此,接收方必須正確解釋發送設備發送的任何字節。
InputStream
使您可以訪問傳輸的原始字節,並且您必須對它們進行一些處理。 即根據需要以某種方式對其進行解碼。 例如,如果發送方在發送之前使用ObjectOutputStream
進行編碼,則接收方將必須使用ObjectInputStream
解碼輸入。
您可能需要閱讀InputStream
( read()
), ObjectInputStream
和toString()
。
此外,從阻塞流中讀取數據幾乎總是應該在單獨的線程中完成; 特別是在從某些遠程設備/主機/網絡/ ...讀取數據時,可能會出現未知的延遲或傳輸速度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.