HowTo05 < Mobileandembedded < TWiki
|
TWiki . Mobileandembedded . HowTo05
|
How to Start with Marge 0.5
Marge 0.5 is almost compatible with 0.4.0. Try to add the new jar and check what have changed, it is intuitive. Currently, there is two ways to have devices connected using Marge: Default (1) and AutoConnect (2). In addition, you can use Marge independently, like if you just want the inquiry stuff, for example.
Before start, be sure you have read how to use Marge in Eclipse IDE and Netbeans 6 IDE (by Eivar Montenegro Mosquera).
Also, take a look in the demo applications, including:
In Marge, the concept of server-client is used and the server starts a connection (registering a service) that clients would be able to connect. A server or a client can perform inquiries, but usually clients will do it to get the connection string.
1. Default (Inquiry for Devices -> Search for Services -> Connect and start exchanging messages)
Basically you need to start a server and the client needs to inquiry for the Bluetooth devices, search for specific service in a device and then connect. After this process, the both can exchange messages.
Interfaces that will have to be implemented
Before we start, Marge uses some interfaces that will be listeners in the inquiry process, message received, etc. We are going to explain each one, because they will be used in the next steps.
- CommunicationListener - It is used by both server and client device to notify received message and possible errors in receiving them.
- ConnectionListener - When a connection is established and because the server waits asynchronous, the server will be notified and returned by this interface.
- InquiryListener - The interface that returns the devices found. Used by DeviceInquirier.
- ServiceSearchListener - The interface that returns the services found. Used by ServiceSearcher.
Start a Server
Basically you create a protocol factory, create a configuration for the server and send this configuration to the factory. Your server device will return by a listener. If you do not specify nothing, default stuff will be used, such as for example, a default service.
// Creating a factory using RFCOMM protocol if you want to use L2CAP protocol
//instead, try L2CAPCommunicationFactory
CommunicationFactory factory = new RFCOMMCommunicationFactory();
// Creating a server configuration. You can set specific configurations using
//this class.
ServerConfiguration sconf = new ServerConfiguration(new CommunicationListenerImpl());
// Creating a server with the factory. The server device instance will be
//rertuned by the ConnectionListenerImpl,
//which is a implementation of ConnectionListener.
factory.waitClients(sconf, new ConnectionListenerImpl());
Inquiry for Devices
The devices found in the inquiry process will be returned by InquiryListenerImpl, which is a implementation of InquiryListener
// Starting GIAC inquiry for devices. You can do LIAC inquiries changing the method
DeviceDiscoverer.getInstance().startInquiryGIAC(
new InquiryListenerImpl());
//Cancel inquiry
DeviceDiscoverer.getInstance().cancelInquiry();
Search for Services
The devices found in the inquiry process will be returned by ServiceSearchListenerImpl which is a implementation of ServiceSearchListener. If you have not specified a specific UUID service, the default will be used, otherwise you can send as a parameter in this startSearch methods.
// Starting search for services in a found device
ServiceDiscoverer.getInstance().startSearch(remoteDeviceFound,
new ServiceSearchListenerImpl());
Connect a client
//Creates a client configuration using the specific ServiceRecord found
//by ServiceSearcher
ClientConfiguration clientConfig = new ClientConfiguration(serviceRecord,
new CommunicationListenerImpl());
//Create a protocol factory to get the client
CommunicationFactory factory = new RFCOMMCommunicationFactory();
//Gets the client device instance
ClientDevice device = factory.connectToServer(clientConfig);
Exchange messages
Both server and device needs to startListening the messages and then can send messages using send method.
// Starting listening for incoming messages
device.startListening();
// Device sending a message
device.send("Test message".getBytes());
// Closing connection
device.close();
2. AutoConnect
If you do not want to program the inquiry and search process, you can use AutoConnect. Be sure the place that the application will run does not contain too much Bluetooth devices and is running the service you want to connect, otherwise AutoConnect can take a long time to response back or possible will connect in another device with the same service running. It is a very cool feature, but it does not work in some devices, because of some implementation problems. Take a try! It is easy and fun!
Create a server
//Creates a server. The service UUID will be generated by the name.
//The serverDevice will be returned by ConnectionListenerImpl.
AutoConnect.createClient("MargeBluechatServer",
new CommunicationListenerImpl(), new ConnectionListenerImpl());
Client connects to a server
//Creates a server. The service UUID will be generated by the name.
ClientDevice device = AutoConnect.createServer("MargeBluechatServer",
new CommunicationListenerImpl());
Check "Building a Java ME Bluetooth chat in 12 minutes..." with Marge AutoConnect? in this post.
3. Layer
If you want special behavior in the messages exchanged, you can use the new concept of layer. It is still evolving to maybe something like a decorator. Currently, there is a unicast layer, if you want your device having this behavior, just extend UnicastMessageLayer. After that, you can instantiate it passing a device and then use its methods to have the special behavior.
//send a message yo a specific address
unicastLayer.sendMessage(stringAddress, "Marge rocks!".getBytes());
//all the names on the server will be returned by
//receiveNameOnServer(String address, String name) method
unicastLayer.getNamesOnServer();
//you need to implement it
public abstract void receiveMessage(String from, byte[] message);
//you need to implement it.
public abstract void receiveNameOnServer(String address, String name);
-- Main.brunogh - 15 Mar 2008
----- Revision r10 - 17 Sep 2008 - 02:41:19 - Main.brunogh
|