2011-11-18 58 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