java如何使用蓝牙线程
我正在尝试连接安卓和arduino,并在它们之间发送数据。我在遵循指南http://developer.安卓.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)的帮助都将不胜感激
# 1 楼答案
我不确定你vaguley理解了什么,也不确定你不理解什么,但我会试着解释一下这些课程的总体目的
ConnectThread
接收在发现阶段(显然是在连接之前)发现的蓝牙设备,从该设备获取BT套接字,并在调用run()时尝试连接到该设备。 如果连接成功——在代码中它只是说manageConnectedSocket(mmSocket);
,但这意味着你应该打开一个ConnectedThread
,通过套接字接收和发送数据ConnectedThread -
如前所述,是用于管理发送和接收数据的线程。正如您在run()
中看到的,它不断地使用一个while(true)
来监听,它调用正在阻塞的read,这意味着“线程被卡住了”,直到它接收到传入的数据。 当接收到数据时,它会使用mHandler进行处理,这里也没有实现,同样,您应该只实现您想要对接收到的数据执行的任何操作。 write方法只接收字节数组并将其写入套接字,注意这也是一个阻塞调用,因此您应该从另一个线程使用它希望这能帮助你理解