Android 安卓:如何实现一个;“等待接收数据”;在异步任务中

Android 安卓:如何实现一个;“等待接收数据”;在异步任务中,android,android-asynctask,Android,Android Asynctask,我正在编写一个Android应用程序,它将通过BLE(蓝牙低能量)向另一个设备发送一条消息,该设备将响应ACK/NACK消息。我使用的BLE服务将使通信像普通UART通信一样工作 我在一个异步任务中实现了两个设备之间的通信,因为通信涉及许多发送/接收循环。我可以发送消息和接收消息,问题是在发送消息后,我需要等待至少一段时间(超时)才能接收响应。在这段等待时间内,我需要反复检查是否收到有效响应,超时后,我需要停止等待。我知道我们可以让AsyncTask睡眠,所以睡眠时间就是超时。但是,如果我只能在

我正在编写一个Android应用程序,它将通过BLE(蓝牙低能量)向另一个设备发送一条消息,该设备将响应ACK/NACK消息。我使用的BLE服务将使通信像普通UART通信一样工作

我在一个异步任务中实现了两个设备之间的通信,因为通信涉及许多发送/接收循环。我可以发送消息和接收消息,问题是在发送消息后,我需要等待至少一段时间(超时)才能接收响应。在这段等待时间内,我需要反复检查是否收到有效响应,超时后,我需要停止等待。我知道我们可以让AsyncTask睡眠,所以睡眠时间就是超时。但是,如果我只能在完全睡眠时间(如3秒钟)后检查消息,则效率低下

怎么做

以下是我的任务:

public class configTask extends AsyncTask<String, Integer, Integer> {
    @Override
    protected Integer doInBackground(String... message) {

        // Using StringBuilder here just to show the example, 
        // I will add more string here in real situation
        final StringBuilder sb = new StringBuilder(20);
        sb.append("A test message\r");
        sb.trimToSize();
        try {
            byte[] tx_data = String.valueOf(sb).getBytes("UTF-8");

            // This line will send out the packet through a BLE serivce,
            // "mService" is the BLE service that I have initialize in the
            // MainActivity.
            mService.writeRXCharacteristic(tx_data);
        }
        catch (UnsupportedEncodingException e){
            Log.d(TAG, "Encode StringBuilder to byte[] get UnsupportedEncodingException");
        }

        // After sent out the packet, I need to check whether received
        // a valid response here. The receive and parse routine is 
        // implemented in the MainActivity, once the BLE service received a 
        // packet, it will parse it and set a flag to indicate a packet
        // is received.

        // And then other send/receive routines...

        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
}
公共类configTask扩展了AsyncTask{
@凌驾
受保护的整数doInBackground(字符串…消息){
//这里使用StringBuilder只是为了显示示例,
//我会在这里添加更多的字符串在实际情况中
最终StringBuilder sb=新StringBuilder(20);
sb.追加(“测试消息\r”);
使某人整洁;
试一试{
byte[]tx_data=String.valueOf(sb).getBytes(“UTF-8”);
//这条线路将通过一个BLE服务发送数据包,
//“mService”是我在
//主要活动。
mService.writeRXCharacteristic(发送单元数据);
}
捕获(不支持的编码异常e){
Log.d(标记,“将StringBuilder编码为字节[]获取不支持的DencodingException”);
}
//发送包后,我需要检查是否收到
//此处为有效响应。接收和分析例程为
//一旦BLE服务收到
//数据包,它将解析它并设置一个标志来指示一个数据包
//收到了。
//然后是其他发送/接收例程。。。
返回null;
}
@凌驾
受保护的void onProgressUpdate(整型…值){
super.onProgressUpdate(值);
}
@凌驾
受保护的void onPostExecute(整数结果){
super.onPostExecute(结果);
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
}

问题在某种程度上是相同的

您应该在doInBackground中实现取消机制,并在超时时调用AsyncTask.cancel()(
(java.lang.Runnable,long


在你的情况下,你考虑过服务吗?

< P>问题在某种程度上是相同的。

您应该在doInBackground中实现取消机制,并在超时时调用AsyncTask.cancel()(
(java.lang.Runnable,long


在你的情况下,你考虑过服务吗?

< P>你可以做如下的事情:
numBytes = 0;
for(int i=0; i<300; i++){
    numBytes += mService.read(bytes, .....) //Not sure about signature of your read method so just putting variables generally used.

    //TODO - copy your bytes to main buffer here

    if(numBytes == requiredBytes)
        break
    else
        Thread.Sleep(10);
}
numBytes=0;

对于(inti=0;i您可以执行以下操作

numBytes = 0;
for(int i=0; i<300; i++){
    numBytes += mService.read(bytes, .....) //Not sure about signature of your read method so just putting variables generally used.

    //TODO - copy your bytes to main buffer here

    if(numBytes == requiredBytes)
        break
    else
        Thread.Sleep(10);
}
numBytes=0;

对于(int i=0;如果对我来说,它看起来不太适合
AsyncTask
。更像是在它自己的线程上运行的绑定服务。我是Android新手。BLE通信是作为服务实现的。在我的MainActivity中,我实现了一个BroadcastReceiver,它可以从BLE服务接收消息,并对消息进行一次解析收到了。在这种情况下,启动另一个服务会更好吗?对我来说,
AsyncTask
,这看起来不是一个好用法。更像是在自己的线程上运行的绑定服务。我是Android新手。BLE通信是作为一个服务实现的。在我的MainActivity中,我实现了一个BroadcastReceiver,它可以获取从BLE服务接收消息,并在收到消息后对其进行解析。在这种情况下,启动另一个服务会更好吗?但我不想只实现超时,我需要检查在等待时间内是否接收到BLE消息。您的代码似乎只是在1ms后取消异步任务。我演示了如何进行im执行超时(看门狗),结果应该在
doInBackground()
中检查,当您在
onPostExecute()中收到do smth时检查。仍然考虑使用服务。但我不想仅仅实现超时,我需要检查等待时间是否收到BLE消息。您的代码似乎在1ms之后取消AssiCtask。我演示了如何实现超时(看门狗),结果应在 doIbBead()中检查。< />代码>当您在代码< > OpTestExcExter()/<代码>中接收到SMTH时,仍然考虑使用服务。