📄 simplediscovery.java
字号:
/**
* BlueCove - Java library for Bluetooth
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @version $Id: SimpleDiscovery.java 198 2007-04-30 21:21:17Z skarzhevskyy $
*/
package com.intel.bluetooth.test;
import java.io.IOException;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DataElement;
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;
import com.intel.bluetooth.DebugLog;
/**
* This class provides a stand-alone test for Blue Cove
*
*/
public class SimpleDiscovery
{
public static void main(String[] args)
{
// System.getProperties().put("bluecove.debug", "true");
// System.getProperties().put("bluecove.native.path", ".");
LocalDevice l;
try
{
l = LocalDevice.getLocalDevice();
} catch (BluetoothStateException e)
{
System.err.println("Cannot get local device: " + e);
return;
}
l.getDiscoveryAgent();
System.out.println("Local btaddr is " + l.getBluetoothAddress());
System.out.println("Local name is " + l.getFriendlyName());
BluetoothInquirer bi = new BluetoothInquirer();
while (true)
{
System.out.println("Starting inquiry");
if (!bi.startInquiry())
break;
while (bi.inquiring)
{
try
{
Thread.sleep(1000);
} catch (Exception e)
{
}
}
}
}
public static class BluetoothInquirer implements DiscoveryListener
{
boolean inquiring;
public boolean startInquiry()
{
try
{
LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(
DiscoveryAgent.GIAC, this);
} catch (BluetoothStateException e)
{
System.err.println("Cannot start inquiry: " + e);
return false;
}
inquiring = true;
return true;
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
{
DebugLog.debug("deviceDiscovered");
StringBuffer name;
String nameStr=null;
try
{
DebugLog.debug("call getFriendlyName");
name = new StringBuffer(btDevice.getFriendlyName(false));
nameStr = name.toString();
} catch (IOException ioe)
{
ioe.printStackTrace();
name = new StringBuffer();
}
while (name.length() < 20)
name.append(' ');
System.out.println("Found " + btDevice.getBluetoothAddress()
+ " :" + name + ": " + cod);
if (!nameStr.equalsIgnoreCase("Nokia 3230"))
return;
int[] attr = new int[]{0x0000, 0x0001, 0x0002, 0x0003, 0x0004,
0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D,
0x0100, 0x0101, 0x0102, 0x0200, 0x0201,
0x0301, 0x0302, 0x0303, 0x0304, 0x0305, 0x0306, 0x0307, 0x0308, 0x0309,
0x030A, 0x030B, 0x030C, 0x030D, 0x030E, 0x0310, 0x0311, 0x0312, 0x0313 };
//
// search for L2CAP services, most services based on L2CAP
//
// note: Rococo simulator required a new instance of Listener for
// every search. not sure if this is also the case in real devices
try
{
DiscoveryAgent agent =LocalDevice.getLocalDevice().getDiscoveryAgent();
agent.searchServices( attr, // attributes to retrieve from remote device
new UUID[]{ new UUID( 0x0100 ) }, // search criteria, 0x0100 =
btDevice,
this);
} catch (BluetoothStateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} // direct discovery response to Listener object
}
public void printRecord(ServiceRecord record)
{
int[] attrids = record.getAttributeIDs();
for (int i=0;i<attrids.length;i++)
{
int id = attrids[i];
DataElement element = record.getAttributeValue(id);
System.out.println("attrid="+id+", datatype="+element .toString());
//// if (id==0x0000)
//// {
//// System.out.println("ServiceRecordHandle="+element.INT_4);
//// }else if (id==0x0001)
//// {
////
// }
}
}
public void servicesDiscovered(int transID, ServiceRecord[] servRecord)
{
System.out.println("[servicesDiscovered] transID="+transID+", servRecord.length="+servRecord.length);
for (int i=0;i<servRecord.length;i++)
{
printRecord(servRecord[i]);
System.out.println("--------------------------------");
}
}
public void serviceSearchCompleted(int transID, int respCode)
{
}
public void inquiryCompleted(int discType)
{
inquiring = false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -