将蓝牙腕带按钮连接到Android应用程序

时间:2015-03-06 14:21:12

标签: android bluetooth uuid

我正在尝试将我的应用程序连接到蓝牙设备。该设备是一个腕带,带有来自另一家公司的按钮(它为我提供了UUID)以连接到他们的设备。我们希望让腕带上的按钮在应用程序中启动一个动作。

然而,当我尝试将应用程序连接到我正在获取的设备时

java.io.IOException: read failed, socket might closed or timeout, read ret: -1

如果我尝试读取数据,我会得到一个无限循环的

java.io.IOException: bt socket closed, read return: -1

我的活动中的代码如下

void openConnection() throws IOException
{
    UUID uuid = UUID.fromString("00002A19-0000-1000-8000-00805F9B34FB"); //Standard SerialPortService ID
    mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
    mmSocket.connect();
}


void readData() throws IOException
{
    ct = new ConnectedThread(mmSocket);
    ct.start();
}

线程包括:

public class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket) {
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the input and output streams, using temp objects because
        // member streams are final
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) { }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        byte[] buffer = new byte[1024];  // buffer store for the stream
        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);
                // Send the obtained bytes to the UI activity
                //mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                //        .sendToTarget();
                Log.e("test", "recevied");
            } catch (IOException e) {
                Log.e("test", e.toString());
            }
        }
    }

    /* Call this from the main activity to send data to the remote device */
    public void write(byte[] bytes) {
        try {
            mmOutStream.write(bytes);
        } catch (IOException e) { }
    }

    /* Call this from the main activity to shutdown the connection */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) { }
    }
}

我从制造商处获得的UUID如下。不确定这些是否真的对读取设备上的按钮点击有用。我尝试替换UUID字符串以匹配这些UUID(例如0000FFD0-0000-1000-8000-00805F9B34FB),但没有运气。

Battery service UUID??0x180F
Eigenvalue UUID??0x2A19

Motor Service UUID??0xFFD0
Motor strength eigenvalue UUID??0xFFD1
High 0x03??Mid 0x02??low 0x01??

我将不胜感激任何帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

RFCOMM / SPP串行配置文件的标准GUID / UUID为{00001101-0000-1000-8000-00805F9B34FB} ... 无论如何,有一种方式知道'关于您的设备公开的服务将是: 1)查询附近的设备 2)在列表中找到您的腕带(按名称或MacAddress) 3)然后在设备上执行发现,以枚举所有可用服务(SDP)。 然后,您将确切地知道他们是谁,并且您将能够使用“已发现的”信息与他们建立联系。而不是硬编码' GUID。 只有我的建议......;)