简体   繁体   English

从Android中的配对蓝牙设备读取数据

[英]Read data from paired Bluetooth devices in Android

I am working on an android 4.0 application which reads data from the paired bluetooth device. 我正在研究一个从配对的蓝牙设备读取数据的android 4.0应用程序。 I am able to search discoverable bluetooth device and pair the devices. 我能够搜索可发现的蓝牙设备并配对设备。 However, I am unsure of how to read data from Bluetooth device via serial port? 但是,我不确定如何通过串口从蓝牙设备读取数据? Does android system support SERIAL PORT? android系统是否支持SERIAL PORT? Because I cannot find the serial port like COM1 or COM2 in the android system. 因为我在android系统中找不到像COM1或COM2这样的串口。 At the moment, I am using the BluetoothSocket to pair the device. 目前,我正在使用BluetoothSocket配对设备。 However, is there any way to read data from Bluetooth serial port like Windows does? 但是,有没有办法从像Windows这样的蓝牙串口读取数据呢?

socket = _devices.get(k).createRfcommSocketToServiceRecord(MY_UUID_SECURE);
socket.connect();

Any helps appreciated! 任何帮助赞赏! Thanks in adance. 谢谢你的支持。

Regards, 问候,

Charles 查尔斯

Yes there is. 就在这里。 The socket you receive can give you an InputStream instance. 您收到的socket可以为您提供一个InputStream实例。 If the socket is connected, you can then read data ( char , String or byte , depending on what reader you will wrap around your InputStream , if you wrap one). 如果socket已连接,则可以读取数据( charStringbyte ,具体取决于您将在InputStream包装的读取器,如果您将其换行)。

To open a serial port with the device, you have to use the Serial Port Profile in the UUID you use to create your socket. 要使用设备打开串行端口 ,必须使用用于创建套接字的UUID中的串行端口配置文件。 A simple UUID thrown around on the web is 在网络上抛出一个简单的UUID是

00001101-0000-1000-8000-00805F9B34FB

You can then create your socket, connect to it, get streams and read/write bytes with them. 然后,您可以创建套接字,连接到它,获取流以及使用它们读/写字节。 Example : 示例:

private static final String UUID_SERIAL_PORT_PROFILE 
                       = "00001101-0000-1000-8000-00805F9B34FB";

private BluetoothSocket mSocket = null;
private BufferedReader mBufferedReader = null;

private void openDeviceConnection(BluetoothDevice aDevice)
        throws IOException {
    InputStream aStream = null;
    InputStreamReader aReader = null;
    try {
        mSocket = aDevice
                .createRfcommSocketToServiceRecord( getSerialPortUUID() );
        mSocket.connect();
        aStream = mSocket.getInputStream();
        aReader = new InputStreamReader( aStream );
        mBufferedReader = new BufferedReader( aReader );
    } catch ( IOException e ) {
        Log.e( TAG, "Could not connect to device", e );
        close( mBufferedReader );
        close( aReader );
        close( aStream );
        close( mSocket );
        throw e;
    }
}

private void close(Closeable aConnectedObject) {
    if ( aConnectedObject == null ) return;
    try {
        aConnectedObject.close();
    } catch ( IOException e ) {
    }
    aConnectedObject = null;
}

private UUID getSerialPortUUID() {
    return UUID.fromString( UUID_SERIAL_PORT_PROFILE );
}

And then, somewhere in your code you can read from the reader: 然后,在代码中的某处,您可以从阅读器中读取:

String aString = mBufferedReader.readLine();

And you can do the same thing in opposite direction using OutputStream and various writers. 你可以使用OutputStream和各种编写器在相反的方向做同样的事情。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM