summaryrefslogtreecommitdiff
path: root/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothFinder.java
blob: dc0db45744fcde6356677eb03b13418b15ecd0ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.libreoffice.impressremote.communication;

import java.util.Collection;
import java.util.HashMap;

import org.libreoffice.impressremote.Globals;
import org.libreoffice.impressremote.communication.Server.Protocol;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

public class BluetoothFinder {

    // TODO: add removal of cached items
    private Context mContext;

    BluetoothAdapter mAdapter;

    public BluetoothFinder(Context aContext) {
        mContext = aContext;
        mAdapter = BluetoothAdapter.getDefaultAdapter();

    }

    public void startFinding() {
        if (mAdapter == null) {
            return; // No bluetooth adapter found (emulator, special devices)
        }
        IntentFilter aFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        aFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        aFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        mContext.registerReceiver(mReceiver, aFilter);
        mAdapter.startDiscovery();
    }

    public void stopFinding() {
        if (mAdapter == null) {
            return; // No bluetooth adapter found (emulator, special devices)
        }
        mAdapter.cancelDiscovery();
        try {
            mContext.unregisterReceiver(mReceiver);
        } catch (IllegalArgumentException e) {
            // The receiver wasn't registered
        }
    }

    private HashMap<String, Server> mServerList = new HashMap<String, Server>();

    public Collection<Server> getServerList() {
        return mServerList.values();
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent aIntent) {
            if (aIntent.getAction().equals(BluetoothDevice.ACTION_FOUND)) {
                BluetoothDevice aDevice = (BluetoothDevice) aIntent.getExtras()
                                .get(BluetoothDevice.EXTRA_DEVICE);
                Log.i(Globals.TAG, "BluetoothFinder.onReceive: found " + aDevice.getName() + " at " + aDevice.getAddress());
                Server aServer = new Server(Protocol.BLUETOOTH,
                                aDevice.getAddress(), aDevice.getName(),
                                System.currentTimeMillis());
                mServerList.put(aServer.getAddress(), aServer);
                Intent aNIntent = new Intent(
                                CommunicationService.MSG_SERVERLIST_CHANGED);
                LocalBroadcastManager.getInstance(mContext).sendBroadcast(
                                aNIntent);
            } else if (aIntent.getAction().equals(
                            BluetoothAdapter.ACTION_DISCOVERY_FINISHED)
                            || aIntent.getAction()
                                            .equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                // Start discovery again after a small delay.
                // but check whether device is on incase the user manually
                // disabled bluetooth
                if (mAdapter.isEnabled()) {
                    Handler aHandler = new Handler();
                    aHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            // Looping, huh?
                            Log.i(Globals.TAG, "BluetothFinder: looping");
                        }
                    }, 1000 * 15);
                }
            }

        }

    };
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */