gpt4 book ai didi

android - 带有 Inputstream.read 的 Android 2.1 蓝牙 SPP 错误

转载 作者:行者123 更新时间:2023-11-29 00:49:52 24 4
gpt4 key购买 nike

我尝试连接蓝牙适配器,它在发送握手后向我发送一些数据。我的问题是,在适配器发送之前,代码工作正常,但如果它停止,in.read(xxx 命令 block 没有任何异常。我必须关闭适配器并等待几秒钟,然后发生异常并且代码恢复。如何解决这个问题? (为了更好地理解,我删除了错误处理)

BluetoothAdapter bt_adapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice bt_device = null;

//Bluetooth not supportet
if (bt_adapter == null) {
return false;
}

//enable Bluetooth
if (!bt_adapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
return false;
}

//search K01-Blue Adapter
Set<BluetoothDevice> pairedDevices = bt_adapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
String name = device.getName();
if (name.contains("K01-Blue")) {
bt_device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress());
break;
}
}
}

//Exit if no Adapter found
if(bt_device == null){ return false; }

//Create Socket
try {
UUID uuid =UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
GlobalVars.bluetooth.bt_socket =GlobalVars.bluetooth.bt_device.createRfcommSocketToServiceRecord(uuid);
} catch (Exception e) {
e.printStackTrace();
}

//Exit if socket not created
if(bt_socket == null){ return false; }

/Connect to Socket, otherwise exit
try {bt_socket.connect();
} catch (IOException e1) {return false;}

//get InputStream
InputStream in = null;
try {in = GlobalVars.bluetooth.bt_socket.getInputStream();
} catch (IOException e1) {return false;}

//get OutputStream
OutputStream out = null;
try {out = GlobalVars.bluetooth.bt_socket.getOutputStream()
} catch (IOException e1) {return false;}

//Say hello to K01 Adapter
byte[] hello = {0x2F, 0x3F, 0x21, 0x0D, 0x0A};

try {out.write(hello);
} catch (IOException e1) {return false;}

//Listen to Answer of K01 Adapter
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];

try {
nRead = in.read(data, 0, data.length);
while (nRead != -1) {
nRead = in.read(data, 0, data.length);
buffer.write(data, 0, nRead);
}
} catch (IOException e1) {e1.printStackTrace();}


//Create String from Bytearray
String str1 = new String(buffer.toByteArray());

最佳答案

这对我有用:

static ByteArrayOutputStream readbuffer_to()
{
ByteArrayOutputStream found = new ByteArrayOutputStream();
long start = System.currentTimeMillis();
try {
if(GlobalVars.bluetooth.in.available() == 0){return found;}
int nRead = GlobalVars.bluetooth.in.read();
boolean catched = false;
while (true) {
if(GlobalVars.bluetooth.in.available() > 0)
{
found.write(nRead);
nRead = GlobalVars.bluetooth.in.read();
start = System.currentTimeMillis();

}
if(System.currentTimeMillis() - start > 5000) {break;}

}


} catch (IOException e1) {


}
return found;
}

关于android - 带有 Inputstream.read 的 Android 2.1 蓝牙 SPP 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3761291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com