001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.midp.io.j2me.btgoep;
027:
028: import javax.microedition.io.StreamConnectionNotifier;
029: import javax.microedition.io.StreamConnection;
030: import javax.microedition.io.Connection;
031: import com.sun.kvem.jsr082.obex.ObexTransportNotifier;
032: import com.sun.kvem.jsr082.obex.ObexTransport;
033: import java.io.IOException;
034: import javax.bluetooth.*;
035: import com.sun.kvem.jsr082.bluetooth.ServiceRecordImpl;
036:
037: /**
038: * Provides underlying stream notifier to shared obex implementation.
039: */
040: public class BTGOEPNotifier implements ObexTransportNotifier {
041:
042: /** Keeps notifier for transport layer */
043: private StreamConnectionNotifier notifier;
044:
045: /** Keeps OBEX UUID for service record construction. */
046: static final DataElement DE_OBEX_UUID = new DataElement(
047: DataElement.UUID, new UUID(0x0008));
048:
049: /**
050: * Create BTGOEP Notifier
051: * @param notifier notifier for transport layer
052: * @exception IOException if an error occured while service record
053: * creation
054: */
055: protected BTGOEPNotifier(StreamConnectionNotifier notifier)
056: throws IOException {
057: this .notifier = notifier;
058: try {
059: ServiceRecord servRec = LocalDevice.getLocalDevice()
060: .getRecord(notifier);
061: DataElement protocolList = null;
062: synchronized (servRec) {
063: protocolList = servRec
064: .getAttributeValue(ServiceRecordImpl.PROTOCOL_DESCRIPTOR_LIST);
065: DataElement p = new DataElement(DataElement.DATSEQ);
066: p.addElement(DE_OBEX_UUID); // obex UUID
067: protocolList.addElement(p);
068: servRec.setAttributeValue(
069: ServiceRecordImpl.PROTOCOL_DESCRIPTOR_LIST,
070: protocolList);
071: }
072: } catch (BluetoothStateException e) {
073: throw new IOException(e.getMessage());
074: }
075: }
076:
077: /**
078: * Accepts client connection to the service this notifier is assigned to.
079: *
080: * @return connection to a client just accepted on transport layer.
081: * @exception IOException if an error occured on transport layer.
082: */
083: public ObexTransport acceptAndOpen() throws IOException {
084: return createTransportConnection((StreamConnection) (notifier
085: .acceptAndOpen()));
086: }
087:
088: /**
089: * Closes this notifier on the transport layer
090: * @exception IOException if an error occured on transport layer
091: */
092: public void close() throws IOException {
093: notifier.close();
094: }
095:
096: /**
097: * Create btgoep transport connection.
098: * @param sock transport connection
099: * @return BTGOEP Connection
100: */
101: protected BTGOEPConnection createTransportConnection(
102: StreamConnection sock) throws IOException {
103: return new BTGOEPConnection(sock);
104: }
105:
106: /**
107: * Get transport connection noifier
108: * @return transport notifier
109: */
110: public Connection getUnderlyingConnection() {
111: return notifier;
112: }
113: }
|