Android Open Source - BluetoothGlass Send Picture To Device






From Project

Back to project page BluetoothGlass.

License

The source code is released under:

GNU General Public License

If you think the Android project BluetoothGlass listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.vicmns.bluetoothglass.client.services;
/* ww w.jav  a  2 s  . c  o  m*/
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import com.vicmns.bluetoothglass.client.bluetooth.SendFileToDeviceTask;

import java.io.File;
import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by Victor Cervantes on 3/19/14.
 */
public class SendPictureToDevice extends Service implements
        SendFileToDeviceTask.ConnectToDeviceListeners {
    private static final String TAG = SendPictureToDevice.class.getSimpleName();
    public static final String DEVICE_MAC_ADDRESS = "device_mac_address";
    public static final String PICTURE_TO_SEND = "picture_to_send";
    private String deviceAddress, filePath;
    private BluetoothAdapter myBt;
    private BluetoothDevice dev;
    private Queue<File> picturesQueue;

    private SendFileToDeviceTask sendFileToDeviceTask;

    @Override
    public void onCreate() {
        if(picturesQueue == null)
            picturesQueue = new LinkedList<File>();

        if(myBt == null)
            myBt = BluetoothAdapter.getDefaultAdapter();

        Log.i(TAG, "Service Started");
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Bundle extras = intent.getExtras();

        if(extras != null) {
            deviceAddress = extras.getString(DEVICE_MAC_ADDRESS);
            filePath = extras.getString(PICTURE_TO_SEND);

            picturesQueue.add(new File(filePath));

            if(myBt == null) return Service.START_NOT_STICKY;

            dev = myBt.getRemoteDevice(deviceAddress);

            startSendingProcess();
        }

        return Service.START_STICKY;
    }


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "Service Stopped");
    }

    private void startSendingProcess() {
        if(sendFileToDeviceTask != null) return;
        try {
            sendFileToDeviceTask = new SendFileToDeviceTask(this, dev, picturesQueue.peek());
            sendFileToDeviceTask.setConnectToDeviceListeners(this);
            sendFileToDeviceTask.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onPrepareFileSending() {
        Log.i(TAG, "Sending File: " + picturesQueue.peek());
    }

    @Override
    public void onFileSent() {
        Log.i(TAG, "File Sent: " + picturesQueue.peek());
        sendFileToDeviceTask = null;
        if(picturesQueue.poll() == null)
            stopSelf();
        else
            startSendingProcess();
    }

    @Override
    public void onConnectionClosed() {
        if(picturesQueue.size() > 0)
            startSendingProcess();
        else
            stopSelf();
    }
}




Java Source Code List

com.vicmns.bluetoothglass.client.MainApplication.java
com.vicmns.bluetoothglass.client.activities.BluetoothClient.java
com.vicmns.bluetoothglass.client.activities.MainActivity.java
com.vicmns.bluetoothglass.client.adapters.BluetoothDevicesAdapter.java
com.vicmns.bluetoothglass.client.bluetooth.SendFileToDeviceTask.java
com.vicmns.bluetoothglass.client.callbacks.CardScrollCallBacks.java
com.vicmns.bluetoothglass.client.data.BluetoothParametersHolder.java
com.vicmns.bluetoothglass.client.models.BluetoothDeviceModel.java
com.vicmns.bluetoothglass.client.services.GoogleVoiceTriggerService.java
com.vicmns.bluetoothglass.client.services.SendPictureToDevice.java
com.vicmns.bluetoothglass.client.tools.FileExtensionFilter.java
com.vicmns.bluetoothglass.client.views.CameraView.java
com.vicmns.bluetoothglass.client.views.OverlayView.java
com.vicmns.bluetoothglass.server.MainActivity.java
com.vicmns.bluetoothglass.server.MainApplication.java
com.vicmns.bluetoothglass.server.data.BluetoothParametersHolder.java
com.vicmns.bluetoothglass.server.handlers.BluetoothConnectionHandler.java
com.vicmns.bluetoothglass.server.handlers.BluetoothReadFromSocketHandler.java
com.vicmns.bluetoothglass.server.receivers.DeviceBootReceiver.java
com.vicmns.bluetoothglass.server.service.BluetoothService.java