Android 在循环中向OutputStream写入字节时,无法重新打开流
当我将Android 在循环中向OutputStream写入字节时,无法重新打开流,android,android-bluetooth,dataoutputstream,bluecove,Android,Android Bluetooth,Dataoutputstream,Bluecove,当我将inLoop()方法放入while循环中时,服务器将永远不会运行 它会终止,但我希望服务器不断地一次又一次地向客户端发送字节 我该怎么做 SimpleSpServer- public class SimpleSPPServer { //start server private void startServer() throws IOException{ //Create a UUID for SPP UUID uuid = new UUI
inLoop()
方法放入while循环中时,服务器将永远不会运行
它会终止,但我希望服务器不断地一次又一次地向客户端发送字节
我该怎么做
SimpleSpServer-
public class SimpleSPPServer {
//start server
private void startServer() throws IOException{
//Create a UUID for SPP
UUID uuid = new UUID("1101", true);
//Create the servicve url
String connectionString = "btspp://localhost:" + uuid +";name=Sample SPP Server";
//open server url
StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString );
//Wait for client connection
System.out.println("\nServer Started. Waiting for clients to connect...");
StreamConnection connection=streamConnNotifier.acceptAndOpen();
while(true)
{
inLoop(connection);
}
}
public void inLoop(StreamConnection connection) throws IOException
{
RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
System.out.println("Remote device address: "+dev.getBluetoothAddress());
System.out.println("Remote device name: "+dev.getFriendlyName(true));
//send response to spp client
OutputStream outStream=connection.openDataOutputStream();
DataOutputStream mmOutStream = new DataOutputStream(outStream);
byte[] bb1 = new byte[] {
(byte)0x7D, (byte)0x43, (byte)0x92, (byte)0x05, (byte)0x0E,
(byte)0x2E, (byte)0x0A };
System.out.println("Writing Bytes...");
try
{
//mmOutStream.writeByte(bb[k]);
mmOutStream.write(bb1, 0, bb1.length);
Thread.sleep(5000);
//mmOutStream.write(bb2, 0, bb2.length);
}
catch (IOException e)
{
e.printStackTrace();
}
catch (NullPointerException e)
{
} catch (Exception e) {
e.printStackTrace();
}
PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(mmOutStream));
try {
mmOutStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
pWriter.flush();
pWriter.close();
}
public static void main(String[] args) throws IOException {
//display local device address and name
LocalDevice localDevice = LocalDevice.getLocalDevice();
System.out.println("Address: "+localDevice.getBluetoothAddress());
System.out.println("Name: "+localDevice.getFriendlyName());
SimpleSPPServer sampleSPPServer=new SimpleSPPServer();
sampleSPPServer.startServer();
}
Exception in thread "main" java.io.IOException: Stream cannot be reopened
at com.intel.bluetooth.BluetoothRFCommConnection.openOutputStream(BluetoothRFCommConnection.java:190)
at com.intel.bluetooth.BluetoothRFCommConnection.openDataOutputStream(BluetoothRFCommConnection.java:213)
at SimpleSPPServer.inLoop(SimpleSPPServer.java:52)
at SimpleSPPServer.startServer(SimpleSPPServer.java:41)
at SimpleSPPServer.main(SimpleSPPServer.java:104)
例外情况-
public class SimpleSPPServer {
//start server
private void startServer() throws IOException{
//Create a UUID for SPP
UUID uuid = new UUID("1101", true);
//Create the servicve url
String connectionString = "btspp://localhost:" + uuid +";name=Sample SPP Server";
//open server url
StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString );
//Wait for client connection
System.out.println("\nServer Started. Waiting for clients to connect...");
StreamConnection connection=streamConnNotifier.acceptAndOpen();
while(true)
{
inLoop(connection);
}
}
public void inLoop(StreamConnection connection) throws IOException
{
RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
System.out.println("Remote device address: "+dev.getBluetoothAddress());
System.out.println("Remote device name: "+dev.getFriendlyName(true));
//send response to spp client
OutputStream outStream=connection.openDataOutputStream();
DataOutputStream mmOutStream = new DataOutputStream(outStream);
byte[] bb1 = new byte[] {
(byte)0x7D, (byte)0x43, (byte)0x92, (byte)0x05, (byte)0x0E,
(byte)0x2E, (byte)0x0A };
System.out.println("Writing Bytes...");
try
{
//mmOutStream.writeByte(bb[k]);
mmOutStream.write(bb1, 0, bb1.length);
Thread.sleep(5000);
//mmOutStream.write(bb2, 0, bb2.length);
}
catch (IOException e)
{
e.printStackTrace();
}
catch (NullPointerException e)
{
} catch (Exception e) {
e.printStackTrace();
}
PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(mmOutStream));
try {
mmOutStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
pWriter.flush();
pWriter.close();
}
public static void main(String[] args) throws IOException {
//display local device address and name
LocalDevice localDevice = LocalDevice.getLocalDevice();
System.out.println("Address: "+localDevice.getBluetoothAddress());
System.out.println("Name: "+localDevice.getFriendlyName());
SimpleSPPServer sampleSPPServer=new SimpleSPPServer();
sampleSPPServer.startServer();
}
Exception in thread "main" java.io.IOException: Stream cannot be reopened
at com.intel.bluetooth.BluetoothRFCommConnection.openOutputStream(BluetoothRFCommConnection.java:190)
at com.intel.bluetooth.BluetoothRFCommConnection.openDataOutputStream(BluetoothRFCommConnection.java:213)
at SimpleSPPServer.inLoop(SimpleSPPServer.java:52)
at SimpleSPPServer.startServer(SimpleSPPServer.java:41)
at SimpleSPPServer.main(SimpleSPPServer.java:104)
从
doLoop()
中打开和关闭流。在使用while开始循环之前打开流。永远不要关闭小溪。或者当循环完成时。如果流无法重新打开,那么为什么要这样做?@greenapps-如果流无法重新打开,那么为什么要这样做?
-我希望在每次迭代中处理相同的字节。所以我就是这样做的。请告诉我们如何在循环中连续处理字节而不会出现重新打开错误。为什么要多次处理相同的字节?@greenapps-数据库中会及时创建记录。所以处理相同的字节,当时间达到某个阈值时,就会创建记录。这只是一个想法。实时逻辑更为复杂。