简体   繁体   English

从HC-06 BL Device在Android Studio上获取getInputStream()时格式错误

[英]Wrong format while i getInputStream() on Android Studio from HC-06 BL Device

I use this AsyncTask to get lot of string from the HC-06. 我使用此AsyncTask从HC-06中获取了很多字符串。 I correct receive them but sometimes the string appear on different lines on a ListView. 我正确地收到了它们,但有时该字符串出现在ListView的不同行上。

For example if i send on arduino Ide jghv i got j on first line and ghv on the second D/BLE_Activity: j D/BLE_Activity: ghv 例如,如果我在arduino Ide jghv上发送,则我在第一行得到j,在第二行得到ghv D / BLE_Activity:j D / BLE_Activity:ghv

Can someone please tell me what i'm doing wrong? 有人可以告诉我我在做什么错吗?

private class Stream extends AsyncTask<Void, Void, Void>  
{

    protected Void doInBackground(Void... devices)
    {
        try {
            while (btSocket.isConnected()) {

                char[] buffer = new char[256];
                int bytes;
                InputStream tmpIn = btSocket.getInputStream();
                InputStreamReader red = new InputStreamReader(tmpIn);
                bytes = red.read(buffer);
                String readMessage = new String(buffer, 0, bytes);

                Log.d(TAG, readMessage);

                Array.add(readMessage);

                if (isCancelled()) break;

            }
        } catch (IOException e) {

        }
        return null;
    }
}

This is how I create the ListView 这就是我创建ListView的方式

adapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_spinner_item, Array);



adapter.setDropDownViewResource(
                             android.R.layout.simple_spinner_dropdown_item);

sItems = (ListView) findViewById(R.id.Stream);
sItems.setAdapter(adapter);

And this is the declaration of Array and adapter 这是数组和适配器的声明

 List<String> Array = new ArrayList<String>();
 ArrayAdapter<String> adapter;

Thank to all of you 谢谢大家

Try to read text from inputstream line by line 尝试逐行从inputstream中读取文本

BufferedReader br = new BufferedReader(red);
        String line;
        while ((line = br.readLine()) != null) {
            Array.add(line);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 通过蓝牙从 HC-06 发布/接收数据 - Android Post/Receive data from HC-06 via Bluetooth 如何通过Blutooth HC-06在TextView上将Ping传感器数据获取到Android Studio - How to get the Ping sensor data to Android Studio on a TextView via Blutooth HC-06 HC-06蓝牙模块 - HC-06 bluetooth module 在处理草图中将树莓派的蓝牙重新连接到 HC-06 - Reconnect raspberry pi's bluetooth to an HC-06 in a processing sketch 当android设备在wifi时,SSLSocket在getInputStream处挂起 - SSLSocket hangs at getInputStream when android device is in wifi Android FileNotFound异常 - 无法从没有文件格式的图像URL获取getInputStream - Android FileNotFound Exception - Cannot getInputStream from image URL that does not have file format 通过 HC-05 蓝牙从 Arduino 传感器接收字符串数据到 Android Studio Textview - Receive string data from Arduino sensor to Android Studio Textview via HC-05 Bluetooth 当我从Android Studio启动它时,Android Studio应用程序可以运行,但从设备启动时却不能运行 - Android Studio app works when I launch it from Android Studio, but not when I launch from device 将应用程序安装到设备中时,Android Studio错误 - Android Studio error while installing app into a device 将文件从android设备传输到android studio - Transfer file from android device to android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM