我是android开发应用程序的新手。想从我的蓝牙套接字读取缓冲区中的数据并显示它。数据来自温度传感器(LM35>微控制器>蓝牙模块> android设备)。下面是编码读取并将数据保存到buffer.i想知道,如何显示它。希望你们能帮助我。非常感谢你..
byte[] buffer = new byte[1024];
int bytes;
try {
InputStream instream = Bee_btSocket.getInputStream();
bytes = instream.read(buffer);
}
catch (IOException e) {
break;
答案 0 :(得分:0)
我通过以下代码来做这件事...
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}