Header Sep

Java Tips, Tricks & Code

我的评分 用户评价
登录票选本页

February 2006

JSR 82 example, Bluetooth probe

 

Here is a Bluetooth, Java ME example on how to search for Bluetooth-enabled mobile phones and services.

Download source code sample>>

The JSR 82 API is supported by Sony Ericsson phones from Java Platform 5 (JP-5) onwards and includes phones such as the K750, K600, W800, Z520 and W810.

Use this to see if your phone supports JSR 82:

try{
    Class.forName("javax.bluetooth.LocalDevice");
}
catch(Exception ex){
    System.out.println("No support for JSR-82");
}

To start searching for Bluetooth-enabled phones, use the DiscoveryAgent:

LocalDevice localDevice = LocalDevice.getLocalDevice();

discoveryAgent = localDevice.getDiscoveryAgent();
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);


The deviceDiscovered method is called when a mobile phone is found:

public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass cod) {
        try{
            remoteDevices.addElement(remoteDevice);
        } catch(Exception e){
                e.printStackTrace();
        }

}

When a phone is found, we can search for services on that device:

LocalDevice localDevice = LocalDevice.getLocalDevice();
DiscoveryAgent discoveryAgent = localDevice.getDiscoveryAgent();
discoveryAgent.searchServices(attrSet, uuidSet, remoteDevice, this);

And the servicesDiscovered method is called when a service is found:

public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {

        for(int i = 0; i < servRecord.length; i++) {

            DataElement serviceNameElement =
  servRecord[i].getAttributeValue(0x0100);
                String temp_serviceName =
 (String)serviceNameElement.getValue();

                String serviceName = temp_serviceName.trim();
        }
}


我的评分 用户评价
登录票选本页