i just want to know how to send a byte array through bluetooth connection , i m doing an application in that i have to send a byte array to the other mobile how the sending and recieving occuring ..i got a sample code, in that its sending a string . i jus want to send a byte array ,in example code they are using streamconnnection
-----------------------client send a string when connected-------------------
- Code: Select all
StreamConnection conn = null;
try {
string msg = "hello world";
conn = (StreamConnection)Connector.open(serviceUrl);
OutputStream output = conn.openOutputStream();
output.write(msg);
output.write(msg.getBytes());
output.close();
System.out.println(readData(conn));
} catch (Exception ex) {
System.out.println(ex);
}
-----------------------server replaying for the msg "hello world" ------------
- Code: Select all
// Pauses thread until Transmission occurs
conn = notifier.acceptAndOpen();
// Read Data Transmission
String msg = readData(conn);
System.out.println("Received Message from Client: " + msg);
// Send Back a Message
msg = "Hello Back from Server";
output = conn.openOutputStream();
output.write(msg.length()); // length is 1 byte
output.write(msg.getBytes());
//output.close();
--------------read data()--------------
- Code: Select all
public final static String readData(StreamConnection conn) { //kris
InputStream input = null;
byte[] data = null;
try {
input = conn.openInputStream();
// Probably want to throw an exception if length is not greater then 0
int length = input.read();
data= new byte[length];
length = 0;
// Assemble data
while (length != data.length) {
int ch = input.read(data, length, data.length - length);
if (ch == -1) {
throw new IOException("Can't read data");
}
length += ch;
}
} catch (IOException e) {
System.err.println(e);
} finally {
// close input stream
if (input != null) {
try {
input.close();
} catch (IOException e) {}
}
}
return new String(data);
}
pls sort it out
thanks in advance
