Source Code Cross Referenced for GenericBluetoothStack.java in  » 6.0-JDK-Modules » j2me » com » sun » midp » jsr082 » bluetooth » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » com.sun.midp.jsr082.bluetooth 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:
027:        package com.sun.midp.jsr082.bluetooth;
028:
029:        import com.sun.midp.jsr082.BluetoothUtils;
030:
031:        /**
032:         * BluetoothStack implementation which relies on HCI command/event flow.
033:         */
034:        public class GenericBluetoothStack extends BluetoothStack {
035:
036:            /** Maximum HCI packet size as defined by Bluetooth 1.1 specification. */
037:            private final int MAX_HCI_PACKET_SIZE = 257;
038:            /** HCI Inquiry Complete event code. */
039:            private final int HCI_INQUIRY_COMPLETE = 0x01;
040:            /** HCI Inquiry Result event code. */
041:            private final int HCI_INQUIRY_RESULT = 0x02;
042:            /** HCI Authentication Complete event code. */
043:            private final int HCI_AUTH_COMPLETE = 0x06;
044:            /** HCI Remote Name Request Complete event code. */
045:            private final int HCI_NAME_COMPLETE = 0x07;
046:            /** HCI Encrypt Change event code. */
047:            private final int HCI_ENCRYPT_CHANGE = 0x08;
048:
049:            /** Internal byte array for storing incoming HCI events. */
050:            private byte[] buffer = new byte[MAX_HCI_PACKET_SIZE];
051:
052:            /**
053:             * Extracts Bluetooth address from the internal buffer.
054:             *
055:             * @param offset offset in the internal buffer
056:             * @return Bluetooth address consisting of 12 hexadecimal characters
057:             */
058:            protected String extractAddress(int offset) {
059:                byte[] addr = new byte[BluetoothUtils.BTADDR_SIZE];
060:                System.arraycopy(buffer, offset, addr, 0,
061:                        BluetoothUtils.BTADDR_SIZE);
062:                return BluetoothUtils.getAddressString(addr);
063:            }
064:
065:            /**
066:             * Extracts string (e.g. friendly name) from the internal buffer. The
067:             * string is stored in UTF-8 format and is terminated by NULL character.
068:             *
069:             * @param offset offset in the internal buffer
070:             * @return Java string retrieved from binary event data
071:             */
072:            protected String extractString(int offset) {
073:                int length = 0;
074:                while (offset + length < MAX_HCI_PACKET_SIZE
075:                        && buffer[offset + length] != 0) {
076:                    length++;
077:                }
078:                // IMPL_NOTE: UTF-8 encoding support for Java strings?
079:                //      return new String(buffer, offset, length, "UTF-8");
080:                return stringUTF8(buffer, offset, length);
081:            }
082:
083:            /**
084:             * Extracts 2 octets from the internal buffer.
085:             *
086:             * @param offset offset in the internal buffer
087:             * @return 16 bits of data from the given offset
088:             */
089:            protected int extractShort(int offset) {
090:                return ((int) buffer[offset] & 0xff)
091:                        | (((int) buffer[offset + 1] & 0xff) << 8);
092:            }
093:
094:            /**
095:             * Retrieves Bluetooth event from HCI event data containing in the
096:             * internal buffer.
097:             *
098:             * @return BluetoothEvent subclass instance
099:             */
100:            protected BluetoothEvent retrieveEvent() {
101:                readData(buffer);
102:                int code = buffer[0];
103:                int len = buffer[1];
104:                switch (code) {
105:                case HCI_INQUIRY_COMPLETE:
106:                    return new InquiryCompleteEvent(buffer[2] == 0);
107:                case HCI_INQUIRY_RESULT:
108:                    int num = buffer[2];
109:                    int offset = 3;
110:                    InquiryResult[] results = new InquiryResult[num];
111:                    for (int i = 0; i < num; i++) {
112:                        String addr = extractAddress(offset);
113:                        int cod = 0;
114:                        for (int j = 0; j < 3; j++) {
115:                            cod |= ((int) buffer[offset + 9 + j] & 0xff) << (16 - j * 8);
116:                        }
117:                        results[i] = new InquiryResult(addr, cod);
118:                        offset += 14; // 14 is the size of the response structure
119:                    }
120:                    return new InquiryResultEvent(results);
121:                case HCI_AUTH_COMPLETE:
122:                    return new AuthenticationCompleteEvent(extractShort(3),
123:                            buffer[2] == 0);
124:                case HCI_NAME_COMPLETE:
125:                    return new NameResultEvent(extractAddress(3),
126:                            extractString(9));
127:                case HCI_ENCRYPT_CHANGE:
128:                    return new EncryptionChangeEvent(extractShort(3),
129:                            buffer[2] == 0, buffer[5] == 1);
130:                default:
131:                    return null;
132:                }
133:            }
134:
135:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.