gpt4 book ai didi

java - 如何使用类 - Bluetooth Thread

转载 作者:行者123 更新时间:2023-11-30 11:19:28 25 4
gpt4 key购买 nike

我正在尝试连接 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) 的帮助将不胜感激!

最佳答案

我不确定你模糊地理解了什么,不理解什么,但我会尝试从总体上解释这些类的目的..

ConnectThread - 接收在发现阶段发现的蓝牙设备(显然在连接之前)从设备获取 BT 套接字,并在调用 run() 时尝试连接到它。如果连接成功 - 在代码中它只是说 manageConnectedSocket(mmSocket); 但这意味着你应该打开一个 ConnectedThread 以通过套接字接收和发送数据。

ConnectedThread - 如前所述,是管理发送和接收数据的线程。正如您在 run() 中看到的那样,它不断地使用 while(true) 进行监听,它调用阻塞的 read - 意思是“线程卡在那里”,直到它收到传入的数据。当接收到数据时,它会使用此处也未实现的 mHandler 对其进行处理,同样,您应该只实现您想要对接收到的数据执行的任何操作。write 方法只是接收一个字节数组并将其写入套接字,请注意,这也是一个阻塞调用,因此您应该从另一个线程使用它。

希望这能帮助你理解

关于java - 如何使用类 - Bluetooth Thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23211800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com