3

问题。 我的 Android 应用程序中通过蓝牙 2 SPP 连接的数据流在大约 30 秒后挂起

语境。 我正在尝试将运行 Android 4.3(第一代 nexus 7)作为客户端的 android 平板电脑连接到由 Atmega 1284P 微控制器控制的蓝牙 2 RN-41 模块。微控制器只是通过 USART 线将数据推送到蓝牙模块,我已经成功连接到模块并在我的 android 应用程序中流式传输一些数据。但是,我注意到大约三十秒后数据流停止(即使连接似乎仍然存在)。这让我发疯,我不太确定出了什么问题。我是一名初级 Android 开发人员。感谢大家的帮助!

安卓代码。我去掉了 else 和一些 catch 来缩短代码量,并试图将代码分解为逻辑的、注释的部分。我是新手,所以如果我的代码很愚蠢,我深表歉意!

private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
private InputStream inStream = null;
private BluetoothDevice pairedScale = null;
private UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); // found this at https://stackoverflow.com/questions/4632524/how-to-find-the-uuid-of-serial-port-bluetooth-device
private String MAC_ADDR;
private boolean isBTConnected = false;
...


public void connectToBT() {

/* * *
 * Find Bluetooth device from list of paired devices
 * * */

        if (mBluetoothAdapter.isEnabled()) {
            Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
            MAC_ADDR = macSpinner.getItemAtPosition(macSpinner.getSelectedItemPosition()).toString();
            for (BluetoothDevice device : pairedDevices) {
                Log.v("HP", "Iterating through paired BT devices for a match");
                if (MAC_ADDR.equals(device.toString())) {
                    pairedScale = device;
                    Toast.makeText(MainActivity.this, device.toString(), Toast.LENGTH_LONG).show();
                    Log.v("HP", "Found a BT device match: " + device.toString());
                    changeDisplayText("\nUsing paired device: " + device.toString(), true);
                    break;
                }
            }
        }


/* * *
 * Spawn new thread and attempt to connect
 * * */

        // If socket created successfully, spawn new thread
        if (btSocket != null) {
            new Thread(new Runnable() {
                public void run() {
                    isConnecting = true;
                    // try to connect
                    try {
                        btSocket.connect();
                    } catch (IOException e) {
                        e.printStackTrace();
                        Log.e("HP", "Couldn't connect socket");
                    }

/* * *
 * Create IO Streams
 * * */

                    // create IO stream
                    if (btSocket.isConnected()) {
                        Log.v("HP", "Congratulations! Socket connected");
                        isBTConnected = true;
                        try {
                            inStream = btSocket.getInputStream();
                            outStream = btSocket.getOutputStream();
                        } catch (IOException e) {
                            e.printStackTrace();
                            Log.e("HP", "Couldn't create io stream");
                        }
                    } else {
                        Log.e("HP", "Couldn't connect socket");
                    }

                    // we have our io streams, now get data
                    if (isBTConnected && inStream != null) {
                        Log.v("HP", "Attempting to get data from inStream");
                        runOnUiThread(new Runnable() {
                            public void run() {
                                changeDisplayText("\nConnection successful!" +
                                        "\nStreaming Data...", true);
                            }
                        });
                        Log.v("HP", "Starting data stream...");
                        // read from the input stream
                        byte[] scaleData = new byte[512];
                        byte[] allScaleData;
                        String hpFileName = "streamedData_" + epoch.toString() + ".dat";
                        FileOutputStream fOutStream = null;

/* * *
 * Create file to store data
 * * */

                        // external storage
                        File myDir = new File(Environment.getExternalStorageDirectory().toString() + "/streamedData");
                        myDir.mkdirs();
                        File file = new File(myDir, hpFileName);
                        Log.v("HP", "Attempted to make file");
                        try {
                            fOutStream = new FileOutputStream(file);
                            Log.v("HP", "Made file");
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                            Log.e("HP", "Error: Couldn't make file");
                        }


/* * *
 * Stream data here
 * * */

                        Integer bytesRead;
                        while (true) {
                            try {
                                bytesRead = inStream.read(scaleData);
                                Log.d("HP", "Read " + bytesRead.toString() + " bytes");
                                fOutStream.write(Arrays.copyOfRange(scaleData, 0, bytesRead.intValue()));
                            } catch (IOException e) {
                                Log.d("HP", "Done reading bytes");
                                try {
                                    btSocket.close();
                                } catch (IOException e1) {
                                    e1.printStackTrace();
                                }
                                break;
                            }
                        }

                        try {
                            fOutStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    clearBTConnection();
                    isConnecting = false;
                }
            }).start();
        }
    }
}

研究。我读了几篇没有运气的文章。例如: Android 蓝牙 SPP 连接似乎在几秒钟后就死机了

4

0 回答 0