Java Reference
In-Depth Information
public void run() {
// This is the BT client portion of the dating service
try {
mDiscoveryAgent = LocalDevice.getLocalDevice().getDiscoveryAgent();
} catch (Exception ex) {
}
StreamConnection conn = null;
DataOutputStream dos = null;
while( !mEndNow) {
try {
mDateConnect = mDiscoveryAgent.selectService(DATING_SERVICE_ID,
ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
if (mDateConnect != null) {
conn = (StreamConnection) Connector.open(mDateConnect);
dos = conn.openDataOutputStream();
dos.writeUTF(mPref);
dos.writeUTF(mHeight);
dos.writeUTF(mContact);
dos.flush();
dos.close();
}
} catch (Exception ex) {
}
}
}
}
In Listing 12-1, in the run() method, note the use of the selectService() method of
DiscoveryAgent to greatly simplify Bluetooth client coding. This client will discover and use the
dating service identified by the DATING_SERVICE_ID , a UUID. If there are multiple nearby dating
services, however, the Bluetooth implementation will have control over the actual service that
will be used.
Coding the Bluetooth Service
The BTMIDlet.java file contains both the GUI and the dating service code. The logic for the
Bluetooth service is in the run() method. A thread called mServer is started in the startApp()
method of the MIDlet for running the service. Listing 12-2 contains the code.
Search WWH ::




Custom Search