📄 spp2comm.java
字号:
/*==========================================================================*/
/* | */
/* Filename | SPP2COMM.java */
/* | */
/* Purpose | This demo application will perform device and service */
/* | discovery in order to find a peer SPP device upon */
/* | reception of data on the serial port. At the same time */
/* | an SPP server will wait to be connected from another */
/* | SPP client device. The Bluetooth connection will be */
/* | established in either way, whichever occurs first. */
/* | Data is send from the serial port to the BT SPP */
/* | connection and vice versa. */
/* | */
/* Remarks | Needs Java API for Bluetooth (JSR-82) to operate. */
/* | */
/* Copyright | (c) 2002, Smart Network Devices GmbH, Germany */
/* | */
/* Created | July 27, 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 SPP2COMM implements DiscoveryListener
{
// The connection to the serial port
static StreamConnection serialport = null;
// The Input/Output streams to the local serial port
static OutputStream ser_out = null;
static InputStream ser_in = null;
// The Bluetooth connection to the peer device
static StreamConnection bluetoothport = null;
// The Input/Output streams to the Bluetooth connection
static OutputStream bt_out = null;
static InputStream bt_in = null;
// 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 cable replacement service
private ServiceRecord record;
// Keeps track of the devices found during an inquiry.
private Vector deviceList;
// The constructor: creates an SPP2COMM and prepares the object
// for device discovery and service searching.
public SPP2COMM() 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 a cable replacement peer is found that this
// application can connect 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 cable replacement service that we are going to use to
// the list of UUIDs to search for. (a fictional cable replacement service UUID)
searchList[1] = new UUID( "FFEEDDCCBBAA998877665544332211", 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 cable replacement 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 cable replacement peer that is available to connect to.
public ServiceRecord findCableReplacementService()
{
// 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 cable replacement peer from the list of cached devices.
// Will try to find an cable replacement peer in the list of pre-known devices.
devList = agent.retrieveDevices( DiscoveryAgent.PREKNOWN );
if( devList != null )
{
if( searchServices(devList) )
{
return record;
}
}
// Did not find an cable replacement peer in the list of pre-known or cached
// devices. So start an inquiry to find all devices that could be
// an cable replacement peer 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();
}
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)
{
SPP2COMM client = null;
SppServerProcess server = null;
int baudrate;
// Validate the proper number of arguments exist when starting this application.
if( (args == null) || (args.length != 1) )
{
System.out.println( "usage: java SPP2COMM <baudrate>" );
return;
}
// Create a new SPP2COMM object.
try
{
client = new SPP2COMM();
}
catch( BluetoothStateException e )
{
System.out.println( "Failed to start Bluetooth System" );
System.out.println( "BluetoothStateException: " + e.getMessage() );
}
// get the baudrate for the serial port from the command line
baudrate = Integer.parseInt( args[0] );
// make the connection to the serial port
try {
// get the connection
serialport = (StreamConnection)Connector.open( "comm:1;baudrate=" + baudrate, Connector.READ_WRITE, true );
}
catch( Exception e )
{
System.out.println( "serial port open exception: " + e );
System.exit( 0 );
}
// open the serial port's output stream
try
{
ser_out = serialport.openOutputStream();
}
catch( Exception e )
{
System.out.println( "serial output stream open exception: " + e );
System.exit( 0 );
}
// open the serial port's input stream
try
{
ser_in = serialport.openInputStream();
}
catch( Exception e )
{
System.out.println( "serial input stream open exception: " + e );
System.exit( 0 );
}
// Create a new SPP server object.
try
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -