blob: 60cf3df9d289d4b761d61323add887a07855d3d7 (
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
/* -*- Mode: Java; 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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 org.libreoffice.impressremote.util.Intents;
import org.libreoffice.impressremote.communication.Server.Protocol;
class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder, Runnable {
private static final int SEARCH_DELAY_IN_MILLISECONDS = 1000 * 10;
private final Context mContext;
private final Map<String, Server> mServers;
public BluetoothServersFinder(Context aContext) {
mContext = aContext;
mServers = new HashMap<String, Server>();
}
@Override
public void startSearch() {
if (!isBluetoothAvailable()) {
return;
}
if (BluetoothAdapter.getDefaultAdapter().isDiscovering()) {
return;
}
setUpSearchResultsReceiver();
BluetoothAdapter.getDefaultAdapter().startDiscovery();
}
private boolean isBluetoothAvailable() {
return BluetoothAdapter.getDefaultAdapter() != null;
}
private void setUpSearchResultsReceiver() {
IntentFilter aSearchResultsFilter = new IntentFilter(
BluetoothDevice.ACTION_FOUND);
aSearchResultsFilter
.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
mContext.registerReceiver(this, aSearchResultsFilter);
}
@Override
public void onReceive(Context aContext, Intent aIntent) {
if (BluetoothDevice.ACTION_FOUND.equals(aIntent.getAction())) {
BluetoothDevice aBluetoothDevice = aIntent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
addServer(buildServer(aBluetoothDevice));
callUpdatingServersList();
return;
}
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
.equals(aIntent.getAction())) {
startDiscoveryDelayed();
}
}
private void addServer(Server aServer) {
mServers.put(aServer.getAddress(), aServer);
}
private Server buildServer(BluetoothDevice aBluetoothDevice) {
String aServerAddress = aBluetoothDevice.getAddress();
String aServerName = aBluetoothDevice.getName();
return new Server(Protocol.BLUETOOTH, aServerAddress, aServerName);
}
private void callUpdatingServersList() {
Intent aIntent = Intents.buildServersListChangedIntent();
LocalBroadcastManager.getInstance(mContext).sendBroadcast(aIntent);
}
private void startDiscoveryDelayed() {
// Start discovery again after a small delay.
// but check whether device is on in case the user manually
// disabled bluetooth
if (!BluetoothAdapter.getDefaultAdapter().isEnabled()) {
return;
}
Handler aHandler = new Handler();
aHandler.postDelayed(this, SEARCH_DELAY_IN_MILLISECONDS);
}
@Override
public void run() {
BluetoothAdapter.getDefaultAdapter().startDiscovery();
}
@Override
public void stopSearch() {
if (!isBluetoothAvailable()) {
return;
}
tearDownSearchResultsReceiver();
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
}
private void tearDownSearchResultsReceiver() {
try {
mContext.unregisterReceiver(this);
} catch (IllegalArgumentException e) {
// Receiver not registered.
// Fixed in Honeycomb: Android’s issue #6191.
}
}
@Override
public List<Server> getServers() {
return new ArrayList<Server>(mServers.values());
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|