📄 l2capechoclient.java
字号:
/*==========================================================================*/
/* | */
/* Filename | L2CAPEchoClient.java */
/* | */
/* Purpose | This demo application will perform device and service */
/* | discovery and communicates with an echo server. */
/* | */
/* Remarks | Needs Java API for Bluetooth (JSR-82) to operate. */
/* | */
/* Copyright | (c) 2002, Smart Network Devices GmbH, Germany */
/* | */
/* Created | July 15, 2002 */
/* | */
/* Last Change | October 21, 2002 by Peter Duchemin */
/* | */
/*==========================================================================*/
import java.lang.*;
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.bluetooth.*;
public class L2CAPEchoClient implements DiscoveryListener
{
// The DiscoveryAgent for the local Bluetooth device.
private DiscoveryAgent agent;
// The max number of service searches that can occur at any one time.
private int maxServiceSearches = 0;
// The number of service searches that are presently in progress.
private int serviceSearchCount;
// Keeps track of the transaction IDs returned from searchServices.
private int transactionID[];
// The service record to an echo server that can reply to the message
// provided at the command line.
private ServiceRecord record;
// Keeps track of the devices found during an inquiry.
private Vector deviceList;
// The constructor: creates an L2CAPEchoClient object and prepares the object
// for device discovery and service searching.
public L2CAPEchoClient() throws BluetoothStateException
{
// Retrieve the local Bluetooth device object.
LocalDevice local = LocalDevice.getLocalDevice();
// Retrieve the DiscoveryAgent object that allows us to perform device
// and service discovery.
agent = local.getDiscoveryAgent();
// Retrieve the max number of concurrent service searches that can
// exist at any one time.
try
{
maxServiceSearches = Integer.parseInt( LocalDevice.getProperty("bluetooth.sd.trans.max") );
}
catch( NumberFormatException e )
{
System.out.println( "General Application Error" );
System.out.println( "NumberFormatException: " + e.getMessage() );
}
transactionID = new int[maxServiceSearches];
// Initialize the transaction list
for( int i=0; i<maxServiceSearches; i++ )
{
transactionID[i] = -1;
}
record = null;
deviceList = new Vector();
}
// Adds the transaction table with the transaction ID provided.
private void addToTransactionTable( int trans )
{
for( int i=0; i<transactionID.length; i++ )
{
if( transactionID[i] == -1 )
{
transactionID[i] = trans;
return;
}
}
}
// Removes the transaction from the transaction ID table.
private void removeFromTransactionTable( int trans )
{
for( int i=0; i<transactionID.length; i++ )
{
if( transactionID[i] == trans )
{
transactionID[i] = -1;
return;
}
}
}
// Completes a service search on each remote device in the list until all
// devices are searched or until an echo server is found that this application
// can send messages to.
private boolean searchServices( RemoteDevice[] devList )
{
UUID[] searchList = new UUID[2];
// Add the UUID for L2CAP to make sure that the service record
// found will support L2CAP. This value is defined in the
// Bluetooth Assigned Numbers document.
searchList[0] = new UUID( 0x0100 );
// Add the UUID for the echo service that we are going to use to
// the list of UUIDs to search for. (a fictional echo service UUID)
searchList[1] = new UUID( "00112233445566778899AABBCCDDEEFF", false );
// Start a search on as many devices as the system can support.
for( int i=0; i<devList.length; i++ )
{
System.out.println( "Length = " + devList.length );
// If we found a service record for the echo service, then
// we can end the search.
if( record != null )
{
System.out.println( "Record is not null" );
return true;
}
try
{
System.out.println( "Starting Service Search on " + devList[i].getBluetoothAddress() );
int trans = agent.searchServices( null, searchList, devList[i], this );
System.out.println( "Starting Service Search " + trans );
addToTransactionTable( trans );
}
catch( BluetoothStateException e )
{
// Failed to start the search on this device, try another device.
System.out.println( "BluetoothStateException: " + e.getMessage() );
}
// Determine if another search can be started. If not, wait for
// a service search to end.
synchronized( this )
{
serviceSearchCount++;
System.out.println( "maxServiceSearches = " + maxServiceSearches );
System.out.println( "serviceSearchCount = " + serviceSearchCount );
if( serviceSearchCount == maxServiceSearches )
{
System.out.println( "Waiting" );
try
{
this.wait();
}
catch( Exception e ) {}
}
System.out.println( "Done Waiting " + serviceSearchCount );
}
}
// Wait until all the service searches have completed.
while( serviceSearchCount > 0 )
{
synchronized (this)
{
try
{
this.wait();
}
catch (Exception e) {}
}
}
if( record != null )
{
System.out.println( "Record is not null" );
return true;
}
else
{
System.out.println( "Record is null" );
return false;
}
}
// Finds the first echo server that is available to send messages to.
public ServiceRecord findEchoServer()
{
// If there are any devices that have been found by a recent inquiry,
// we don't need to spend the time to complete an inquiry.
RemoteDevice[] devList = agent.retrieveDevices( DiscoveryAgent.CACHED );
if( devList != null )
{
if( searchServices(devList) )
{
return record;
}
}
// Did not find any echo servers from the list of cached devices.
// Will try to find an echo server in the list of pre-known devices.
devList = agent.retrieveDevices( DiscoveryAgent.PREKNOWN );
if( devList != null )
{
if( searchServices(devList) )
{
return record;
}
}
// Did not find an echo server in the list of pre-known or cached
// devices. So start an inquiry to find all devices that could be
// an echo server and do a search on those devices.
try
{
agent.startInquiry(DiscoveryAgent.GIAC, this);
// Wait until all the devices are found before trying to start the
// service search.
synchronized( this )
{
try
{
this.wait();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -