Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to accept bluetooth received file in Android application?

Tags:

I would like to implement an application to receive a file from a Bluetooth device.

Before receiving, a notification will be raised to accept an incoming file request.

From there, i would like to activate "accept" and download the file automatically without raising an accept dialog when the user receive a second file from another Bluetooth paired device, without notification disturbance when the user launchs an application.

like image 920
prasad.gai Avatar asked Apr 11 '12 09:04

prasad.gai


2 Answers

I developed an app that include this kind of task, and you can use BluetoothChat example. You must set the secure flag to false: ` boolean secure = false;

        try {
            if (secure) {
                tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE,
                    MY_UUID_SECURE);
            } else {
                tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(
                        NAME_INSECURE, MY_UUID_INSECURE);
            }
        } catch (IOException e) {
            Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e);

        mmServerSocket = tmp;
    }`

And then read the buffer from the InputStream that you can find in ConnectedThread:

while (true) {
            try {

                bytes = mmInStream.read(buffer);
                 /*write bytes in a file*/


            } catch (IOException e) {
                Log.e(TAG, "disconnected", e);
                connectionLost();

                BluetoothChatService.this.start();
                break;
            }
        }
like image 95
VidaLux Avatar answered Oct 20 '22 06:10

VidaLux


You can try using the Bluetooth socket connection to set a client server TCP like connection.

like image 27
Anurag Ramdasan Avatar answered Oct 20 '22 07:10

Anurag Ramdasan