#ff0000 18  2005. - #000655  Bluetooth  J2ME . (JSR-82) -  2.  Connection  Device Management API
#def  Connection API
  Java API  Bluetooth,    Bluetooth ,  Serial Port Profile (RFCOMM), L2CAP  OBEX. OBEX     (    P900/P908),      . 
 Bluetooth 
Java API  Bluetooth   CLDC 1.0 Generic Connection Framework (GCF).       GCF  Bluetooth API. GCF  Java API  Bluetooth    . 
    Connector.    RFCOMM  OBEX, Bluetooth API     GCF  StreamConnection  StreamConnectionNotifier.  L2CAP    : L2CAPConnection  L2CAPConnectionNotifier. 
StreamConnection   InputConnection  OutputConnection,            .  StreamConnectionNotifier    stream . StreamConnectionNotifier    acceptAndOpen(),       .            MIDP 1.0 Javadoc. 
L2CAPConnection    InputConnection  OutputConnection.       ,          Maximum Transmission Unit (MTU). MTU    ,         . L2CAPConnectionNotifier    StreamConnectionNotifier,      L2CAP . L2CAPConnectionNotifier    acceptAndOpen(),      L2CAP . 
 
 Bluetooth (  )      GCF ,        Connector.      Connector.open()   URL,    Bluetooth .      open(): 
open(String url) 
open(String url, int mode) 
open(String url, int mode, boolean timeout) 
 url -  URL,    Bluetooth  (  ). 
mode -   : READ, WRITE  READ_WRITE (   ). 
timeout -  ,  ,          (InterruptedIOException),       (     false). 
     .  URL   : 
scheme://host:port;parameters
 
scheme -    (   ). 
host -      ;  : licalhost. 
port -     ;   - service UUID. 
parameters -  ,      MTU. 
      URL    GCF ,     RFCOMM  L2CAP Bluetooth : Bluetooth Connection 	URL Scheme	Client Connection 	Server Connection
Serial Port Profile (RFCOMM)	Btspp	StreamConnection	StreamConnectionNotifier, StreamConnection 
L2CAP	btl2cap	L2CAPConnection	L2CAPConnectionNotifier, L2CAPConnection 
   Bluetooth ,     URL     . ,   RFCOMM ,   URL btspp ,     : 
StreamConnection con = (StreamConnection) Connector.open("btspp://0050C000321B:5"); 
 L2CAP     btl2cap,     . 
L2CAPConnection con = (L2CAPConnection) Connector.open("btl2cap://0050C000321B:1000"); 
   ,   ServiceRecord.getConnectionURL(). 
     Bluetooth ,   localhost, service UUID  . ,   RFCOMM (SPP) ,   btspp, localhost   , UUID  : 
StreamConnectionNotifier cn = (StreamConnectionNotifier) Connector.open("btspp://localhost:" + MY_SERVICE_NUMBER); 
  MY_SERVICE_NUMBER  UUID   : 
"3B9FA89520078C303355AAA694238F08;name=mySPPSrv".
  L2CAP ,   btl2cap, UUID  : 
L2CAPConnectionNotifier = cn (L2CAPConnectionNotifier) 
Connector.open("btl2cap://localhost:" + MY_SERVICE_NUMBER); 

MY_SERVICE_NUMBER    RFCOMM (SPP)
  Bluetooth             : 
LocalDevice local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC); 
 
    
  ,    : 
 RFCOMM - StreamConnectionNotifier 
 L2CAP - L2CAPConnectionNotifier 
         acceptAndOpen(). 
Listing 1.   L2CAP 
L2CAPConnectionNotifier server = null; 
byte[] data = null; 
int length; 
: 
: 
try { 
    //           
    LocalDevice local = LocalDevice.getLocalDevice(); 
    local.setDiscoverable(DiscoveryAgent.GIAC); 
    //   L2CAP 
    server = (L2CAPConnectionNotifier) 
     Connector.open("btl2cap://localhost:1020304050d0708093a1b121d1e1f100"); 
    while (!done) { 
        L2CAPConnection conn = null; 
        //   L2CAP  
        conn = server.acceptAndOpen(); 
        //   
        length = conn.getReceiveMTU(); 
        data = new byte[length]; 
        length = conn.receive(data); 
        : 
        : 
    } 
} catch (Exception e) { 
    ...   
} 
Listing 2.    .
StreamConnectionNotifier server = null; 
byte[] data = new byte[256]; 
int length;
try { 
    //            
    LocalDevice local = LocalDevice.getLocalDevice(); 
    local.setDiscoverable(DiscoveryAgent.GIAC); 
    //    
    server = (StreamConnectionNotifier)Connector.open( 
        "btspp://localhost:11111111111111111111111111111111"); 
    while (!done) { 
        StreamConnection conn = null; 
        //     
        conn = server.acceptAndOpen(); 
        //    
        InputStream in = conn.openInputStream(); 
        length = in.read(data); 
        : 
        : 
    } 
} catch (Exception e) { 
    ...   
} 
   
       StreamConnection,      L2CAP  - L2CAPConnection
Listing 3.      L2CAPConnection. 
String url = "..."; 
int index = 0; 
L2CAPConnection con = null; 
transmitBuffer[] temp = null; 
byte[] data = ...; 
 
try { 
    //     
    con = (L2CAPConnection)Connector.open(url); 
    //  transmit MTU (  ,    )
    int MTUSize = con.getTransmitMTU(); 
    //    MTU. 
    transmitBuffer = new byte[MaxOutBufSize]; 
    : 
    : 
 
    while (index < data.length) { 
        //   ...  MTUSize     
        if ((data.length - index) < MTUSize) { 
            System.arraycopy(data, index, transmitBuffer, 0, data.length - index); 
        } else { 
            System.arraycopy(data, index, transmitBuffer, 0, MTUSize); 
        } 
        //      
        con.send(transmitBuffer); 
        index += MTUSize; 
        //    
        for (int=0; i<MTUSize; i++) transmitBuffer[i] = 0; 
    } 
    //     .
    con.close(); 
} catch (Exception e) { 
    ...   .
} 
Listing 4.      StreamConnection. 
String url = ; 
OutputStream os = null; 
StreamConnection con = null; 
: 
: 
try { 
   //     
   con =(StreamConnection)Connector.open(url); 
   //    
   os = con.openOutputStream(); 
   //       
   os.write(data.getBytes()); 
   //     
   os.close(); 
   con.close(); 
} catch (Exception e) { 
    ...    
} 
 Device Management API
       javax.bluetooth.LocalDevice, javax.bluetooth.RemoteDevice  javax.bluetooth.DeviceClass.       Generic Access Profile (GAP). 
 
   Bluetooth     javax.bluetooth.LocalDevice.     ,        (, Bluetooth ,  ,    ).       : 
static LocalDevice getLocalDevice() -   LocalDevice ,   . 
java.lang.String getBluetoothAddress() -    Bluetooth .     Java.lang.String    12 . 
java.lang.String getFriendlyName() -      Bluetooth . 
DiscoveryAgent getDiscoveryAgent() -       . 
boolean setDiscoverable(int mode) -      . 
static java.lang.String getProperty(java.lang.String property -     Bluetooth . 
ServiceRecord getRecord(javax.microedition.io.Connection notifier) -    ,     (btspp, btl2cap  btgoep). 
Listing 5.  LocalDevice .
import javax.microedition.io.*; 
import javax.bluetooth.*; 
 
LocalDevice localDevice; //   
String localAddress; //    
String localName; //   
DiscoveryAgent agent; //     Bluetooth 
: 
: 
try { 
    //    
    localDevice = LocalDevice.getLocalDevice(); 
    //      
    localDevice.setDiscoverable(DiscoveryAgent.GIAC); 
    //    Bluetooth  
    localAddress = localDevice.getBluetoothAddress(); 
    //    
    localName = localDevice.getFriendlyName(); 
     //         
    agent = localDevice.getDiscoveryAgent(); 
} catch (Exception e) { 
    ...  
} 
 
 Bluetooth    javax.bluetooth.RemoteDevice.       RemoteDevice ,   Bluetooth ;       ,   ,    .     : 
static RemoteDevice getRemoveDevice(javax.microedition.io.Connection) -   RemoteDevice ,    . 
java.lang.String getBluetoothAddress() -    Bluetooth .     Java.lang.String    12 . 
java.lang.String getFriendlyName() -      Bluetooth . 
boolean authenticate() -    . 
boolean isAuthenticated() - ,     . 
boolean isEncrypted() - ,           
Listing 6. Using the RemoteDevice methods. 
import javax.microedition.io.*; 
import javax.bluetooth.*; 
 
String url = "..."; 
StreamConnection con; 
RemoteDevice remoteDevice; //  
String remoteAddress; //   
String remoteName; //   
: 
: 
try { 
    con = (StreamConnection) Connector.open(url); 
    remoteDevice = RemoteDevice.getRemoteDevice(con); 
    //    
    remoteAddress = remoteDevice.getBluetoothAddress(); 
    //    
    remoteName = remoteDevice.getFriendlyName(); 
    if (!remoteDevice.isEncrypted()) { 
        //    ,   .
        if (!remoteDevice.authenticate() || !remoteDevice.encrypt(con, true)) { 
            // ,   
            return; 
        } 
    } 
} catch (Exception e) { 
    ...  
} 
 
DeviceClass    (CoD)  Bluetooth .      major, minor  service .  DeviceClass   : 
int getMajorDeviceClass() -  major  . 
int getMinorDeviceClass() -  minor  . 
int getServiceClasses() -  service . 
Listing 7.   DeviceClass. 
static final NLDMSC = 0x22000; // Networking, Limited Discoverable Major Service Class 
static final PHONE_MAJOR_CLASS = 0x200; 
static final CELLULAR_MINOR_CLASS = 0x04; 
: 
: 
LocalDevice localDevice; 
DeviceClass deviceClass; 
: 
: 
try { 
    //    
    localDevice = LocalDevice.getLocalDevice(); 
    //      
    deviceClass = localDevice.getDeviceClass(); 
    //        
    if (deviceClass.getMajorDeviceClass() == PHONE_MAJOR_CLASS) { 
        if (deviceClass.getMinorDeviceClass() == CELLULAR_MINOR_CLASS) { 
            : 
            : 
        } 
    } 
} catch (Exception e) { 
    ...  
} 
    Bluetooth 
Java APIs for Bluetooth      Bluetooth .         LocalDevice.getProperty(): 

LocalDevice locaDevice = LocalDevice.getLocalDevice(); 
String apiVer = localDevice.getProperty(bluetooth.api.version); 
         	
bluetooth.api.version 	   Java APIs for Bluetooth
bluetooth.l2cap.receiveMTU.max 	  L2CAP  ReceiveMTU  
bluetooth.connected.devices.max 	    (  )
bluetooth.connected.inquiry 	     ?
bluetooth.connected.page 	       ?
bluetooth.connected.inquiry.scan 	      ?
bluetooth.connected.page.scan 	      ?
bluetooth.master.switch 	    master/slave?
bluetooth.sd.trans.max 	    .
bluetooth.sd.attr.retrievable.max 	   ,      
#ff0000      Developing Applications with the Java APIs for Bluetooth (JSR-82),    http://developer.sonyericsson.com
#0000ff : aRix.