I am having some difficulty sending files over Bluetooth. After it attempts to send the file, it will list the transfer as having 'failed' with an 'unknown file' error. I have double-checked my file path but am still having this problem. Do you guys see anything that I am missing? The target phone that is supposed to receive the file isn't showing the incoming file notification that asks the user to accept it or not. I believe this is where the failure is. Do you guys know how to pass the 'permission asking' (I guess we can call it that) to the target device?
//some code used from
// http://examples.javacodegeeks.com/android/core/ui/progressdialog/android-progressdialog-example/
// http://developer.android.com/guide/topics/connectivity/bluetooth.html
package com.project.BluetoothTransfer_v1000;
import java.io.File;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
@SuppressLint("DefaultLocale")
public class TransferFragment extends Fragment{
private TextView filePathTextView;
private Button startTransferButton;
private ImageView bluetoothImage;
ProgressDialog transferDialog;
Handler updateBarHandler;
private static final int REQUEST_BLUETOOTH = 1;
private static final int DISCOVER_DURATION = 300;
Context context;
ArrayAdapter mArrayAdapter;
long timeCheckStart = 0;
long timeCheckEnd = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, final Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//set the user interface layout for this activity
setRetainInstance(false);
View v = inflater.inflate(R.layout.activity_bluetooth_transfer, parent, false);
context = this.getActivity();
filePathTextView = (TextView) v.findViewById(R.id.file_path_textView);
startTransferButton = (Button) v.findViewById(R.id.start_transfer_button);
bluetoothImage = (ImageView) v.findViewById(R.id.bluetooth_imageView);
bluetoothImage.setClickable(true);
startTransferButton.setOnClickListener(new View.OnClickListener() {
//start transfer processes
@Override
public void onClick(View v){
//check to make sure the file path text view != null
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
int REQUEST_ENABLE_BT = -1;
//ensure the device being used has bluetooth capability
if (filePathTextView.getText().toString().length() > 4 && btAdapter != null){
//check-enable bluetooth
if (!btAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
//make the device discoverable and check to make sure device isn't already discoverable
if (timeCheckStart == 0 || System.currentTimeMillis() - 60000 > timeCheckStart){
timeCheckStart = System.currentTimeMillis();
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 60);
startActivity(discoverableIntent);
}
// /storage/emulated/0/Test.jpg
// /storage/extSdCard/Test.jpg
String filePath = filePathTextView.getText().toString();
Toast.makeText(context, filePath, Toast.LENGTH_LONG);
String fileType = filePath.substring(filePath.length()-3,filePath.length()).toLowerCase();
//handles the sending of different file types
//@@@@@@@@@@@@@@@@@@ where im having trouble @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
switch (fileType){
case "jpg": //allow to fall through to png
case "png": Intent pictureIntent = new Intent(Intent.ACTION_SEND);
pictureIntent.setType("image/*");
pictureIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(filePath));
startActivity(Intent.createChooser(pictureIntent, "Send Image"));
break;
case "mp3": Intent audioIntent = new Intent(Intent.ACTION_SEND);
audioIntent.setType("audio/*");
audioIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(filePath));
startActivity(Intent.createChooser(audioIntent, "Send Audio"));
break;
case "txt": Intent textIntent = new Intent(Intent.ACTION_SEND);
textIntent.setType("text/*");
textIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(filePath));
startActivity(Intent.createChooser(textIntent, "Send Text"));
break;
default: new AlertDialog.Builder(context).setTitle("Alert")
.setMessage("The file type submitted is not supported: ("+fileType+")")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
}).show(); break;
}//end fileType switch
}//if text view null (if)
else {
//alert user to input file path
new AlertDialog.Builder(context).setTitle("Error")
.setMessage("Please insert a filename to send and be sure to include the type extension.")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
}).show();
}//bt equipped/text view null check (else)
}//end anon class
});
bluetoothImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//display dialog showing program specs and creators
new AlertDialog.Builder(context).setTitle("About")
.setMessage("Created by:"+"\n"+ "Hal, Chris, and Roger")
.setPositiveButton("Awesome!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
});
return v;
}
}