My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2010 Mina Shokry
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @version $Id$
*/

package com.intel.bluetooth;

import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
*
* @author Mina Shokry
*/
public class AndroidBluetoothConnection {
private static volatile long nextHandle;
private static Map<Long, AndroidBluetoothConnection> connectionsMap;

static {
nextHandle = 1;
connectionsMap = new HashMap<Long, AndroidBluetoothConnection>();
}

private BluetoothSocket socket;
private BluetoothServerSocket serverSocket;
private InputStream inputStream;
private OutputStream outputStream;
private long handle;

private AndroidBluetoothConnection(long handle, BluetoothSocket socket, boolean isServer) throws IOException {
this.handle = handle;
this.socket = socket;
if (!isServer) {
socket.connect();
}
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
}

private AndroidBluetoothConnection(long handle, BluetoothServerSocket serverSocket) {
this.handle = handle;
this.serverSocket = serverSocket;
}

public synchronized static AndroidBluetoothConnection createConnection(BluetoothSocket socket, boolean isServer) throws IOException {
AndroidBluetoothConnection bluetoothConnection = new AndroidBluetoothConnection(nextHandle, socket, isServer);
connectionsMap.put(nextHandle, bluetoothConnection);

nextHandle ++;

return bluetoothConnection;
}

public synchronized static AndroidBluetoothConnection createServerConnection(BluetoothServerSocket serverSocket) {
AndroidBluetoothConnection bluetoothConnection = new AndroidBluetoothConnection(nextHandle, serverSocket);
connectionsMap.put(nextHandle, bluetoothConnection);

nextHandle ++;

return bluetoothConnection;
}


public static AndroidBluetoothConnection getBluetoothConnection(long handle) {
return connectionsMap.get(handle);
}

public long getHandle() {
return handle;
}

public BluetoothSocket getSocket() {
return socket;
}

public BluetoothServerSocket getServerSocket() {
return serverSocket;
}

public InputStream getInputStream() {
return inputStream;
}

public OutputStream getOutputStream() {
return outputStream;
}

public void close() throws IOException {
if (outputStream != null) {
outputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
if (socket != null) {
socket.close();
}
if (serverSocket != null) {
serverSocket.close();
}
connectionsMap.remove(handle);
}
}

Change log

r3063 by minashokry on Nov 27, 2010   Diff
fix the error of opening the bluetooth
socket twice on server connection
Go to: 
Project members, sign in to write a code review

Older revisions

r3056 by minashokry on Aug 14, 2010   Diff
Implementation of rfcomm server
connections in android2 module
r3053 by minashokry on Jul 31, 2010   Diff
finish service discovery and implement
rfcomm connections
All revisions of this file

File info

Size: 3512 bytes, 127 lines
Powered by Google Project Hosting