我正在开发一个应用程序,它应该与 Bluehood 完全相同,Bluehood 是 Google 市场上的一个应用程序。
所以现在我正在研究蓝牙。事实是,我想在两个设备之间传输字符串(JSON)。我在 stackoverflow 上看到了很多帖子,在互联网上看到了一些例子,但我不太清楚。
我知道我必须使用 createInsecureRfcommSocketToServiceRecord 发送信息,并使用 ListenUsingInsecureRfcommWithServiceRecord 接收信息,但我正在搜索一些简单的教程来解释它的工作原理以及如何在两个设备之间传输数据。
提前感谢您的解释...
最佳答案
很难知道我是否有效地回答了这个问题,正如您所说,您已经搜索过网络,并且我在 android com on Bluetooth 找到了最有用的教程之一。我提供了部分代码,不是完整的线程类,而是让您了解在连接期间如何使用临时套接字直到找到套接字并将其确定为最终套接字,以及线程如何管理每个阶段的信息。连接过程。
listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
用于创建服务器套接字。它监听连接。它的行为就像服务器。这是在充当服务器或监听传入连接的设备上。
这是在一个单独的线程中完成的。
public AcceptThread() {
BluetoothServerSocket tmp = null;
// Create a new listening server socket
try {
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
} catch (IOException e) {
}
mmServerSocket = tmp;
}
public void run() {
BluetoothSocket socket = null;
// Listen to the server socket if we're not connected
while (mState != STATE_CONNECTED) {
try {
// This is a blocking call and will only return on a
// successful connection or an exception
socket = mmServerSocket.accept();
} catch (IOException e) {
break;
}
// If a connection was accepted
if (socket != null) {
synchronized (BluetoothConnection.this) {
switch (mState) {
case STATE_LISTEN:
case STATE_CONNECTING:
// Situation normal. Start the connected thread.
connected(socket, socket.getRemoteDevice());
break;
case STATE_NONE:
case STATE_CONNECTED:
// Either not ready or already connected. Terminate new socket.
try {
socket.close();
} catch (IOException e) {
}
break;
}
}
}
}
}
有一个单独的线程充当客户端,寻求连接。它会寻找连接。这是在寻求与服务器设备连接的设备上。 (这些可以互换)。
public ConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
}
mmSocket = tmp;
}
public void run() {
// Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery();
// Make a connection to the BluetoothSocket
try {
// This is a blocking call and will only return on a
// successful connection or an exception
mmSocket.connect();
} catch (IOException e) {
// Close the socket
try {
mmSocket.close();
} catch (IOException e2) {
}
connectionFailed();
return;
}
然后您需要一个线程来管理实际的连接。当客户端遇到服务器时。也在一个单独的线程中。
public ConnectedThread(BluetoothSocket socket) {
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) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
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(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
connectionLost();
// Start the service over to restart listening mode
BluetoothConnection.this.start();
break;
}
}
}
在此线程中,您还可以使用代码来管理通过此连接写入数据。
有samples通过 android.com 提供。 我还找到了this tutorial很好,作为蓝牙发现和连接的简单背景,尽管它并没有为您提供读取和写入数据所需的所有内容。
就读取和写入数据而言,以下代码 fragment 是处理读取数据并将其解析为可用内容的方法的示例。从连接线程内调用处理程序。在本例中,我将数据附加到 textView,但您可以用它做任何您想做的事情,它展示了如何将其放入字符串中。 (这就是您正在寻找的)。
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
textView1.append("\nMessage " + messageCount + ": " + readMessage);
....
同样,还有一些用于写入消息的代码 - 这是在连接的线程类中。但是,我使用带有发送按钮的 OnClick 事件来获取此信息。从 EditText 中获取文本并将其发送到函数以将字符串解析为字节。
其中 message 是一个字符串,mChatService 正在从 Connected 线程调用 write 方法。 将字符串转换为字节数组,以便可以发送。
// Get the message bytes and tell the BTManager to write
byte[] send = message.getBytes();
mChatService.write(send);
从连接的线程写入方法:
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
mHandler.obtainMessage(MESSAGE_WRITE, -1, -1, buffer).sendToTarget();
} catch (IOException e) {
}
}
值得注意的是,必须监视设备的状态(您可以查看相关教程)。
让后台线程远离 UI 也很重要。因此,这就是在 UI 和套接字连接之间传输数据的技能(和处理程序)的用武之地。
关于android - 通过蓝牙发送字符串,我需要一些信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28213056/