所以我试图做一些事情有点不寻常,它只是为了好玩。我有一个使用libgdx创建的游戏,它包含一个可以拍摄的船。我想要做的是使用一些外部按钮来移动它。按钮将信号发送给arduino,然后将它们发送到HC-05蓝牙模块。然而,我很怀疑事情的android方面。我做的基本上是以下几点:连接到Arduino的Android应用程序,它使用libgdx(使用蓝牙)
因为我在使用libgdx我创建了一个名为BluetoothDude的接口,它有三个方法setBluetooth()(它将为特定平台设置蓝牙),String whatIsTheMessage()会告诉你发送给手机的内容)和布尔值isActive(),以了解当然蓝牙是否处于活动状态。
的MainGame将收到BluetoothDude使得特定类,如船舶有权访问消息,并能够对其作出反应。
然后我做了针对Android的特定实现蓝牙,在setBluetooth()我遵循这个指南非常密切:https://developer.android.com/guide/topics/connectivity/bluetooth.html
我敢肯定它是正确连接,因为当它创建的插座就可以打印“与HC-05的连接成功”(只有在创建套接字的方法(我称之为BTConnect()返回true)时才会打印)。
这个问题似乎是在读取数据时,我正在使用的代码是
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
private Handler handler;
public ConnectedThread(BluetoothSocket socket,Handler mHandler) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
handler = mHandler;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024];
int bytes;
while (true) {
try {
bytes = mmInStream.read(buffer);
handler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
break;
}
}
}
我setBluetooth使这个类的一个对象像这样
if (device != null) {
if (BTconnect()) {
isActive = true;
connectedThread = new ConnectedThread(socket,handler);
System.out.println("connection success with" + device.getName() + " message: " + message);
}
我有很多怀疑 首先这里的目标是什么,mHandler是在BluetoothDude中创建的,那么目标是什么?第二,我很确定线程甚至没有运行,因为如果我把一行像System.out.println( “运行”)内运行()它并没有显示出像万亿英寸的行执行应用程序时的logcat。它有什么问题,我希望你能帮助我,我并不是很有经验,这让我发疯。