Look to the appendix (link, which I gave). There is explanation, what is "setinputmode" and "getinputvalues". My code brings nothing. In code there's only connecting with bluetooth device (NXT Bricks, without problem), next - send few bytes, which set "input mode" (0x00, 0x05, 0x00, 0x02 - sensor type, 0x00 - sensor mode), next send command "get input values" (it's still in bytes) - 0x00, 0x07, 0x00.
- Code: Select all
Connection conn = new Connection("btspp://0016530BD2F6:1;authenticate=false;encrypt=false;master=false", 100);
System.out.println("Receive bytes...");
conn.send(new byte[] {0x00, 0x05, 0x00, 0x02, 0x0b});
Thread.sleep(2000);
while(true){
conn.send(new byte[] {0x00, 0x07, 0x00});
Thread.sleep(100);
System.out.println(conn.print(conn.receive()));
}
Connection class
- Code: Select all
public class Connection {
private String serviceURL;
private LinkedList<byte[]> received;
private LinkedList<byte[]> toSend;
Timer timer;
InputStream is;
OutputStream os;
public int drukuj(byte[] buffer) {
byte a = 0, b = 0, a2 = 0, b2 = 0;
if (buffer!=null) {
for (int i=0;i<buffer.length;i++){
System.out.println("Byte " + i + ": " + (Integer.toHexString(byteToInt(buffer[i]))+" ").toUpperCase() );
if (i == 8) b2 = buffer[i];
else if (i == 9) a2 = buffer[i];
if (i == 10) b = buffer[i];
else if (i == 11) a = buffer[i];
}
int angle = (a << 8) + b;
int angle2 = (a2 << 8) + b2;
//System.out.println("ANGLE 1: " + (angle - 204));
//int res = (int)(((double)(angle - 206)) / 3);
//if (res < 0) res = (int)(res * 2);
int res = angle2
return res;
}
return 0;
}
// konstruktor(adres uslugi, interval)
public Polaczenie(String url, int interval) {
doWyslania = new LinkedList<byte[]>();
odebrane = new LinkedList<byte[]>();
serviceURL = url;
System.out.print("Connected with devices");
try {
StreamConnection connection = (StreamConnection) Connector.open(serviceURL);
is = connection.openInputStream();
os = connection.openOutputStream();
System.out.println("Ok.");
timer = new Timer();
Zadanie zadanie = new Zadanie();
timer.scheduleAtFixedRate(zadanie, 0, interval);
} catch(IOException e) {
e.printStackTrace();
}
}
public void wyslij(byte[] buffer) {
toSend.addLast(buffer);
}
public byte[] receive() {
byte buffer[]= null;
try {
int ile;
ile = is.available();
if (ile>0) {
buffer = new byte[2];
is.read(buffer);
buffer=new byte[buffer[0]];
is.read(buffer);
odebrane.addLast(buffer);
}
} catch (IOException e) {
e.printStackTrace();
}
if (!odebrane.isEmpty())
buffer = odebrane.pollFirst();
return buffer;
}
public void wylacz() {
timer.cancel();
}
class Zadanie extends TimerTask
{
public void run( )
{
if (is != null) {
byte buffer[];
try {
// ZAPIS
if (!doWyslania.isEmpty()) {
buffer = doWyslania.pollFirst();
os.write(buffer.length);
os.write(0x00);
os.write(buffer);
os.flush();
}
// ODCZYT
//int ile = is.available();
/*buffer = null;
if (ile>0) {
buffer = new byte[2];
is.read(buffer);
buffer=new byte[buffer[0]];
is.read(buffer);
odebrane.addLast(buffer);
}*/
//if (ile > 0){
// drukuj(buffer);
//}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
As you will see this code brings nothing