【问题标题】:Android: Bluetooth - How to read incoming dataAndroid:蓝牙 - 如何读取传入数据
【发布时间】:2017-06-16 15:36:23
【问题描述】:

我已成功配对并连接到蓝牙设备。我现在有兴趣接收在 2 之间传输的所有数据并看看是什么。

我正在从套接字获取输入流并尝试读取它。我将其退回并记录下来。

从我所读到的内容中,我知道这样做的唯一方法就是使用字节缓冲区读取以返回一个 int。但是我应该有大量的数据通过。我怎样才能不断地读出正在传输的数据,并将其格式化为字节而不是整数。

谢谢。

完整代码如下:

public class ConnectThread {

    private BluetoothSocketWrapper bluetoothSocket;
    private BluetoothDevice device;
    private boolean secure;
    private BluetoothAdapter adapter;
    private List<UUID> uuidCandidates;
    private int candidate;


    /**
     * @param device the device
     * @param secure if connection should be done via a secure socket
     * @param adapter the Android BT adapter
     * @param uuidCandidates a list of UUIDs. if null or empty, the Serial PP id is used
     */
    public ConnectThread(BluetoothDevice device, boolean secure, BluetoothAdapter adapter,
                              List<UUID> uuidCandidates) {
        this.device = device;
        this.secure = secure;
        this.adapter = adapter;
        this.uuidCandidates = uuidCandidates;

        if (this.uuidCandidates == null || this.uuidCandidates.isEmpty()) {
            this.uuidCandidates = new ArrayList<UUID>();
            this.uuidCandidates.add(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
        }
    }

    public BluetoothSocketWrapper connect() throws IOException {
        boolean success = false;
        while (selectSocket()) {
            adapter.cancelDiscovery();

            try {
                bluetoothSocket.connect();
                success = true;
                break;
            } catch (IOException e) {
                //try the fallback
                try {
                    bluetoothSocket = new FallbackBluetoothSocket(bluetoothSocket.getUnderlyingSocket());
                    Thread.sleep(500);
                    bluetoothSocket.connect();
                    success = true;
                    break;
                } catch (FallbackException e1) {
                    Log.w("BT", "Could not initialize FallbackBluetoothSocket classes.", e);
                } catch (InterruptedException e1) {
                    Log.w("BT", e1.getMessage(), e1);
                } catch (IOException e1) {
                    Log.w("BT", "Fallback failed. Cancelling.", e1);
                }
            }
        }

        if (!success) {
            throw new IOException("Could not connect to device: "+ device.getAddress());
        }

        receiveData(bluetoothSocket);
        return bluetoothSocket;
    }

    private boolean selectSocket() throws IOException {
        if (candidate >= uuidCandidates.size()) {
            return false;
        }

        BluetoothSocket tmp;
        UUID uuid = uuidCandidates.get(candidate++);

        Log.i("BT", "Attempting to connect to Protocol: "+ uuid);
        if (secure) {
            tmp = device.createRfcommSocketToServiceRecord(uuid);
        } else {
            tmp = device.createInsecureRfcommSocketToServiceRecord(uuid);
        }
        bluetoothSocket = new NativeBluetoothSocket(tmp);

        return true;
    }

    public static interface BluetoothSocketWrapper {

        InputStream getInputStream() throws IOException;

        OutputStream getOutputStream() throws IOException;

        String getRemoteDeviceName();

        void connect() throws IOException;

        String getRemoteDeviceAddress();

        void close() throws IOException;

        BluetoothSocket getUnderlyingSocket();

    }


    public static class NativeBluetoothSocket implements BluetoothSocketWrapper {

        private BluetoothSocket socket;

        public NativeBluetoothSocket(BluetoothSocket tmp) {
            this.socket = tmp;
        }

        @Override
        public InputStream getInputStream() throws IOException {
            return socket.getInputStream();
        }

        @Override
        public OutputStream getOutputStream() throws IOException {
            return socket.getOutputStream();
        }

        @Override
        public String getRemoteDeviceName() {
            return socket.getRemoteDevice().getName();
        }

        @Override
        public void connect() throws IOException {
            socket.connect();
        }

        @Override
        public String getRemoteDeviceAddress() {
            return socket.getRemoteDevice().getAddress();
        }

        @Override
        public void close() throws IOException {
            socket.close();
        }

        @Override
        public BluetoothSocket getUnderlyingSocket() {
            return socket;
        }

    }

    public class FallbackBluetoothSocket extends NativeBluetoothSocket {

        private BluetoothSocket fallbackSocket;

        public FallbackBluetoothSocket(BluetoothSocket tmp) throws FallbackException {
            super(tmp);
            try
            {
                Class<?> clazz = tmp.getRemoteDevice().getClass();
                Class<?>[] paramTypes = new Class<?>[] {Integer.TYPE};
                Method m = clazz.getMethod("createRfcommSocket", paramTypes);
                Object[] params = new Object[] {Integer.valueOf(1)};
                fallbackSocket = (BluetoothSocket) m.invoke(tmp.getRemoteDevice(), params);
            }
            catch (Exception e)
            {
                throw new FallbackException(e);
            }
        }

        @Override
        public InputStream getInputStream() throws IOException {
            return fallbackSocket.getInputStream();
        }

        @Override
        public OutputStream getOutputStream() throws IOException {
            return fallbackSocket.getOutputStream();
        }


        @Override
        public void connect() throws IOException {
            fallbackSocket.connect();
        }


        @Override
        public void close() throws IOException {
            fallbackSocket.close();
        }

    }

    public static class FallbackException extends Exception {

        /**
         *
         */
        private static final long serialVersionUID = 1L;

        public FallbackException(Exception e) {
            super(e);
        }

    }

    public void sendData(BluetoothSocketWrapper socket, int data) throws IOException{
        ByteArrayOutputStream output = new ByteArrayOutputStream(4);
        output.write(data);
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(output.toByteArray());
    }

    public int receiveData(BluetoothSocketWrapper socket) throws IOException{
        byte[] buffer = new byte[256];
        ByteArrayInputStream input = new ByteArrayInputStream(buffer);
        InputStream inputStream = socket.getInputStream();
        inputStream.read(buffer);
        return input.read();
    }
}

【问题讨论】:

标签: java android multithreading bluetooth stream


【解决方案1】:

首先,停止使用ByteArrayInputStreamByteArrayOutputStream 以获得更多控制权。

如果套接字发送/接收文本,请执行此操作。

发送

String text = "My message";
socketOutputStream.write(text.getBytes());

接收

int length = socketInputStream.read(buffer);
String text = new String(buffer, 0, length);

socketOutputStream 应该是您的bluetoothSocket.getOutputStream()

如果套接字发送/接收大量数据,关键是 while 循环以防止内存不足异常。数据将按块读取(例如每 4KB 的缓冲区大小),当您选择缓冲区大小时,请考虑堆大小,如果您是实时流媒体,还要考虑延迟和质量。

发送:

int length;
while ((length = largeDataInputStream.read(buffer)) != -1) {
    socketOutputStream.write(buffer, 0, length);
}

接收:

int length;
//socketInputStream never returns -1 unless connection is broken
while ((length = socketInputStream.read(buffer)) != -1) {
    largeDataOutputStream.write(buffer, 0, length);
    if (progress >= dataSize) {
        break; //Break loop if progress reaches the limit
    }
}

常见问题解答:

  • 如何获取接收数据的大小?您必须自己实现通知远程设备准备接收数据(包括文件大小),这将需要至少一个双套接字连接(2 个套接字,1 个设备),例如,1 个用于文本的套接字片段和自定义命令,以及 1 个用于大数据(如文件或流)的套接字。
  • 什么是largeDataInputStreamlargeDataOutputStream?这些流可以是普通的 I/O 流,FileInputStream/FileOutputStream 等。
  • 为什么BluetoothSocket 的while 循环永远不会结束?套接字输入不断接收数据,read() 方法会自行阻塞,直到检测到数据。为防止阻塞该行中的代码,必须中断 while 循环。

注意:此答案可能需要修改。我不是以英语为母语的人。

【讨论】:

  • 这应该是公认的答案,因为没有它该代码块将毫无意义。
【解决方案2】:

按照上述建议,我现在正在使用此代码检索数据。

    public void receiveData(BluetoothSocketWrapper socket) throws IOException{
    InputStream socketInputStream =  socket.getInputStream();
    byte[] buffer = new byte[256];
    int bytes;

    // Keep looping to listen for received messages
    while (true) {
        try {
            bytes = socketInputStream.read(buffer);            //read bytes from input buffer
            String readMessage = new String(buffer, 0, bytes);
            // Send the obtained bytes to the UI Activity via handler
            Log.i("logging", readMessage + "");
        } catch (IOException e) {
            break;
        }
    }

}

【讨论】:

    猜你喜欢
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    相关资源
    最近更新 更多