我试图连接android和arduino并在它们之间发送数据。我正在遵循指南http://developer.android.com/guide/topics/connectivity/bluetooth.html#ManagingAConnection
我想我隐约明白这是如何运作的,但我并没有完全掌握基础知识,所以我有点卡住了。
我正在查看"作为客户端连接的代码":
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
mmDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.cancelDiscovery();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
// Do work to manage the connection (in a separate thread)
manageConnectedSocket(mmSocket);
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
和"管理连接"
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
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) {
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
我基本上复制了确切的代码并制作了包含它们的新java文件。 我想实际使用这些类来发送数据,所以我配对了设备,然后找到了ID:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0)
{
// Loop through paired devices
for (BluetoothDevice device : pairedDevices)
{
if (device.getName().equals("HC-06"))
{
//NEED TO INSERT CODE HERE (I think...)
}
else
{
Intent intentneedsetting = new Intent(this, NeedSettingsActivity.class);
startActivity(intentneedsetting);
}
}
}
else
{
Intent intentneedsetting = new Intent(this, NeedSettingsActivity.class);
startActivity(intentneedsetting);
}
非常感谢有关如何使用这些类(ConnectThread / ConnectedThread)的任何帮助!
答案 0 :(得分:1)
我不确定你是什么意思,什么不是,但我会试着解释一下这些课程的目的..
ConnectThread
- 接收在发现阶段发现的蓝牙设备(显然在连接之前)从设备获取BT套接字,当调用run()时,它尝试连接到它。
如果连接成功 - 在代码中它只是说manageConnectedSocket(mmSocket);
但这意味着你应该打开ConnectedThread
来通过套接字接收和发送数据。
ConnectedThread -
是管理发送和接收数据的线程。正如你在run()
中看到的那样,它经常使用while(true)
监听它调用阻塞的读取 - 意味着“线程卡在那里”,直到它接收到传入的数据。
当收到数据时,它使用mHandler处理它,这也没有在这里实现,你应该再次实现你想要对收到的数据做什么。
write方法只接收一个字节数组并将其写入套接字,请注意,这也是一个阻塞调用,因此你应该从另一个线程使用它。
希望这有助于你理解