从Android电子配对的蓝牙设备读取数据 [英] Read data from paired Bluetooth devices in Android

查看:405
本文介绍了从Android电子配对的蓝牙设备读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的是Android 4.0的应用程序,从配对蓝牙设备读取数据。我能搜索发现的蓝牙设备与设备配对。不过,我不确定如何通过串口读取蓝牙设备的数据?是否android系统支持串行端口?因为我无法找到像COM1或COM2串口在Android系统。目前,我使用的BluetoothSocket配对的设备。但是,有没有办法像Windows那样从蓝牙串口读取数据?

I am working on an android 4.0 application which reads data from the paired bluetooth device. 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? Because I cannot find the serial port like COM1 or COM2 in the android system. At the moment, I am using the BluetoothSocket to pair the device. However, is there any way to read data from Bluetooth serial port like Windows does?

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

任何帮助AP preciated!谢谢adance。

Any helps appreciated! Thanks in adance.

问候,

查尔斯

推荐答案

是的,有。在插座您收到可以给你一个的InputStream 实例。如果插座连接,您就可以读取数据(字符字符串字节,这取决于读者,你会环绕你的的InputStream ,如果你换一个)。

Yes there is. The socket you receive can give you an InputStream instance. 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).

要打开串口与设备,则必须使用串行端口配置文件在您用来创建套接字的UU​​ID。抛来抛去网络上的一个简单的UUID是

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. A simple UUID thrown around on the web is

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 );
}

然后,在某个地方的code可以从阅读器读取:

And then, somewhere in your code you can read from the reader:

String aString = mBufferedReader.readLine();

你还可以使用的OutputStream 和各种作家做同样的事情在相反的方向。

And you can do the same thing in opposite direction using OutputStream and various writers.

这篇关于从Android电子配对的蓝牙设备读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆