get bluetooth Remote Device - Java javax.bluetooth

Java examples for javax.bluetooth:RemoteDevice

Description

get bluetooth Remote Device

Demo Code

/*/*from  www  .j  a v  a2  s . c  o m*/
 *  $HeadURL$
 *
 *
 *  Copyright (c) 2001-2008 Motorola, Inc.  All rights reserved.
 *
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *
 *  Revision History:
 *
 *  Date             Author                   Comment
 *  ---------------------------------------------------------------------------------
 *  Oct 15,2006      Motorola, Inc.           Initial creation
 *
 */
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;

public class Main{
    public static void main(String[] argv) throws Exception{
        String btAddress = "java2s.com";
        System.out.println(getRemoteDevice(btAddress));
    }
    private static RemoteDevice rDev = null;
    public static RemoteDevice getRemoteDevice(String btAddress) {
        LocalDevice device;
        boolean result = false;
        DiscoveryListenerImpl listen = null;
        DiscoveryAgent da;
        String addr;
        int count = 0;
        Boolean synch;

        if (rDev != null) {
            btAddress = (btAddress.trim()).toUpperCase();
            addr = rDev.getBluetoothAddress();
            addr = (addr.trim()).toUpperCase();

            if (btAddress.equals(addr)) {
                return rDev;
            }
        }

        do {
            try {
                synch = new Boolean(true);
                listen = new DiscoveryListenerImpl(synch, btAddress);

                device = LocalDevice.getLocalDevice();
                da = device.getDiscoveryAgent();

                synchronized (synch) {
                    result = da.startInquiry(DiscoveryAgent.GIAC, listen);

                    try {
                        synch.wait();
                    } catch (Exception e) {
                        e.printStackTrace();
                        return null;
                    }
                }
            } catch (Throwable th) {
                System.out.println("Error : " + th);
            }

            rDev = listen.getRemoteDevice();

        } while ((rDev == null) && (count++ < 5));

        return rDev;
    }
}

Related Tutorials