How to close color Sensor properly? I want to get data from the color sensor with Bluetooth. I can get data but when the program terminates, the color sensor dose not close even though I call function close() on the color sensor. The following code is what I did with EV3 Bluetooth connection.

RemoteEV3 ev3 = new RemoteEV3("10.0.1.1");
ev3.setDefault();
Keys keys1 = ev3.getKeys();
color_sensor = new EV3ColorSensor(SensorPort.S2);

do{
  color_id = color_sensor.getColorID();
  System.out.println("Here is color id: "+color_id);
  Delay.msDelay();
  keys1.waitForAnyPress();
}while(keys.getButtons()!=Keys.ID_ESCAPE);

color_sensor.close();


And it throws the following exceptions:

Exception in thread "main" lejos.hardware.DeviceException: unable to open port
at lejos.internal.ev3.EV3Port.open(Unknown Source)
at lejos.hardware.motor.BaseRegulatedMotor.&lt;init&gt;(Unknown Source)
at lejos.hardware.motor.EV3LargeRegulatedMotor.&lt;init&gt;(Unknown Source)
at lejos.remote.ev3.RMIRemoteRegulatedMotor.&lt;init&gt;(Unknown Source)
at lejos.remote.ev3.RMIRemoteEV3.createRegulatedMotor(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
at sun.rmi.transport.Transport$1.run(Transport.java:177)
at sun.rmi.transport.Transport$1.run(Transport.java:174)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:556)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:811)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:670)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at com.sun.proxy.$Proxy0.createRegulatedMotor(Unknown Source)
at lejos.remote.ev3.RemoteEV3.createRegulatedMotor(Unknown Source)
at test_bluetooth.Bluetooth.main(Bluetooth.java:186)


I am not sure that color sensor is really closed because when I run the program for the second time it yields the exception that it's unable to open the port of that color sensor. However, after I restart the brick, it works fine for the first time the program is run, then I need to restart again to use that color sensor port. Can anyone tell me how to properly close the color sensor?
Looking at the stack trace in the exception you've posted, the error seems to be coming from a call to RemoteEV3.createRegulatedMotor:

at lejos.remote.ev3.RemoteEV3.createRegulatedMotor(Unknown Source)
at test_bluetooth.Bluetooth.main(Bluetooth.java:186)


After that, it's just going through the motions to try and open a port on a remote device...

From looking at the LeJOS source, that method isn't called by the main RemoteEV3 constructor when setting up the ports, so it is probably something you're calling in your main block.

Both Sensors and Motors need to be closed.

As a point of note, it's recommended that instead of connecting to the remote sensors directly as you're doing there, you instead use the createSampleProvider method:

RemoteEV3 ev3 = new RemoteEV3("10.0.1.1");
RMISampleProvider color_sensor = ev3.createSampleProvider("S2",
               "lejos.hardware.sensor.EV3ColorSensor", "Color ID");
float[] color_id= new float[1];
color_sensor.fetchSample(color_id,0);


It is apparently more efficient because instead of running the sensor driver on the PC and sending requests to the remote EV3 you're creating the sensor driver on the remote EV3 and sending the samples to the PC.