2011-11-18 60 views

回答

2

這很奇怪的是Android有沒有明確的OBEX API兩種權限。使用OBEX


用於文件共享或者您可以使用this解決方案

BluetoothDevice device; 
String filePath = Environment.getExternalStorageDirectory().toString() + "/file.jpg"; 

ContentValues values = new ContentValues(); 
values.put(BluetoothShare.URI, Uri.fromFile(new File(filePath)).toString()); 
values.put(BluetoothShare.DESTINATION, device.getAddress()); 
values.put(BluetoothShare.DIRECTION, BluetoothShare.DIRECTION_OUTBOUND); 
Long ts = System.currentTimeMillis(); 
values.put(BluetoothShare.TIMESTAMP, ts); 
Uri contentUri = getContentResolver().insert(BluetoothShare.CONTENT_URI, values); 

(它需要this class -

+0

您的意思是[API鏈接](http://developer.android.com/reference/android/net/Uri.html#fromFile(java.io.File))? – Reno

+0

@ Reno ..我在我的應用程序中嘗試了上面的代碼片段,但它不會將文件發送到目標設備。我們是否需要像下面那樣開始意圖。什麼是文件格式可以支持藍牙共享。 –

+0

@Reno我讀了你的解決方案真的很棒,我已經選擇了它作爲有用的,但我需要一些建議,實際上在我的應用程序,我必須連接配對的藍牙設備來打印圖像...告訴我哪個將是最好的方法要做到這一點..? – Sun

4

這是一個小功能,您可以使用

/** 
    * Method to share data via bluetooth 
    * */ 
    public void bluetoothFunctionality() { 
     String path = Environment.getExternalStorageDirectory() + "/" 
       + Config.FILENAME; 

     File file = new File(path); 

     Intent intent = new Intent(); 
     intent.setAction(Intent.ACTION_SEND); 
     intent.setType("text/plain"); 
     intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); 
     startActivity(intent); 
    } 

此方法將文件發送到使用默認設備藍牙功能的其他裝置。 在你做這個之前,你必須首先配對這個設備,這是有限制的。 發送不同類型的文件,你必須只改變MIME類型集合類型的方法

在清單文件必須添加像

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
    <uses-permission android:name="android.permission.BLUETOOTH" /> 
+3

如果我沒有錯,那些權限不是必需的。 – xmen