Java me 有没有人在使用j2me连接蓝牙服务时遇到过任何问题?
我一直在尝试使用j2me连接到蓝牙服务,但我得到一个空异常 客户端能够识别附近地区的设备,但无法连接到其服务。我正在粘贴代码,如果有人能告诉我原因以及代码是否正确,我将不胜感激 //服务器Java me 有没有人在使用j2me连接蓝牙服务时遇到过任何问题?,java-me,bluetooth,midp-2.0,jsr82,Java Me,Bluetooth,Midp 2.0,Jsr82,我一直在尝试使用j2me连接到蓝牙服务,但我得到一个空异常 客户端能够识别附近地区的设备,但无法连接到其服务。我正在粘贴代码,如果有人能告诉我原因以及代码是否正确,我将不胜感激 //服务器 public void startServer() { System.out.println("server is running..."); UUID uuid = new UUID("1101", true); String connection
public void startServer() {
System.out.println("server is running...");
UUID uuid = new UUID("1101", true);
String connectionString = "btspp://localhost:" + uuid +";name=Server";
try {
// create a server connection
StreamConnectionNotifier notifier =(StreamConnectionNotifier) Connector.open(connectionString);
// accept client connections
StreamConnection connection = notifier.acceptAndOpen();
LocalDevice localDevice = LocalDevice.getLocalDevice();
stringItem.setText(localDevice.getFriendlyName()+" : "+localDevice.getBluetoothAddress());
System.out.println("Address: "+localDevice.getBluetoothAddress());
System.out.println("Name: "+localDevice.getFriendlyName());
// prepare to send/receive data
byte buffer[] = new byte[100];
String msg = "hello there, client";
InputStream is = connection.openInputStream();
OutputStream os = connection.openOutputStream();
// send data to the client
os.write(msg.getBytes());
// read data from client
is.read(buffer);
int len = is.available();
stringItem.setText(Integer.toString(len));
connection.close();
} catch(IOException e) {
e.printStackTrace();
}
}
//客户
private void deServiceSearch(RemoteDevice device) {
//int[] attr={10203040607040A1B1C1DE100};
System.out.println("TEst4..");
setError("2");
UUID[] uuids = new UUID[1];
uuids[0] = new UUID(1101);
try {
System.out.println("TEst6..");
UUID[] uuidSet = new UUID[1];
uuidSet[0]=new UUID("1101",false);
System.out.println("\nSearching for service...");
setError("3");
agent.searchServices(null,uuidSet,device,this);
} catch (BluetoothStateException ex) {
setError("4");
}
}
您正在要获取的服务的属性中指定
null
。请设置属性,正如我在下面所做的
//搜索服务
可能是因为客户端代码没有任何连接?
public void doServiceSearch(RemoteDevice device){
//0x100 - Service name attribute
//0x101 - Service Description attribute
//0x102 - Provider Name attribute
//0x1002 - return all the services that are public browseable
int[] attributes = {0x100,0x101,0x102};
UUID[] uuids = new UUID[1];
uuids[0] = new UUID(0x1002);
try{
agent.searchServices(attributes, uuids, device, this);
}catch(BluetoothStateException bse){
bse.printStackTrace();
}
}