2

我正在使用 Sample Bluetooth Chat android,它可以正常聊天。现在我想使用该代码传输文件。

这就是我想要做的:首先,用户向服务器发送一个文件名。然后,服务器使用该代码发回该文件。

       ContentValues values = new ContentValues();
       values.put(BluetoothShare.URI, "file:///sdcard/refresh.txt");
       values.put(BluetoothShare.DESTINATION, deviceAddress);
       values.put(BluetoothShare.DIRECTION, BluetoothShare.DIRECTION_OUTBOUND);
       Long ts = System.currentTimeMillis();
       values.put(BluetoothShare.TIMESTAMP, ts);
       getContentResolver().insert(BluetoothShare.CONTENT_URI, values);

但我这样做会收到套接字错误。你能给我推荐一个关于android文件传输的教程或示例代码吗?

4

1 回答 1

1

为了传输文件,您可以使用意图显式调用 ACTION_SEND

使用 ACTION_SEND 意图,将弹出一个菜单,其中包含可以处理您要发送的文件类型的应用程序,用户需要从中选择蓝牙,然后选择设备。

File sourceFile = new File("//mnt/sdcard/TviderFB.apk"); 
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
Intent.setType("image/jpeg"); 
intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(sourceFile));
startActivity(intent);
于 2012-12-01T06:01:34.070 回答