gpt4 book ai didi

android - 管理蓝牙连接安卓

转载 作者:太空狗 更新时间:2023-10-29 14:16:12 24 4
gpt4 key购买 nike

我目前正在尝试将蓝牙 HC-05 模块与 Android 连接,以便它通过模块将字节发送到 Android 设备。我几乎直接从这里的 android 开发者页面中抢走了一些代码: http://developer.android.com/guide/topics/connectivity/bluetooth.html#ManagingAConnection

这是主要的。我遇到的问题是 mhandler 没有收到 MESSAGE_READ 案例,这意味着我没有收到来 self 的模块的数据。我想知道我需要做什么来获取要发送的数据以运行 MESSAGE_READ 案例?到目前为止,程序已将设备配对并向我的 arduino 发送“已成功连接”。

这也是一个可能措辞比我更好的人之前提出的问题,但没有得到回答,所以我想我不是唯一一个。

https://stackoverflow.com/questions/20088856/no-data-buffered-from-bluetooth-module

我在我们的代码中看到的区别主要是他做了 start() 他的 connectedThread()。感谢您的帮助!

public class Main_Activity extends Activity implements OnItemClickListener {

ArrayAdapter<String> listAdapter;

ListView listView;
BluetoothAdapter btAdapter;
Set<BluetoothDevice> devicesArray;
ArrayList<String> pairedDevices;
ArrayList<BluetoothDevice> devices;
public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
protected static final int SUCCESS_CONNECT = 0;
protected static final int MESSAGE_READ = 1;
IntentFilter filter;
BroadcastReceiver receiver;
String tag = "debugging";
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Log.i(tag, "in handler");
super.handleMessage(msg);
switch(msg.what){
case SUCCESS_CONNECT:
// DO something
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
//Toast.makeText(getApplicationContext(), "CONNECT", 0).show();
String s = "successfully connected";
connectedThread.write(s.getBytes());
Log.i(tag, "connected");

break;
case MESSAGE_READ:
byte[] readBuf = (byte[])msg.obj;
String string = new String(readBuf);
Toast.makeText(getApplicationContext(), string, 0).show();
break;

}
}

};

这是应该向上发送处理程序的代码:

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;
Log.i(tag, "construct");
// 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) {
Log.i(tag, "get socket failed");

}
mmSocket = tmp;
}

public void run() {
// Cancel discovery because it will slow down the connection
btAdapter.cancelDiscovery();
Log.i(tag, "connect - run");
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
Log.i(tag, "connect - succeeded");
} catch (IOException connectException) { Log.i(tag, "connect failed");
// 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)

mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
}



/** 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; // 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
buffer = new byte[1024];
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();

} catch (IOException e) {
break;
}
}
}

最佳答案

您对线程的使用完全错误。

创建线程对象并不会启动物理线程!

因此,正如您所做的那样,放置一个可能很慢的套接字创建者函数线程对象的构造函数又是错误的。

而且,由于您还没有启动 ()-ed 您的线程,套接字 connect() 永远不会被调用!

请按以下方式尝试:

class ConnectThread extends Thread {

public ConnectThread(BluetoothDevice device) {
// nothing long running here!
}

public void run() {
device.createRfcommSocketToServiceRecord(MY_UUID);

// followed by connect() etc.
}
}

记得调用激活线程

  connectedThread.start();

否则什么也不会发生。

关于android - 管理蓝牙连接安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21869024/

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