📄 spp2comm.java
字号:
{
server = new SppServerProcess();
server.start();
}
catch( Exception e )
{
System.out.println( "Failed to start Spp Server" + e );
System.exit( 0 );
}
// the main loop runs forever. However, it can be stopped
// by terminating the KVM from the command line
while( true )
{
// Create buffer to receive data from the serial port
byte[] rxdata = new byte[64];
int rxlen=0;
int data;
try
{
// read in as many bytes from the serial port
// as currently available but do not exceed the
// current buffer length.
// The read() method blocks but is periodically released
// by an InterruptedIOException in order to allow other
// things to happen meanwhile
while( true )
{
data = ser_in.read();
rxdata[rxlen] = (byte)data;
rxlen++;
if( rxlen >= 64 || data == -1 )
break;
}
System.out.println( "data received from serial port, len=" + rxlen );
}
catch( InterruptedIOException e )
{
System.out.println( "serial port receive timeout: " + e );
}
catch( Exception e )
{
System.out.println( "serial port receive exception: " + e );
}
// Did we get any data from the serial port?
if( rxlen > 0 )
{
// Do we have a Bluetooth connection already?
if( bluetoothport != null )
{
// Do we have an OutputStream on the BT connection already?
if( bt_out == null )
{
// no, then create one
try
{
bt_out = bluetoothport.openOutputStream();
}
catch( Exception e )
{
System.out.println( "Bluetooth output stream open exception: " + e );
}
}
System.out.println( "send serial data on Bluetooth link" );
try
{
bt_out.write( rxdata );
bt_out.flush();
}
catch( Exception e )
{
System.out.println( "Bluetooth output stream write exception: " + e );
}
}
else
{
System.out.println( "No Bluetooth link: try to establish one..." );
// Find a cable replacement service in the local area
ServiceRecord cableReplacementService = client.findCableReplacementService();
if( cableReplacementService != null )
{
// retrieve the connection URL string
String conURL = cableReplacementService.getConnectionURL( ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false );
try
{
// Create a connection to the SPP peer
bluetoothport = (StreamConnection)Connector.open( conURL );
}
catch( Exception e )
{
System.out.println( "Failed to establish Bluetooth link: " + e );
}
if( bluetoothport != null )
{
try
{
// open an OutputStream on the Bluetooth connection
bt_out = bluetoothport.openOutputStream();
}
catch( Exception e )
{
System.out.println( "Bluetooth output stream open exception: " + e );
}
// and send the data from the serial port
System.out.println( "send serial data on Bluetooth link" );
try
{
bt_out.write( rxdata );
bt_out.flush();
}
catch( Exception e )
{
System.out.println( "Bluetooth output stream write exception: " + e );
}
}
}
else
{
System.out.println( "No SPP peer found" );
}
}
}
// do we have a Bluetooth connection already?
if( bluetoothport != null )
{
// do we have an InputStream on the Bluetooth connection already?
if( bt_in == null )
{
// no, then create one
try
{
bt_in = bluetoothport.openInputStream();
}
catch( Exception e )
{
System.out.println( "Bluetooth output stream open exception: " + e );
}
}
// listen on the bluetooth connection
rxlen = 0;
try
{
// read in as many bytes from the serial port
// as currently available but do not exceed the
// current buffer length.
// The read() method blocks but is periodically released
// by an InterruptedIOException in order to allow other
// things to happen meanwhile
while( true )
{
data = bt_in.read();
rxdata[rxlen] = (byte)data;
rxlen++;
if( rxlen >= 64 || data == -1 )
break;
}
System.out.println( "data received from bluetooth port, len=" + rxlen );
}
catch( InterruptedIOException e )
{
System.out.println( "Bluetooth port receive timeout: " + e );
}
catch( Exception e )
{
System.out.println( "Bluetooth port receive exception: " + e );
}
try
{
System.out.println( "send Bluetooth data on serial link" );
ser_out.write( rxdata );
ser_out.flush();
}
catch( Exception e )
{
System.out.println( "Bluetooth output stream write exception: " + e );
}
}
}
}
// 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];
System.out.println( "After this" );
if( record == null )
{
System.out.println( "The Seocnd 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) {}
}
}
}
class SppServerProcess extends Thread
{
/* the constructor */
SppServerProcess()
{
}
public void run()
{
StreamConnectionNotifier Server = null;
try
{
LocalDevice local = LocalDevice.getLocalDevice();
local.setDiscoverable( DiscoveryAgent.GIAC );
}
catch( BluetoothStateException e )
{
System.err.println( "Failed to start service" );
System.err.println( "BluetoothStateException: " + e.getMessage() );
return;
}
try
{
// start the SPP server (with a fictional UUID)
Server = (StreamConnectionNotifier)Connector.open( "btspp://localhost:FFEEDDCCBBAA99887766554433221100" );
}
catch( IOException e )
{
System.err.println( "Failed to start service" );
System.err.println( "IOException: " + e.getMessage() );
return;
}
System.out.println( "Starting SPP Server" );
while( true )
{
// accept connections only if we are not yet connected
if( SPP2COMM.bluetoothport == null )
{
try
{
// wait for incoming client connections (blocking method)
SPP2COMM.bluetoothport = Server.acceptAndOpen();
}
catch( IOException e )
{
System.out.println("IOException: " + e.getMessage());
}
}
try
{
Thread.sleep( 1000 );
}
catch( Exception e ) { }
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -