Tuesday, July 17, 2012

Bluetooth Client and Server Information

In J2ME, the client in Bluetooth works through the DiscoveryListener interface. This means that the client-side must have some object that extends the DiscoveryListener interface. Meanwhile, the server has a DiscoveryAgent and does some things (as detailed in my last blog) to create an input-output stream between the server and client. However, the method for picking up the other (client) end of the steam was unknown. Below is a tutorial on how the J2ME client should look simply to get an input and output stream.

Note: Something followed by "()" is used to define it as a function in the API, but does not necessarily mean it has no arguments.

An action on the DiscoveryAgent on the client's side causes the four triggers in the DiscoveryListener, also on the client's side. To start, the startInquiry() method on the DiscoveryAgent will eventually call the deviceDiscovered() method in the DiscoveryListener whenever a server device is discovered. Then, the searchServices() method on the DiscoveryAgent finds services from the server and calls the servicesDiscovered() method once it has completed. This will return a ServiceRecord, which contains all the services that the server contains. To summarize, DiscoveryAgent.startInquiry() eventually calls DiscoveryListener.deviceDiscovered(), while DiscoveryAgent.searchServices() eventually calls DiscoveryListener.servicesDiscovered(), which contains as an argument a ServiceRecord.

This ServiceRecord can be used to be accessed all of the services, but the connection URL can be obtained with one line of code (Where servRecord is the instance of ServiceRecord):

String connectionURL = servRecord.getConnectionURL(0,false);

The URL returned is the RFCOMM URL that connects to this device. With this, a StreamConnection can be created with another line of code, which connects the devices and opens the stream between them:

StreamConnection con = (StreamConnection)Connector.open(connectionURL);

This StreamConnection can be broken down into an input and output stream, as in the server. These input-output stream pairs should communicate with each other, and will allow BlueMesh to pass data using the RFCOMM between the devices.

I am still uncertain as to whether the RemoteDevice's Bluetooth address or name should be used as a universal identifier to be passed to the router in BlueMesh. Both are easily accessible from the RemoteDevice. I am leaning towards the address, as the name can be duplicated, but I am unsure as to whether or not it is something that all API's (or at least the Android API) can also deliver.

No comments:

Post a Comment