📄 l2capechoclient.java
字号:
}
catch (Exception e) {}
}
}
catch( BluetoothStateException e )
{
System.out.println( "Unable to find devices to search" );
}
if( deviceList.size() > 0 )
{
devList = new RemoteDevice[deviceList.size()];
deviceList.copyInto( devList );
if( searchServices(devList) )
{
return record;
}
}
return null;
}
// This is the main method of this application.
public static void main(String[] args)
{
L2CAPEchoClient client = null;
// Validate the proper number of arguments exist when starting this application.
if( (args == null) || (args.length != 1) )
{
System.out.println( "usage: java L2CAPEchoClient <message>" );
return;
}
// Create a new EchoClient object.
try
{
client = new L2CAPEchoClient();
}
catch( BluetoothStateException e )
{
System.out.println( "Failed to start Bluetooth System" );
System.out.println( "BluetoothStateException: " + e.getMessage() );
}
// Find an Echo Server in the local area
ServiceRecord echoService = client.findEchoServer();
if( echoService != null )
{
// retrieve the connection URL string
String conURL = echoService.getConnectionURL( ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false );
// create a new client instance
EchoClient echoClient = new EchoClient( conURL );
// and send the message give on the command line
echoClient.sendMessage( args[0] );
}
else
{
System.out.println( "No Echo Server was found" );
}
}
// Called when a device was found during an inquiry. An inquiry
// searches for devices that are discoverable. The same device may
// be returned multiple times.
public void deviceDiscovered( RemoteDevice btDevice, DeviceClass cod )
{
System.out.println( "Found device = " + btDevice.getBluetoothAddress() );
deviceList.addElement( btDevice );
}
// The following method is called when a service search is completed or
// was terminated because of an error. Legal values include:
// SERVICE_SEARCH_COMPLETED, SERVICE_SEARCH_TERMINATED,
// SERVICE_SEARCH_ERROR, SERVICE_SEARCH_DEVICE_NOT_REACHABLE
// and SERVICE_SEARCH_NO_RECORDS
public void serviceSearchCompleted( int transID, int respCode )
{
System.out.println( "serviceSearchCompleted(" + transID + ", " + respCode + ")" );
// Removes the transaction ID from the transaction table.
removeFromTransactionTable( transID );
serviceSearchCount--;
synchronized( this )
{
this.notifyAll();
}
}
// Called when service(s) are found during a service search.
// This method provides the array of services that have been found.
public void servicesDiscovered( int transID, ServiceRecord[] servRecord )
{
// If this is the first record found, then store this record
// and cancel the remaining searches.
if( record == null )
{
System.out.println( "Found a service " + transID );
System.out.println( "Length of array = " + servRecord.length );
if( servRecord[0] == null )
{
System.out.println( "The service record is null" );
}
record = servRecord[0];
if( record == null )
{
System.out.println( "The second try was null" );
}
// Cancel all the service searches that are presently
// being performed.
for( int i=0; i<transactionID.length; i++ )
{
if( transactionID[i] != -1 )
{
System.out.println( agent.cancelServiceSearch(transactionID[i]) );
}
}
}
}
// Called when a device discovery transaction is
// completed. The <code>discType</code> will be
// INQUIRY_COMPLETED if the device discovery transactions ended normally,
// INQUIRY_ERROR if the device discovery transaction failed to complete normally,
// INQUIRY_TERMINATED if the device discovery transaction was canceled by calling
// DiscoveryAgent.cancelInquiry().
public void inquiryCompleted( int discType )
{
synchronized( this )
{
try
{
this.notifyAll();
}
catch (Exception e) {}
}
}
}
// The EchoClient will make a connection using the connection string
// provided and send a message to the server to print the data sent.
class EchoClient
{
// Keeps the connection string in case the application would like to make
// multiple connections to an echo server.
private String serverConnectionString;
// The constructor: creates an EchoClient object that will allow an
// application to send multiple messages to an echo server.
EchoClient( String server )
{
serverConnectionString = server;
}
// Sends a message to the server.
public boolean sendMessage( String msg )
{
L2CAPConnection con = null;
byte[] data = null;
int index = 0;
byte[] temp = null;
try
{
// Create a connection to the server
con = (L2CAPConnection)Connector.open( serverConnectionString );
// Determine the maximum amount of data I can send to the server.
int MaxOutBufSize = con.getTransmitMTU();
temp = new byte[MaxOutBufSize];
// Send as many packets as are needed to send the data
data = msg.getBytes();
while( index < data.length )
{
// Determine if this is the last packet to send or if there
// will be additional packets
if( (data.length - index) < MaxOutBufSize )
{
temp = new byte[data.length - index];
System.arraycopy( data, index, temp, 0, data.length-index );
}
else
{
temp = new byte[MaxOutBufSize];
System.arraycopy( data, index, temp, 0, MaxOutBufSize );
}
con.send(temp);
index += MaxOutBufSize;
}
// Prepare a receive buffer
int rxlen = con.getReceiveMTU();
byte[] rxdata = new byte[rxlen];
// Wait to receive the server's reply (method blocks!)
rxlen = con.receive( rxdata );
// Here, we've got it
String message = new String( rxdata, 0, rxlen );
System.out.println( "Server replied: " + message );
// Close the connection to the server
con.close();
}
catch( BluetoothConnectionException e )
{
System.out.println( "Failed to send the message" );
System.out.println( "BluetoothConnectionException: " + e.getMessage() );
System.out.println( "Status: " + e.getStatus() );
}
catch( IOException e )
{
System.out.println( "Failed to send the message" );
System.out.println( "IOException: " + e.getMessage() );
return false;
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -