无法从蓝牙医疗设备和Android读取数据

时间:2014-06-21 18:59:49

标签: android sockets bluetooth inputstream

我想用脉搏血氧仪(SAT 300 BT)连接我的Android应用程序,但是当我调用inputStrem.read()时遇到问题,方法永远不会返回(我知道这是阻塞读取),我想想因为医疗设备从来没有给我输入流。

我已经读过某些设备在向客户端发送消息之前需要接收输出消息,如果是这种情况,我不知道要发送什么消息。

这是我的代码:

public class BluetoothActivity extends BaseActivity {

private static final String TAG = "BluetoothActivity";
private static final boolean D = true;
private BluetoothAdapter bluetoothAdapter;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    if (bluetoothAdapter.isEnabled()) {
        Set<BluetoothDevice> devicesAvailable = bluetoothAdapter.getBondedDevices();

        if (devicesAvailable.size() > 0) {
            for (BluetoothDevice device : devicesAvailable) {
                Log.d(TAG, device.getName() + " " + device.getAddress() + "\n");
            }
        }
        if (!devicesAvailable.isEmpty()) {
            askUserToPick(devicesAvailable);
        }
    }

}

private void askUserToPick(Set<BluetoothDevice> devicesAvailable) {

    for (Iterator iterator = devicesAvailable.iterator(); iterator.hasNext();) {
        BluetoothDevice bluetoothDevice = (BluetoothDevice) iterator.next();
        if (bluetoothDevice.getAddress().equalsIgnoreCase("00:0C:B6:02:38:80")) {
            try {
                openSocket(bluetoothDevice);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

private void openSocket(BluetoothDevice bluetoothDevice) throws IOException {
    final ConnectRunnable connector = new ConnectRunnable(bluetoothDevice);
    connector.start();
}

private class ConnectRunnable extends Thread {

    private InputStream inputStream;
    private BluetoothSocket socket;
    private final BluetoothDevice bluetoothDevice;

    public ConnectRunnable(BluetoothDevice bluetoothDevice) {
        this.bluetoothDevice = bluetoothDevice;
    }

    public void run() {
        try {
            UUID uuid = bluetoothDevice.getUuids()[0].getUuid();
            BluetoothSocket socket = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);

            socket.connect();
            Log.d(TAG, "connected");

            try {
                InputStream inputStream = socket.getInputStream();
                readInputStream(inputStream);

                inputStream.close();
                socket.close();
            } catch (Exception e) {
                Log.d(TAG,
                        "connect(): Error attaching i/o streams to socket. msg="
                                + e.getMessage());
            }

        } catch (SecurityException e) {
            Log.e(TAG, "SecEx", e);
        } catch (IllegalArgumentException e) {
            Log.e(TAG, "IArgEx", e);
        } catch (IOException e) {
            Log.e(TAG, "IOEx", e);
        }

    }

    private void readInputStream(InputStream inputStream) throws IOException {
        StringBuilder builder = new StringBuilder();

        byte[] buffer = new byte[300];
        int bytes = inputStream.read(buffer);

        builder.append(new String(buffer, 0, bytes));
    }
}

}

0 个答案:

没有答案