我有一个我想通过蓝牙控制的机器人。我在蓝牙插座上写字要求机器人移动,如果机器人成功地读取我的消息,它会向我发回信号,说“我读你了!”。问题是机器人需要3秒钟才能响应。我的理想情况是这样的
public class Robot {
private boolean lastMsgWasRead;
public boolean askMeWalk(){
lastMsgWasRead=false;
writeToBluetooth("Walk");
// somehow wait 3 seconds at least.
if (lastMsgWasRead){
return true; // sucessful walk
}else{
return false; // the robot didn't hear you
}
}
}
现在我的问题是:我有什么可以用的:
我已经阅读了postDelayed, timer, and ScheduledExecutorService
等等......
但他们都没有帮我找到对我来说非常重要的回报值。如果我想使用postDelayed左右,我不能设置返回值,因为它们在独立线程上运行。
我尝试了Thread.sleep(3000),但我意识到它确实会因为它停止主线程而产生任何影响 - 所以处理程序没有得到更改以在3000ms.
期间更新lastMsgWasRead值
任何想法??
答案 0 :(得分:0)
所以你可以做一些混合的事情。 3秒很长一段时间。所以你真的不希望你的主线程等待那么久。也许是这样的。
public class AwesomeActivity extends Activity implements Handler.Callback {
private Handler handler;
private AsyncTask<?,?,?> task;
public void onCreate(Bundle b) {
handler = new Handler(this);
// Send your data to the robot via an async task
// Have the async task and listen for the data in the background
task = new TalkAndListen() {
public void onPostExecute(String response) {
// The postExecute() method joins back to the main thread
// and cancels the async callback you fired off earlier on success
handler.removeMessages(0);
// Yay success!
}
}.execute("Walk");
handler.sendEmptyMessageDelayed(0, 3000);
}
public boolean handleMessage(Message message) {
if (message.what == 0) {
// Sad Trombone, kill off the async thread, 3 secs has passed
task.cancel(true);
}
}
private static class TalkAndListen extends AsyncTask<String, Void, String> {
// This is done on a background thread
public String doInBackground(String... words) {
for (String word : words) {
writeToBluetooth(word);
}
return readFromBluetooth();
}
// This is the string returned from doInBackground() passed to the main thread
public void onPostExecute(String responseSignal) {
}
}
}
答案 1 :(得分:0)
我前一段时间创建了我的课程以帮助解决这种情况,你可以看到它here。它允许您发送数据并异步等待它。例如,您将以这种方式使用它:
BluetoothHelper mBluetoothHelper = new BluetoothHelper(this, "bluetoothDeviceName", false, null);
mBluetoothHelper.setConnectionDialog(true); // Shows a dialog while connecting
mBluetoothHelper.connect(true); // Connect to Bluetooth device
因此,通过这种方式,您可以连接到蓝牙设备,现在可以接收实施OnNewBluetoothDataReceivedListener
所需的数据:
mBluetoothHelper.setOnNewBluetoothDataReceived(this);
// Called when you receive data from Bluetooth
@Override
public boolean onNewBluetoothDataReceivedListener(InputStream mBTIn, OutputStream mBTOut) {
}
因此,您不必等待任何事情,当您收到数据时,它将被异步调用。发送数据:
mBluetoothHelper.write(data);
您必须使用此方法实现编写String
的方法。所以你需要
DeviceList.java
strings.xml
还需要<string name="scanning"> Scanning…</string>
<string name="select_device"> Select device</string>
<string name="none_paired"> No paired devices</string>
<string name="none_found"> No devices were found</string>
<string name="title_paired_devices"> Paired devices</string>
<string name="title_other_devices"> Other available devices</string>
<string name="button_scan"> Scan</string>
AndroidManifest.xml
在 <activity android:name=".DeviceListActivity"
android:label="@string/select_device"
android:theme="@android:style/Theme.Holo.Dialog"
android:configChanges="orientation|keyboardHidden" >
</activity>
:
BluetoothHelper.java
您需要这样做,因为当您扫描连接到蓝牙设备时会显示可用设备列表,如果您不需要,可以在{{1}}中对其进行修改。