0
我一直在嘗試使用J2ME連接到藍牙服務,但我得到一個空例外...有沒有人遇到任何問題使用j2me連接到藍牙服務?
客戶能夠識別在附近地區的設備,但不能夠連接到他們的服務。我粘貼我的密碼,如果有人能告訴我原因,以及密碼是否正確,我將非常感激。
//服務器
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");
}
}
也許是因爲客戶端代碼沒有任何*連接*啄? – 2011-04-03 08:10:00