Developer MJ Story

[android/BT] OPP(Object Push Profile)를 활용한 file 전송 본문

Software/Android

[android/BT] OPP(Object Push Profile)를 활용한 file 전송

집근처 2014. 7. 5. 16:24
반응형

android에서는 다양한 Bluetooth profile을 지원합니다.

그 중에서 오늘 알아볼 profile은 OPP(Object Push Profile)입니다.

Push라는 이름에서도 알 수 있듯이 단 방향 profile 입니다.

그 대신 사전pairing이 되어 있지 않아도 file을 바로push 할 수 있는 장점이 있습니다.

그래서 android내부 application에서 phone 간 파일을 공유할 때 사용하는 profile입니다.

아래 그림은 Gallery에서 사용하는 모습입니다.

 

 

OPP는 어떤 이유에서인지는 몰라도 anroid에서 개발자가 직접 BluetoothOppService에 접근할 수 없습니다.

그래서 우회적인 방법으로 Bluetooth OPP를 사용해야 합니다.

 

그렇다고 전혀 어렵지 않습니다.

물론 방법을 모르면…………………..답이 없는거죠??ㅋ

 

아래와 같은 함수 하나면 원하는 파일을 Bluetooth OPP로 전송 가능합니다.

그냥 intent 로 BluetoothOppService에 event를 전달하는 것입니다.

private void BtOppConnect(){

    Intent sharingIntent = new Intent(Intent.ACTION_SEND);

    File file = new File("/storage/emulated/0/Download/1.jpg");

    sharingIntent.setType("image/*");

    sharingIntent.setPackage("com.android.bluetooth");

    sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));

    startActivity(Intent.createChooser(sharingIntent, "Share file"));

Log.d(TAG, "sendBroadcast");

}

 

위와 같이 intent를 던져 주면 아래 사진과 같이 BT device를 선택하는 화면이 나옴니다.

이때, 타겟 device를 선택하면 타겟 device로 Download/1.jpg라는 파일이 OPP를 통해 전송됩니다.

 

 

Android 내부적으로는 보다 복잡한 과정이 있지만, 복잡하므로 생략하겠습니다.

궁금하신 사항은 덧글로 남겨 주세요.

반응형