简体   繁体   中英

Bluetooth file transfer for Xamarin.Android

I'm trying to connect and send a file from android device to a PC or another smartphone with Xamarin.Android via Bluetooth. Connection is estabilished, but it doesn't send the file. It doesn't seems to work since there are no exceptions.

int bufferSize = (int)sourceStream.Length;
byte[] byteArray=File.ReadAllBytes("/sdcard/test.txt");

BluetoothSocket socket = device.CreateInsecureRfcommSocketToServiceRecord(UUID.FromString("00001105-0000-1000-8000-00805f9b34fb"));

try
{
   await socket.ConnectAsync();
   Stream oStream = socket.OutputStream;
   oStream.Write(byteArray, 0, bufferSize); 
}
catch (Exception ex)
{
   //some catching
}

Beside that, do you know any tutorial out there?

I do not know what your receiving code looks like, but you can use the built-in Android BlueTooth Intent.ActionSend Sharing app to start the transfer:

var photoAsset = Assets.OpenFd ("BusinessCard.png");
var javaIOFile = new Java.IO.File (photoAsset.ToString ());
var sendIntent = new Intent (Intent.ActionSend);
sendIntent.SetType ("image/*");
sendIntent.SetComponent (new ComponentName ("com.android.bluetooth", "com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
sendIntent.PutExtra (Intent.ExtraStream, Android.Net.Uri.FromFile (javaIOFile));
StartActivity (sendIntent);     

Of course the receiver would have to have their BlueTooth on and accept the connection/transfer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM