get bluetooth Service Records - Java javax.bluetooth

Java examples for javax.bluetooth:RemoteDevice

Description

get bluetooth Service Records

Demo Code

/*// w  w  w  .  java2 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{
    private static RemoteDevice rDev = null;
    public static ServiceRecord[] getServiceRecords(final String btAddress,
            final UUID[] uArr) {
        DiscoveryListenerImpl listen;
        RemoteDevice dev = null;
        DiscoveryAgent da;
        LocalDevice device;
        ServiceRecord[] records = null;
        int transID = 0;
        int count = 0;

        try {
            Boolean synch = new Boolean(true);
            listen = new DiscoveryListenerImpl(synch);
            device = LocalDevice.getLocalDevice();

            dev = getRemoteDevice(btAddress);

            if (dev == null) {
                System.out.println("TCKAgentUtil : ERROR. Unable to"
                        + " retrieve Remote BT Device.");
                return null;
            }

            da = device.getDiscoveryAgent();
            synchronized (synch) {
                while ((records == null) && (count < 4)) {
                    transID = da.searchServices(null, uArr, dev, listen);
                    for (int k = 0; k < uArr.length; k++) {
                        System.out.println(k + " UUID is " + uArr[k]);
                    }

                    count++;

                    try {
                        System.out.println("Before wait: " + synch);
                        synch.wait();
                        System.out.println("After wait: " + synch);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    records = listen.getRecordArray();
                }
            }

        } catch (Throwable th) {
            System.out.println("TCKAgentUtil : ERROR. Unable to "
                    + "retrieve Remote Service Records.");
            return null;
        }

        System.out.println("TCKAgentUtil.getServiceRecords(): Returning "
                + records);
        return records;
    }
    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