Discover devices and transfer files through Bluetooth in Java ME

Source: Internet
Author: User

One of the first steps in performing a Bluetooth application on a Java ME device is the discovery process (discovery processes). In short, the discovery process is the process of finding each other with Bluetooth devices and then working together to find out which services they can support. The next step is to learn how to transfer data between these 22 devices.

In this little technical article, I'll show you how to create a midlet that can look up a device to each other, and then let the user send a simple message to one of the devices that was found. I've tested and verified this midlet on the Nokia N95 machine, and it can connect to a computer running Windows Vista by enabling Bluetooth support.

I divided the whole process into the following steps:

1. Start the discovery process.

2. Query the services supported by the devices found in the discovery process.

3. Start and process a OBEX data exchange using the URL of a support service.

These steps are described in detail in the following paragraphs. Follow the code snippet in these steps to check the entire source code of the MIDlet. The source code can be obtained in a compressed file under the last resources.

The first step: start the discovery process

The discovery process is used to tell the local Bluetooth stack to be paired with any Bluetooth device in the vicinity. In this midlet, this stack can be done through the JSR 82来 provided by your device provider. The discovery process starts by discovering agents on the local device, as shown in the following code:

// get the local discovery agent
  agent = LocalDevice.getLocalDevice ().getDiscoveryAgent();

  // start the inquiry for general unlimited inquiry

  agent.startInquiry(DiscoveryAgent.GIAC, this);

Once the agent-initiated discovery process is discovered, it invokes various callback methods on a class that performs the Discoverylistener interface. As far as we are concerned, this is our MIDlet class.

Specifically, four methods of this interface must be executed, of which two are of interest to us in the discovery phase: devicediscovered (remotedevice btdevice, deviceclass cod) and inquirycompleted ( int disctype). These two methods deal with the discovery of a device and complete the discovery process. In the code from MIDlet shown below, once they are discovered or when the program is finished, we use these methods to add the UI on our device.

public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
  try {

  // add the devices using the friendly names

  listofDevices.append (btDevice.getFriendlyName(false), null);

  // add to the devices hashtable

  devices.put(new Long(listofDevices.size()), btDevice);

  } catch(Exception ex) { handleError(ex); }

  }

  public void inquiryCompleted(int discType) {

  // once the inquiry is completed, show the list of devices to the user

  if(listofDevices.size() == 0) {

  display.setCurrent(nothing, noteBox);

  } else {

  display.setCurrent(listofDevices);

  }
      }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.