1 /* 2 * Copyright 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.bluetooth.pbap; 18 19 import android.util.Log; 20 21 import com.android.obex.Operation; 22 23 import java.io.IOException; 24 import java.io.OutputStream; 25 26 /** 27 * Handler to emit vCards to PCE. 28 */ 29 public class HandlerForStringBuffer { 30 private static final String TAG = "HandlerForStringBuffer"; 31 32 private final Operation mOperation; 33 private final String mOwnerVCard; 34 35 private OutputStream mOutputStream; 36 HandlerForStringBuffer(Operation op, String ownerVCard)37 public HandlerForStringBuffer(Operation op, String ownerVCard) { 38 mOperation = op; 39 mOwnerVCard = ownerVCard; 40 if (BluetoothPbapService.VERBOSE) { 41 Log.v(TAG, "ownerVCard \n " + mOwnerVCard); 42 } 43 } 44 init()45 public boolean init() { 46 try { 47 mOutputStream = mOperation.openOutputStream(); 48 if (mOwnerVCard != null) { 49 return writeVCard(mOwnerVCard); 50 } 51 return true; 52 } catch (IOException e) { 53 Log.e(TAG, "openOutputStream failed", e); 54 } 55 return false; 56 } 57 writeVCard(String vCard)58 public boolean writeVCard(String vCard) { 59 try { 60 if (vCard != null) { 61 mOutputStream.write(vCard.getBytes()); 62 return true; 63 } 64 } catch (IOException e) { 65 Log.e(TAG, "write failed", e); 66 } 67 return false; 68 } 69 terminate()70 public void terminate() { 71 boolean result = BluetoothPbapObexServer.closeStream(mOutputStream, mOperation); 72 if (BluetoothPbapService.VERBOSE) { 73 if (result) { 74 Log.v(TAG, "closeStream succeeded!"); 75 } else { 76 Log.v(TAG, "closeStream failed!"); 77 } 78 } 79 } 80 } 81