Java me 如何向蓝牙设备发送消息?
使用j2me中的蓝牙API,我想向另一部手机发送消息。我已经能够在相应的设备上发现设备和服务。但是,当我尝试从服务器向客户端发送消息时,我也能够连接到服务。消息已写入,但客户端似乎未收到Java me 如何向蓝牙设备发送消息?,java-me,bluetooth,sms,Java Me,Bluetooth,Sms,使用j2me中的蓝牙API,我想向另一部手机发送消息。我已经能够在相应的设备上发现设备和服务。但是,当我尝试从服务器向客户端发送消息时,我也能够连接到服务。消息已写入,但客户端似乎未收到 public void startServer() throws IOException { UUID uuid = new UUID("1101", false); //Create the service url
public void startServer() throws IOException {
UUID uuid = new UUID("1101", false);
//Create the service url
String connectionString = "btspp://localhost:" + uuid + ";name=xyz";
//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();
RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
System.out.println("Remote device address: " + dev.getBluetoothAddress());
System.out.println("Remote device name: " + dev.getFriendlyName(true));
Survey.setTitle(dev.getFriendlyName(true));
//read string from spp client
try {
DataInputStream in = connection.openDataInputStream();
OutputStream writer=connection.openDataOutputStream();
String str="";
TextField textfield;
for (int i=0;i<questions.size();i++){
textfield = (TextField) questions.elementAt(i);
str += formatSurvey(textfield,i)+"&";
}
writer.write(str.getBytes(), 0, str.getBytes().length);
writer.flush();
System.out.println("Written to client "+str);
System.out.println("Reading "+in.readUTF());
try {
displaySurveyresults(str);
}
catch(Exception e){
System.out.println(e.getMessage());
}
streamConnNotifier.close();
}
catch(Exception e){
System.err.println(e.getMessage());
}
}
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
switchDisplayable(null , getList1());
list1.append(servRecord.toString(), null);
System.out.println("Service discovered..."+servRecord.toString());
for (int i=0;i<servRecord.length;i++){
try {
System.out.println("Test1");
//StreamConnection con = (StreamConnection) Connector.open(servRecord[i].getConnectionURL(0 , false));
String connURL = servRecord[0].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
// Open connection
StreamConnection con = (StreamConnection) Connector.open(connURL);
System.out.println("Test2");
DataInputStream in = con.openDataInputStream();
System.out.println("Test3"+in.readUTF());
//con.openDataOutputStream().write(142);
System.out.println("Test4 "+in.available());
byte[] bte=new byte[in.available()];
System.out.println("Test5 "+bte.length);
in.read(bte);
System.out.println("Test6");
for (int l=0;l<bte.length;l++){
System.out.println(bte[i]);
System.out.println("Test7");
stringItem.setText(stringItem.getText()+1 + bte[i]);
}
OutputStream outStream=con.openOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(outStream);
writer.write("Vimal");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public void startServer()引发IOException{
UUID UUID=新UUID(“1101”,假);
//创建服务url
字符串连接字符串=”btspp://localhost:+uuid+“名称=xyz”;
//打开服务器url
StreamConnectionNotifier StreamConnectionNotifier=(StreamConnectionNotifier)连接器。打开(connectionString);
//等待客户端连接
System.out.println(“\n服务器已启动,正在等待客户端连接…”);
StreamConnection连接=streamConnNotifier.AcceptOnDopen();
RemoteDevice dev=RemoteDevice.getRemoteDevice(连接);
System.out.println(“远程设备地址:+dev.getBluetoothAddress());
System.out.println(“远程设备名称:+dev.getFriendlyName(true));
setTitle(dev.getFriendlyName(true));
//从spp客户端读取字符串
试一试{
DataInputStream in=connection.openDataInputStream();
OutputStream编写器=连接。openDataOutputStream();
字符串str=“”;
TextField;
对于(int i=0;i尝试将新UUID(“1101”,false);
替换为新UUID(0x1101);
我没有得到任何记录。还有其他解决方案吗?