Chr*_*isX 4 io android bluetooth inputstream android-bluetooth
我正在尝试测试PC 和 Android 手机之间的蓝牙通信示例。我的 SPP 客户端正是来自那里的客户端,并且运行良好。我是 Android 新手,我不想让它在单独的线程中运行,因为我不知道如何运行,所以我只是在方法中完成了所有操作onCreate()。如果这不是最好的方法,请随时为我指出更好的方法,但这不是我的主要问题。
问题是我想在 a 上显示通过蓝牙收到的文本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
}
// }
}
}
Run Code Online (Sandbox Code Playgroud)
我注释掉了while(true)循环,因为我认为它使我的应用程序在onPause()被调用时崩溃。我知道这不是最好的实现,但我真的想从蓝牙读取,我觉得我非常接近:),其他方面将在之后处理(例如使用线程等)。
TextView我终于成功地在从 PC 发送的字符串中正确显示(“来自 SPP 客户端的测试字符串\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
Run Code Online (Sandbox Code Playgroud)
这是一个非常基本的示例,仅用于展示如何在设备屏幕上显示通过蓝牙接收的字符串。它不是在单独的线程中完成的,在收到字符串后,您必须关闭应用程序并再次重新启动它,但应用程序的主要目的已经实现(正如我在问这个问题时所说的那样)。我真正真正想要的是从 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
}
// }
}
}
Run Code Online (Sandbox Code Playgroud)
任何问题?:)
| 归档时间: |
|
| 查看次数: |
25686 次 |
| 最近记录: |