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 java.io.DataInputStream; 21 import java.io.DataOutputStream; 22 import java.io.IOException; 23 import java.io.InputStream; 24 import java.io.OutputStream; 25 26 import javax.obex.ObexTransport; 27 28 /** 29 * Generic Obex Transport class, to be used in OBEX based Bluetooth 30 * Profiles. 31 */ 32 public class BluetoothObexTransport implements ObexTransport { 33 private BluetoothSocket mSocket = null; 34 35 /** 36 * Will default at the maximum packet length. 37 */ 38 public static final int PACKET_SIZE_UNSPECIFIED = -1; 39 40 private int mMaxTransmitPacketSize = PACKET_SIZE_UNSPECIFIED; 41 private int mMaxReceivePacketSize = PACKET_SIZE_UNSPECIFIED; 42 BluetoothObexTransport(BluetoothSocket socket)43 public BluetoothObexTransport(BluetoothSocket socket) { 44 this.mSocket = socket; 45 } 46 BluetoothObexTransport(BluetoothSocket socket, int transmitSize, int receiveSize)47 public BluetoothObexTransport(BluetoothSocket socket, int transmitSize, int receiveSize) { 48 this.mSocket = socket; 49 this.mMaxTransmitPacketSize = transmitSize; 50 this.mMaxReceivePacketSize = receiveSize; 51 } 52 53 @Override close()54 public void close() throws IOException { 55 mSocket.close(); 56 } 57 58 @Override openDataInputStream()59 public DataInputStream openDataInputStream() throws IOException { 60 return new DataInputStream(openInputStream()); 61 } 62 63 @Override openDataOutputStream()64 public DataOutputStream openDataOutputStream() throws IOException { 65 return new DataOutputStream(openOutputStream()); 66 } 67 68 @Override openInputStream()69 public InputStream openInputStream() throws IOException { 70 return mSocket.getInputStream(); 71 } 72 73 @Override openOutputStream()74 public OutputStream openOutputStream() throws IOException { 75 return mSocket.getOutputStream(); 76 } 77 78 @Override connect()79 public void connect() throws IOException { 80 } 81 82 @Override create()83 public void create() throws IOException { 84 } 85 86 @Override disconnect()87 public void disconnect() throws IOException { 88 } 89 90 @Override listen()91 public void listen() throws IOException { 92 } 93 isConnected()94 public boolean isConnected() throws IOException { 95 return true; 96 } 97 98 @Override getMaxTransmitPacketSize()99 public int getMaxTransmitPacketSize() { 100 if (mSocket.getConnectionType() != BluetoothSocket.TYPE_L2CAP) { 101 return mMaxTransmitPacketSize; 102 } 103 return mSocket.getMaxTransmitPacketSize(); 104 } 105 106 @Override getMaxReceivePacketSize()107 public int getMaxReceivePacketSize() { 108 if (mSocket.getConnectionType() != BluetoothSocket.TYPE_L2CAP) { 109 return mMaxReceivePacketSize; 110 } 111 return mSocket.getMaxReceivePacketSize(); 112 } 113 getRemoteAddress()114 public String getRemoteAddress() { 115 if (mSocket == null) { 116 return null; 117 } 118 return mSocket.getRemoteDevice().getAddress(); 119 } 120 121 @Override isSrmSupported()122 public boolean isSrmSupported() { 123 if (mSocket.getConnectionType() == BluetoothSocket.TYPE_L2CAP) { 124 return true; 125 } 126 return false; 127 } 128 } 129