Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluetooth not sending file to other device

It is already asked but i didn't find any solution. For bluetooth application i am using the bluetoothShare.class .

My source code for sending the file to the target device

MainActvity.class:

Set<BluetoothDevice> devices = btAdapter
                            .getBondedDevices();
                    final String btDeviceName = selected_deviceName;
                    BluetoothDevice device = null;

                    for (BluetoothDevice itDevice : devices) {
                        if (btDeviceName.equals(itDevice.getName())) {
                            device = itDevice;
                        }
                    }

                    if (device != null) {

                        ContentValues values = new ContentValues();
                        values.put(BluetoothShare.URI, uri.toString());
                        values.put(BluetoothShare.MIMETYPE, "image/jpeg");
                        values.put(BluetoothShare.DESTINATION,
                                device.getAddress());
                        values.put(BluetoothShare.DIRECTION,
                                BluetoothShare.DIRECTION_OUTBOUND);
                        Long ts = System.currentTimeMillis();
                        values.put(BluetoothShare.TIMESTAMP, ts);
                        final Uri contentUri = getApplicationContext()
                                .getContentResolver().insert(
                                        BluetoothShare.CONTENT_URI, values);
                        Log.v(TAG, "Insert contentUri: " + contentUri
                                + "  to device: " + device.getName());
                        Toast.makeText(getApplicationContext(), "Success",
                                Toast.LENGTH_LONG).show();
                    } else {
                        textStatus
                                .setText("Bluetooth remote device not found");
                    }
                } else {
                    textStatus.setText("Bluetooth not activated");
                }
            }

            else {
                Toast.makeText(getApplicationContext(), "No devices found",
                        Toast.LENGTH_LONG).show();
            }

and the blueToothShare.class:

package process.bluetooth.sendfile.opp;

import android.net.Uri;
import android.provider.BaseColumns;


public final class BluetoothShare implements BaseColumns {
private BluetoothShare() {
}

public static final String PERMISSION_ACCESS = "android.permission.ACCESS_BLUETOOTH_SHARE";

public static final Uri CONTENT_URI = Uri
        .parse("content://com.android.bluetooth.opp/btopp");

public static final String TRANSFER_COMPLETED_ACTION = "android.btopp.intent.action.TRANSFER_COMPLETE";

public static final String INCOMING_FILE_CONFIRMATION_REQUEST_ACTION = "android.btopp.intent.action.INCOMING_FILE_NOTIFICATION";

public static final String USER_CONFIRMATION_TIMEOUT_ACTION = "android.btopp.intent.action.USER_CONFIRMATION_TIMEOUT";

public static final String URI = "uri";

public static final String FILENAME_HINT = "hint";

public static final String _DATA = "_data";

public static final String MIMETYPE = "mimetype";

public static final String DIRECTION = "direction";

public static final String DESTINATION = "destination";

public static final String VISIBILITY = "visibility";

public static final String USER_CONFIRMATION = "confirm";

public static final String STATUS = "status";

public static final String TOTAL_BYTES = "total_bytes";

public static final String CURRENT_BYTES = "current_bytes";

public static final String TIMESTAMP = "timestamp";

public static final int DIRECTION_OUTBOUND = 0;

public static final int DIRECTION_INBOUND = 1;

public static final int USER_CONFIRMATION_PENDING = 0;

public static final int USER_CONFIRMATION_CONFIRMED = 1;

public static final int USER_CONFIRMATION_AUTO_CONFIRMED = 2;

public static final int USER_CONFIRMATION_DENIED = 3;

public static final int USER_CONFIRMATION_TIMEOUT = 4;

public static final int VISIBILITY_VISIBLE = 0;

public static final int VISIBILITY_HIDDEN = 1;

public static boolean isStatusInformational(int status) {
    return (status >= 100 && status < 200);
}

public static boolean isStatusSuspended(int status) {
    return (status == STATUS_PENDING);
}

public static boolean isStatusSuccess(int status) {
    return (status >= 200 && status < 300);
}

public static boolean isStatusError(int status) {
    return (status >= 400 && status < 600);
}

public static boolean isStatusClientError(int status) {
    return (status >= 400 && status < 500);
}

public static boolean isStatusServerError(int status) {
    return (status >= 500 && status < 600);
}

public static boolean isStatusCompleted(int status) {
    return (status >= 200 && status < 300)
            || (status >= 400 && status < 600);
}

public static final int STATUS_PENDING = 190;

public static final int STATUS_RUNNING = 192;

public static final int STATUS_SUCCESS = 200;

public static final int STATUS_BAD_REQUEST = 400;

public static final int STATUS_FORBIDDEN = 403;

public static final int STATUS_NOT_ACCEPTABLE = 406;

public static final int STATUS_LENGTH_REQUIRED = 411;

public static final int STATUS_PRECONDITION_FAILED = 412;

public static final int STATUS_CANCELED = 490;

public static final int STATUS_UNKNOWN_ERROR = 491;

public static final int STATUS_FILE_ERROR = 492;

public static final int STATUS_ERROR_NO_SDCARD = 493;

public static final int STATUS_ERROR_SDCARD_FULL = 494;

public static final int STATUS_UNHANDLED_OBEX_CODE = 495;

public static final int STATUS_OBEX_DATA_ERROR = 496;

public static final int STATUS_CONNECTION_ERROR = 497;

}
like image 882
Arut Avatar asked Aug 28 '13 09:08

Arut


People also ask

Why are my Bluetooth files not sending?

1. Before pairing and connecting your devices, make sure that Bluetooth is enabled on both devices, and that both devices are discoverable and visible to other nearby Bluetooth devices (for laptops, check that the file receiving screen is displayed). 2.

Why my laptop is not receiving files via Bluetooth?

To receive files via Bluetooth on your Windows 10 computer, ensure that the device sending the file is paired/connected to your PC. If you're still unable to receive files, you can remove the device from your PC and reconnect it. Navigate to Settings > Devices and tap the device you want to receive files from.


2 Answers

BluetoothShare class not supported android 4.1 and above. you can use the following intent coding to send file in android version 4.1 and above

Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.setComponent(new ComponentName(
        "com.android.bluetooth",
        "com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
    intent.setType("image/jpeg");
    file = new File(filepath);
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
    startActivity(intent);

and also some devices in Android v 2.2/2.3 not send the file via bluetoothShare class.

like image 173
Aravin Avatar answered Nov 04 '22 13:11

Aravin


Maybe you can try another solution with BufferedWriter and BufferedReader.

Here is a snipped code:

            BluetoothDevice mmDevice;
            Set<BluetoothDevice> mBluetoothAdapter;

            BluetoothAdapter bAdapter = BluetoothAdapter
                    .getDefaultAdapter();
            mBluetoothAdapter = bAdapter.getBondedDevices();

            for (BluetoothDevice bc : mBluetoothAdapter) {
                if (bc.getName().indexOf("name_of_bluetoothdevide") != -1) {
                    UUID uuid = UUID
                            .fromString("00001101-0000-1000-8000-00805F9B34FB"); // Standard
                                                                                    // SerialPortService
                                                                                    // ID
                    mmDevice = bc;
                    BluetoothSocket mmSocket = mmDevice
                            .createInsecureRfcommSocketToServiceRecord(uuid);
                    bAdapter.cancelDiscovery();
                    mmSocket.connect();
                    BufferedWriter Writer = new BufferedWriter(
                            new OutputStreamWriter(
                                    mmSocket.getOutputStream()));
                    Writer.write("Bluetooth connected!");
                    Writer.flush();

                    app.setmSocket(mmSocket);

                    break;
                }
            }

And for reading:

  BufferedReader Reader = new BufferedReader(
                    new InputStreamReader(mmSocket.getInputStream()));
            receivedMsg = Reader.readLine();

Hope it can help you.

like image 36
silvia_aut Avatar answered Nov 04 '22 14:11

silvia_aut