如何阅读连续连接的蓝牙设备?

时间:2015-02-11 06:11:02

标签: java android bluetooth

我正在尝试将我的应用程序连接到蓝牙设备,之后我正在执行读取和写入功能。我能够对设备进行连接和写命令。但我无法进行阅读功能。

"阅读"功能就像,只要设备套接字连接到应用程序,就应该连续发送设备信息。但我的情况是,read()方法正在调用,但每次我在InputStream中得到长度为0.

下面是我正在编写的代码。请检查我哪里出错了,请帮助我。

public class ConnectionThread implements Runnable {

    private static final String CLASSTAG = ConnectionThread.class.getSimpleName();
    public static BluetoothSocket mSocket;

    private InputStream inStream;
    private OutputStream outStream;
    private boolean canceled = false;
    private Context mContext;

    public ConnectionThread(Context context, BluetoothDevice device) {
        this.mContext = context;
        try {
            mSocket = device.createRfcommSocketToServiceRecord(UUID.fromString(Constants.mUUID));
        } catch (IOException e) {
            Log.e(CLASSTAG, "createRfcommSocketToServiceRecord", e);
        }
    }

    @SuppressLint("NewApi")
    @Override
    public void run() {

        // Cancel discovery because it will slow down the connection
        if(BluetoothAdapter.getDefaultAdapter().isDiscovering()) {
            BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
        }

        try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            mSocket.connect();
        } catch (IOException e) {
            Log.e(CLASSTAG, "connect failed", e);
            // Unable to connect; close the socket and get out
            disconnect();
            return;
        }

        // Get the input and output streams, using temp objects because
        // member streams are final
        try {
            inStream = mSocket.getInputStream();
            outStream = mSocket.getOutputStream();
        } catch (IOException e) {
            Log.e(CLASSTAG, "IO streams init failed", e);
            disconnect();
            return;
        }

        while (!canceled) {
            read();
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                Log.e(CLASSTAG, "sleep failed", e);
            }
        }
    }

    @SuppressLint("NewApi")
    public void applyCommand(String configCommand) throws IOException {
        byte[] bytes = null;
        bytes = new BigInteger(configCommand,16).toByteArray();
        if (mSocket.isConnected()) {
            //write(bytes);
            outStream.write(bytes);
            Log.d(CLASSTAG, "Command apply: " + bytes + " (" + configCommand + ")");
        }
        Toast.makeText(mContext, mContext.getString(R.string.cmd_updated), Toast.LENGTH_LONG).show();
    }

    private void read() {

        byte[] buffer = new byte[128];

        // Keep listening to the InputStream while connected
        try {
            // Read from the InputStream
            if (inStream.available() > 0) {
                int bytes = inStream.read(buffer);
                if (bytes > 0) {
                    Log.d(CLASSTAG, "Response: " + new String(buffer, 0, bytes));
                }
            }
            // Send the obtained bytes to the UI Activity
            // mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
        } catch (IOException e) {
            Log.e(CLASSTAG, "reading from input stream failed", e);
            disconnect();
        }
    }

    public void disconnect() {
        try {
            if (outStream != null) {
                outStream.close();
            }
            if (inStream != null) {
                inStream.close();
            }
            mSocket.close();
            canceled = true;
        } catch (IOException e) {
            Log.e(CLASSTAG, "close socket", e);
        }
    }
}

0 个答案:

没有答案