1 /*
2  * Copyright (C) 2014 Samsung System LSI
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package com.android.bluetooth;
17 
18 import android.bluetooth.BluetoothSocket;
19 
20 import com.android.bluetooth.flags.Flags;
21 import com.android.obex.ObexTransport;
22 
23 import java.io.DataInputStream;
24 import java.io.DataOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 
29 /** Generic Obex Transport class, to be used in OBEX based Bluetooth Profiles. */
30 public class BluetoothObexTransport implements ObexTransport {
31     private BluetoothSocket mSocket = null;
32 
33     /** Will default at the maximum packet length. */
34     public static final int PACKET_SIZE_UNSPECIFIED = -1;
35 
36     private int mMaxTransmitPacketSize = PACKET_SIZE_UNSPECIFIED;
37     private int mMaxReceivePacketSize = PACKET_SIZE_UNSPECIFIED;
38 
39     private boolean mIsCoverArt = false;
40 
BluetoothObexTransport(BluetoothSocket socket)41     public BluetoothObexTransport(BluetoothSocket socket) {
42         this.mSocket = socket;
43     }
44 
BluetoothObexTransport(BluetoothSocket socket, int transmitSize, int receiveSize)45     public BluetoothObexTransport(BluetoothSocket socket, int transmitSize, int receiveSize) {
46         this.mSocket = socket;
47         this.mMaxTransmitPacketSize = transmitSize;
48         this.mMaxReceivePacketSize = receiveSize;
49     }
50 
51     @Override
close()52     public void close() throws IOException {
53         mSocket.close();
54     }
55 
56     @Override
openDataInputStream()57     public DataInputStream openDataInputStream() throws IOException {
58         return new DataInputStream(openInputStream());
59     }
60 
61     @Override
openDataOutputStream()62     public DataOutputStream openDataOutputStream() throws IOException {
63         return new DataOutputStream(openOutputStream());
64     }
65 
66     @Override
openInputStream()67     public InputStream openInputStream() throws IOException {
68         return mSocket.getInputStream();
69     }
70 
71     @Override
openOutputStream()72     public OutputStream openOutputStream() throws IOException {
73         return mSocket.getOutputStream();
74     }
75 
76     @Override
connect()77     public void connect() throws IOException {}
78 
79     @Override
create()80     public void create() throws IOException {}
81 
82     @Override
disconnect()83     public void disconnect() throws IOException {}
84 
85     @Override
listen()86     public void listen() throws IOException {}
87 
isConnected()88     public boolean isConnected() throws IOException {
89         return true;
90     }
91 
92     @Override
getMaxTransmitPacketSize()93     public int getMaxTransmitPacketSize() {
94         if (mSocket.getConnectionType() != BluetoothSocket.TYPE_L2CAP
95                 || (mIsCoverArt && mMaxTransmitPacketSize != PACKET_SIZE_UNSPECIFIED)) {
96             return mMaxTransmitPacketSize;
97         }
98         return mSocket.getMaxTransmitPacketSize();
99     }
100 
101     @Override
getMaxReceivePacketSize()102     public int getMaxReceivePacketSize() {
103         if (mSocket.getConnectionType() != BluetoothSocket.TYPE_L2CAP) {
104             return mMaxReceivePacketSize;
105         }
106         return mSocket.getMaxReceivePacketSize();
107     }
108 
getRemoteAddress()109     public String getRemoteAddress() {
110         if (mSocket == null) {
111             return null;
112         }
113         String identityAddress =
114                 Flags.identityAddressNullIfUnknown()
115                         ? Utils.getBrEdrAddress(mSocket.getRemoteDevice())
116                         : mSocket.getRemoteDevice().getIdentityAddress();
117         return mSocket.getConnectionType() == BluetoothSocket.TYPE_RFCOMM
118                 ? identityAddress
119                 : mSocket.getRemoteDevice().getAddress();
120     }
121 
122     @Override
isSrmSupported()123     public boolean isSrmSupported() {
124         if (mSocket.getConnectionType() == BluetoothSocket.TYPE_L2CAP) {
125             return true;
126         }
127         return false;
128     }
129 
setConnectionForCoverArt(boolean isCoverArt)130     public void setConnectionForCoverArt(boolean isCoverArt) {
131         mIsCoverArt = isCoverArt;
132     }
133 }
134