通过蓝牙传输文件
问题描述:
是否可以通过蓝牙将文件从一个Android手机传输到任何其他设备? 如果可能的话发送给我一个链接的代码示例...通过蓝牙传输文件
switch (msg.what) {
case MESSAGE_STATE_CHANGE:
if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
switch (msg.arg1) {
case BluetoothChatService.STATE_CONNECTED:
mTitle.setText(R.string.title_connected_to);
mTitle.append(mConnectedDeviceName);
mConversationArrayAdapter.clear();
break;
case BluetoothChatService.STATE_CONNECTING:
mTitle.setText(R.string.title_connecting);
break;
case BluetoothChatService.STATE_LISTEN:
case BluetoothChatService.STATE_NONE:
mTitle.setText(R.string.title_not_connected);
break;
}
break;
case MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
mConversationArrayAdapter.add("Me: " + writeMessage);
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
mConversationArrayAdapter.add(mConnectedDeviceName+": " + readMessage);
break;
case MESSAGE_DEVICE_NAME:
// save the connected device's name
mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
Toast.makeText(getApplicationContext(), "Connected to "
+ mConnectedDeviceName, Toast.LENGTH_SHORT).show();
break;
case MESSAGE_TOAST:
Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
Toast.LENGTH_SHORT).show();
break;
答
在传输文件的情况下,你可以使用意图做出明确调用ACTION_SEND
,如下图所示。
您可以将文件通过OBEX一对夫妇的方式发送到配对的设备:
随着ACTION_SEND
意图,即会弹出一个菜单,可以处理你要发送的文件类型的应用程序,从用户需要选择蓝牙,然后是设备。
Intent i = new Intent(Intent.ACTION_SEND); i.setType("image/jpeg");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("/sdcard/file.jpg"));
startActivity(Intent.createChooser(i, "Send Image"));
有关详情,请访问
请首先通过http://stackoverflow.com/faq#howtoask知道如何要求与正确的格式问题。至少你的代码是否可读? – Rajkiran 2012-03-22 05:25:05
@rajkiran给我一些建议 – Venkat 2012-03-22 05:34:00