2つのデバイス間でファイル内のデータを転送する必要があるアプリを開発しています。
Bluetoothチャットに関するチュートリアルを見ましたが、ファイル転送に必要なものがわかりません。これどうやってするの?
ファイル転送は、メッセージを送信するのと同じです。最初にファイルを入力ストリームに変換し、次にこれを設立されたデバイスに転送します。
このようなもの:
String addr = args[0];
log("Connecting over bluetooth to " + addr);
bs = null;
try {
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(addr);
mBluetoothAdapter.cancelDiscovery();
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
bs = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));
bs.connect();
} catch (Exception e) {
return false;
}
接続するデバイスのMACアドレスが必要です。次に、のInputStreamとOutputStreamを使用しbs
ます。
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
String uri = "/mnt/sdcard/test.jpg";
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(uri)));
startActivity(intent);
このコードをボタンに設定し、uri をファイル パスに設定してから、ボタンを押してファイルを転送してください。