Java examples for javax.bluetooth:RemoteDevice
get bluetooth Service Record
/*//from w ww . ja v a 2s . 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"; int uuid = 2; System.out.println(getServiceRecord(btAddress,uuid)); } public static int SHORT = 2000; private static RemoteDevice rDev = null; public static ServiceRecord getServiceRecord(final String btAddress, final int uuid) { UUID[] uArr = new UUID[1]; ServiceRecord[] records = null; final int RFCOMM_UUID = 0x0003, L2CAP_UUID = 0x0100, OBEX_UUID = 0x0008; final String L2CAP_AGENT_SERVICE_SRVCLASS_ID = "3B9FA89520078C303355AAA694238F07"; final String RFCOMM_AGENT_SERVICE_SRVCLASS_ID = "2000000031b811d88698000874b33fc0"; final String BTGOEP_AGENT_SERVICE_SRVCLASS_ID = "3000000031b811d88698000874b33fc0"; switch (uuid) { case L2CAP_UUID: uArr[0] = new UUID(L2CAP_AGENT_SERVICE_SRVCLASS_ID, false); break; case RFCOMM_UUID: uArr[0] = new UUID(RFCOMM_AGENT_SERVICE_SRVCLASS_ID, false); break; case OBEX_UUID: uArr[0] = new UUID(BTGOEP_AGENT_SERVICE_SRVCLASS_ID, false); break; default: return null; } records = getServiceRecords(btAddress, uArr); pause(SHORT); if (records == null) { return null; } return records[0]; } 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 void pause(final int delay) { try { Thread.sleep(delay); } catch (Exception e) { } } 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; } }