Android应用程序在30秒后丢弃蓝牙2 SPP数据流

时间:2014-05-28 20:10:58

标签: java android bluetooth android-bluetooth spp

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

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

Android代码。我拿出了其他内容和一些捕获来缩短代码量并试图分解逻辑评论部分中的代码。我对此很陌生,所以如果我的代码很傻,我会道歉!

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 Bluetooth SPP connection to seems to go dead after a couple of seconds

0 个答案:

没有答案