Changeset 39 for chess


Ignore:
Timestamp:
02/12/09 16:50:29 (13 years ago)
Author:
BegemoT
Message:
 
Location:
chess/src/chess
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • chess/src/chess/remote/impl/BTNode.java

    r38 r39  
    55import java.io.OutputStream; 
    66import javax.bluetooth.RemoteDevice; 
     7import javax.bluetooth.ServiceRecord; 
    78import javax.microedition.io.Connector; 
    89import javax.microedition.io.StreamConnection; 
     
    3132 
    3233    private RemoteDevice device; 
     34    private final String connectionURL; 
    3335    private IConnectionListener listener = null; 
    3436    private StreamConnection connection = null; 
     
    5658 
    5759    public BTNode( final StreamConnection conn ) throws IOException { 
    58         this( null, null, conn ); 
     60        this( ( String )null, ( RemoteDevice )null, conn ); 
    5961    } 
    6062 
     
    6264                   final StreamConnection conn ) throws IOException { 
    6365        this( null, device, conn ); 
     66    } 
     67 
     68    public BTNode( final String name, 
     69                   final StreamConnection conn ) throws IOException { 
     70        this( name, ( RemoteDevice )null, conn ); 
    6471    } 
    6572 
     
    8794            this.name = name; 
    8895        } 
     96 
     97        connectionURL = getBluetoothURL( device ); 
    8998        connection = conn; 
    9099    } 
    91100 
    92101    public BTNode( final String name, 
    93                    final StreamConnection conn ) throws IOException { 
    94         this( name, null, conn ); 
     102                   final ServiceRecord record ) throws IOException { 
     103        if ( record == null ) { 
     104            throw new IllegalArgumentException( "No-no-no, David Blain! record=null " ); 
     105        } 
     106 
     107        this.device = record.getHostDevice(); 
     108 
     109        if ( name == null ) { 
     110            String _name = MSG.getMessage( "device.unknown" ); 
     111            try { 
     112                _name = this.device.getFriendlyName( true ); 
     113            } catch ( IOException e ) { 
     114                e.printStackTrace(); 
     115            } 
     116            this.name = _name; 
     117        } else { 
     118            this.name = name; 
     119        } 
     120        connectionURL = record.getConnectionURL( 
     121                ServiceRecord.NOAUTHENTICATE_NOENCRYPT, 
     122                false 
     123        ); 
     124        connection = null; 
    95125    } 
    96126 
     
    109139    public synchronized void setListener( final IConnectionListener l ) { 
    110140        if ( l != null && isClosed() ) { 
    111             throw new IllegalStateException( "Node is closed - lsitener is useless" ); 
     141            throw new IllegalStateException( "Node is closed - listener is useless" ); 
    112142        } 
    113143        this.listener = l; 
     
    204234    private void openPipeline() throws IOException { 
    205235        if ( connection == null ) { 
    206             final String addr = getBluetoothURL( device ); 
    207             connection = ( StreamConnection )Connector.open( addr, Connector.READ_WRITE, true ); 
     236            connection = ( StreamConnection )Connector.open( 
     237                    connectionURL, 
     238                    Connector.READ_WRITE, 
     239                    true 
     240            ); 
    208241        } 
    209242        if ( in == null ) { 
     
    341374 
    342375    public static String getBluetoothURL( final RemoteDevice device ) { 
    343         return BTRemote.SCHEME + device.getBluetoothAddress() + ":" + BT_STREAM; 
     376        return BTRemote.SCHEME + device.getBluetoothAddress() + ":" + 
     377                /*BTRemote.SERVICE_UUID.toString()*/ 
     378                BT_STREAM; 
    344379    } 
    345380} 
  • chess/src/chess/remote/impl/BTRemote.java

    r38 r39  
    2121public class BTRemote implements IRemote { 
    2222    public static final String SCHEME = "btspp://"; 
    23     public static final String SERVER_SUUID = "3B9FA89520078C303355AAA694238FF9"; 
    24     public static final UUID SERVER_UUID = new UUID( SERVER_SUUID, false ); 
    25     public static final String SERVER_NAME = "ChessGameServer"; 
     23    public static final String SERVICE_SUUID = "3B9FA89520078C303355AAA694238FF9"; 
     24    public static final UUID SERVICE_UUID = new UUID( SERVICE_SUUID, false ); 
     25    public static final String SERVICE_NAME = "ChessGameServer"; 
    2626 
    2727    public static final IRemoteNode[] EMPTY = new IRemoteNode[0]; 
     
    119119                while ( !isInterruptQueried() ) { 
    120120                    server = ( StreamConnectionNotifier )Connector.open( 
    121                             SCHEME + "localhost:" + SERVER_SUUID + ";name=" + SERVER_NAME, 
     121                            SCHEME + "localhost:" + SERVICE_SUUID + ";name=" + SERVICE_NAME, 
    122122                            Connector.READ_WRITE, 
    123123                            true 
    124124                    ); 
     125                    //final ServiceRecord sr = localDevice.getRecord( server );                     
    125126                    Util.log.println( "creating server and waiting for incoming" ); 
    126127                    try { 
     
    167168            } finally { 
    168169                postFinishedEvent( null ); 
     170                try { 
     171                    localDevice.setDiscoverable( DiscoveryAgent.NOT_DISCOVERABLE ); 
     172                } catch ( BluetoothStateException e ) { 
     173                    e.printStackTrace(); 
     174                } 
    169175                Util.log.println( "server stopped" ); 
    170176            } 
     
    261267        private static final int SERVICE_NAME_ATTR_ID = 0x0100; 
    262268        private static final int[] attrSet = new int[]{ SERVICE_NAME_ATTR_ID }; 
    263         private static final UUID[] uuids = new UUID[]{ SERVER_UUID }; 
     269        private static final UUID[] uuids = new UUID[]{ SERVICE_UUID }; 
    264270 
    265271        public void deviceDiscovered( final RemoteDevice btDevice, 
    266272                                      final DeviceClass cod ) { 
    267273            Util.log.println( "device found:" + btDevice.getBluetoothAddress() ); 
     274            //todo ôèëüòðîâàòü DeviceClass, ÷òîáû íå ïîäêëþ÷àòüñÿ èãðàòü ê íàóøíèêàì :)  
    268275            try { 
    269276                final int transID = agent.searchServices( attrSet, uuids, btDevice, this ); 
  • chess/src/chess/remote/impl/BaseAsyncProcess.java

    r38 r39  
    115115            } catch ( InterruptedException e ) { 
    116116            } 
    117  
    118117        } 
    119118    } 
Note: See TracChangeset for help on using the changeset viewer.