Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Can';t从蓝牙医疗设备和安卓系统读取数据_Android_Sockets_Bluetooth_Inputstream - Fatal编程技术网

Android Can';t从蓝牙医疗设备和安卓系统读取数据

Android Can';t从蓝牙医疗设备和安卓系统读取数据,android,sockets,bluetooth,inputstream,Android,Sockets,Bluetooth,Inputstream,我想用脉搏血氧仪(SAT 300 BT)连接我的android应用程序,但当我调用inputStrem.read()时遇到了问题,该方法永远不会返回(我知道这是一个阻塞读取),我想这是因为医疗设备从未给我输入流 我读到一些设备在向客户端发送消息之前需要接收输出消息,如果是这种情况,我不知道要发送什么消息 这是我的代码: public class BluetoothActivity extends BaseActivity { private static final String TAG =

我想用脉搏血氧仪(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));
    }
}
公共类BluetoothActivity扩展了BaseActivity{
私有静态最终字符串TAG=“BluetoothActivity”;
私有静态最终布尔值D=true;
私人蓝牙适配器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bluetoothAdapter=bluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter.isEnabled()){
Set devicesavable=bluetoothAdapter.getBondedDevices();
如果(devicesAvailable.size()>0){
用于(蓝牙设备:设备可用){
Log.d(标记,device.getName()+“”+device.getAddress()+“\n”);
}
}
如果(!devicesAvailable.isEmpty()){
askUserToPick(设备可用);
}
}
}
专用void askUserToPick(设置设备可用){
for(Iterator Iterator=devicesAvailable.Iterator();Iterator.hasNext();){
BluetoothDevice BluetoothDevice=(BluetoothDevice)迭代器.next();
if(bluetoothDevice.getAddress().equalsIgnoreCase(“00:0C:B6:02:38:80”)){
试一试{
openSocket(蓝牙设备);
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
}
私有void openSocket(BluetoothDevice BluetoothDevice)引发IOException{
最终ConnectRunnable连接器=新的ConnectRunnable(蓝牙设备);
connector.start();
}
私有类ConnectRunnable扩展线程{
私有输入流输入流;
私人蓝牙插座;
专用最终蓝牙设备蓝牙设备;
公共连接可运行(蓝牙设备蓝牙设备){
this.bluetoothDevice=bluetoothDevice;
}
公开募捐{
试一试{
UUID UUID=bluetoothDevice.getUuid()[0].getUuid();
BluetoothSocket=bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
socket.connect();
日志d(标记“已连接”);
试一试{
InputStream InputStream=socket.getInputStream();
readInputStream(inputStream);
inputStream.close();
socket.close();
}捕获(例外e){
Log.d(标签,
“connect():将i/o流附加到套接字时出错。msg=”
+e.getMessage());
}
}捕获(安全异常e){
Log.e(标签“SecEx”,e);
}捕获(IllegalArgumentException e){
Log.e(标签“IArgEx”,e);
}捕获(IOE异常){
Log.e(标签“IOEx”,e);
}
}
私有void readInputStream(InputStream InputStream)引发IOException{
StringBuilder=新的StringBuilder();
字节[]缓冲区=新字节[300];
int bytes=inputStream.read(缓冲区);
append(新字符串(缓冲区,0,字节));
}
}

}

更新:我阅读了设备通信协议,并添加了使用outputStream.write(消息)请求发送实时数据的代码;现在设备返回一些数据给我。。但是当我读取输入流时,我会得到奇怪的字符(我使用utf-8,比如:String message=new String(buffer,0,bytesRead,“utf-8”);它是什么品牌的脉搏血氧仪?你能发送产品链接吗?