aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java
blob: 08c4c1d6f4b37b3263bfe99a214b074bb60a516e (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * Copyright (C) 2010 Herbert von Broeuschmeul
 * Copyright (C) 2010 BluetoothGPS4Droid Project
 * 
 * This file is part of BluetoothGPS4Droid.
 *
 * BluetoothGPS4Droid is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BluetoothGPS4Droid is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with BluetoothGPS4Droid. If not, see <http://www.gnu.org/licenses/>.
 */

package org.broeuschmeul.android.gps.bluetooth.provider;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.broeuschmeul.android.gps.nmea.util.NmeaParser;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.location.GpsStatus.NmeaListener;
import android.os.SystemClock;
import android.util.Log;

public class BlueetoothGpsManager {

	private class ConnectedGps extends Thread {
		private final InputStream in;

		public ConnectedGps(BluetoothSocket socket) {
			InputStream tmpIn = null;
			try {
				tmpIn = socket.getInputStream();
			} catch (IOException e) {
				Log.e("BT test", "error while getting socket streams", e);
			}	
			in = tmpIn;
		}
	
		public void run() {
			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(in,"US-ASCII"));
				String s;
				long now = SystemClock.uptimeMillis();
				long lastRead = now;
				while((enabled) && (now < lastRead+5000 )){
					if (reader.ready()){
						s = reader.readLine();
						Log.e("BT test", "data: "+System.currentTimeMillis()+" "+s + "xxx");
						notifyNmeaSentence(s+"\r\n");
						lastRead = SystemClock.uptimeMillis();
					} else {
						Log.e("BT test", "data: not ready "+System.currentTimeMillis());
						SystemClock.sleep(500);
					}
					now = SystemClock.uptimeMillis();
				}
			} catch (IOException e) {
				Log.e("BT test", "error while getting data", e);
				setMockLocationProviderOutOfService();
			} finally {
				// remove because we want to retry...
				// disable();
			}
		}
	}

	private Service callingService;
	private BluetoothSocket gpsSocket;
	private String gpsDeviceAddress;
	private NmeaParser parser = new NmeaParser(10f);
	private boolean enabled = false;
	private ExecutorService notificationPool;
	private ScheduledExecutorService connectionAndReadingPool;
	private List<NmeaListener> nmeaListeners = Collections.synchronizedList(new LinkedList<NmeaListener>()); 
	private LocationManager locationManager;
	private ConnectedGps connectedGps;
	private Notification connectionProblemNotification;
	private Notification serviceStoppedNotification;
	private Context appContext;
	private NotificationManager notificationManager;

	public BlueetoothGpsManager(Service callingService, String deviceAddress) {
		this.gpsDeviceAddress = deviceAddress;
		this.callingService = callingService;
		this.appContext = callingService.getApplicationContext();
		locationManager = (LocationManager)callingService.getSystemService(Context.LOCATION_SERVICE);
		notificationManager = (NotificationManager)callingService.getSystemService(Context.NOTIFICATION_SERVICE);
		parser.setLocationManager(locationManager);	
		
		connectionProblemNotification = new Notification();
		connectionProblemNotification.icon = R.drawable.icon;
		Intent stopIntent = new Intent(BluetoothGpsProviderService.ACTION_STOP_GPS_PROVIDER);
		// PendingIntent stopPendingIntent = PendingIntent.getService(appContext, 0, stopIntent, PendingIntent.FLAG_CANCEL_CURRENT);
		PendingIntent stopPendingIntent = PendingIntent.getService(appContext, 0, stopIntent, 0);
		connectionProblemNotification.contentIntent = stopPendingIntent;

		serviceStoppedNotification = new Notification();
		serviceStoppedNotification.icon=R.drawable.icon;
		Intent restartIntent = new Intent(BluetoothGpsProviderService.ACTION_START_GPS_PROVIDER);
		PendingIntent restartPendingIntent = PendingIntent.getService(appContext, 0, restartIntent, PendingIntent.FLAG_CANCEL_CURRENT);
		serviceStoppedNotification.setLatestEventInfo(appContext, appContext.getString(R.string.service_closed_because_connection_problem_notification_title), appContext.getString(R.string.service_closed_because_connection_problem_notification_title), restartPendingIntent);


	}

	/**
	 * @return true if the bluetooth GPS is enabled
	 */
	public synchronized boolean isEnabled() {
		return enabled;
	}

	public synchronized boolean enable() {
		notificationManager.cancel(R.string.service_closed_because_connection_problem_notification_title);
		if (! enabled){
			final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
			if (bluetoothAdapter == null) {
				// Device does not support Bluetooth
				Log.e("BT test", "Device does not support Bluetooth");
			} else if (!bluetoothAdapter.isEnabled()) {
				// Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
				//	startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
				Log.e("BT test", "Bluetooth is not enabled");
			} else {
				final BluetoothDevice gpsDevice = bluetoothAdapter.getRemoteDevice(gpsDeviceAddress);
				if (gpsDevice == null){
					Log.e("BT test", "GPS device not found");       	    	
				} else {
					Log.e("BT test", "current device: "+gpsDevice.getName() + " -- " + gpsDevice.getAddress());
					try {
						gpsSocket = gpsDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
					} catch (IOException e) {
						Log.e("BT test", "Error during connection", e);
					}
					if (gpsSocket == null){
						Log.e("BT test", "Error while establishing connection: no socket");
					} else {
						Runnable connectThread = new Runnable() {							
							private int connectionTry=0;
							@Override
							public void run() {
								try {
									connectionTry++;
									Log.e("BT test", "current device: "+gpsDevice.getName() + " -- " + gpsDevice.getAddress());
									try {
										if (gpsSocket != null){
											Log.e("BT test", "trying to close old socket");
											gpsSocket.close();
										}
									} catch (IOException e) {
										Log.e("BT test", "Error during disconnection", e);
									}
									try {
										gpsSocket = gpsDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
									} catch (IOException e) {
										Log.e("BT test", "Error during connection", e);
									}
									if (gpsSocket == null){
										Log.e("BT test", "Error while establishing connection: no socket");
									} else {
										// Cancel discovery because it will slow down the connection
										bluetoothAdapter.cancelDiscovery();
										// we increment the number of connection try
										// Connect the device through the socket. This will block
										// until it succeeds or throws an exception
										gpsSocket.connect();
										// connection obtained so reset the number of connection try
										connectionTry=0;
										notificationManager.cancel(R.string.connection_problem_notification_title);
										connectedGps = new ConnectedGps(gpsSocket);
										connectionAndReadingPool.execute(connectedGps);
									}
								} catch (IOException connectException) {
									// Unable to connect
									Log.e("BT test", "error while connecting to socket", connectException);
								} finally {
									if (connectionTry > 0)
									{
										// Unable to connect
										Log.e("BT test", "Unable to establish connection");
										connectionProblemNotification.when = System.currentTimeMillis();
										String pbMessage = appContext.getResources().getQuantityString(R.plurals.connection_problem_notification, 5-connectionTry, 5-connectionTry);
										connectionProblemNotification.setLatestEventInfo(appContext, 
												appContext.getString(R.string.connection_problem_notification_title), 
												pbMessage, 
												connectionProblemNotification.contentIntent);
										connectionProblemNotification.number = connectionTry;
										notificationManager.notify(R.string.connection_problem_notification_title, connectionProblemNotification);
									}
									// if bluetooth has bean disabled or
									// if two much tries consider that we are enable to connect. So close everything and get out
									if ((!bluetoothAdapter.isEnabled()) || (connectionTry >= 5 )){
										notificationManager.cancel(R.string.connection_problem_notification_title);
										serviceStoppedNotification.when = System.currentTimeMillis();
										notificationManager.notify(R.string.service_closed_because_connection_problem_notification_title, serviceStoppedNotification);
										disable();
									}
								}
							}
						};
						this.enabled = true;
						notificationPool = Executors.newSingleThreadExecutor();
						connectionAndReadingPool = Executors.newSingleThreadScheduledExecutor();
						connectionAndReadingPool.scheduleWithFixedDelay(connectThread, 100, 60000, TimeUnit.MILLISECONDS);
					}
				}
			}
		}
		return this.enabled;
	}

	public synchronized void disable() {
		if (enabled){
			enabled = false;
			if (gpsSocket != null){
				try {
					gpsSocket.close();
				} catch (IOException closeException) {
					Log.e("BT test", "error while closing socket", closeException);
				}
			}
			nmeaListeners.clear();
			disableMockLocationProvider();
			notificationPool.shutdown();
			connectionAndReadingPool.shutdown();
			notificationManager.cancel(R.string.connection_problem_notification_title);
			callingService.stopSelf();
		}
	}

	public void enableMockLocationProvider(String gpsName){
		if (parser != null){
			parser.enableMockLocationProvider(gpsName);
		}
	}

	public void disableMockLocationProvider(){
		if (parser != null){
			parser.disableMockLocationProvider();
		}
	}

	/**
	 * @return the mockGpsEnabled
	 */
	public boolean isMockGpsEnabled() {
		boolean mockGpsEnabled = false;
		if (parser != null){
			mockGpsEnabled = parser.isMockGpsEnabled();
		}
		return mockGpsEnabled;
	}
	/**
	 * @return the mockLocationProvider
	 */
	public String getMockLocationProvider() {
		String  mockLocationProvider = null;
		if (parser != null){
			mockLocationProvider = parser.getMockLocationProvider();
		}
		return mockLocationProvider;
	}

	private void setMockLocationProviderOutOfService(){
		if (parser != null){
			parser.setMockLocationProviderOutOfService();
		}
	}

	public boolean addNmeaListener(NmeaListener listener){
		if (!nmeaListeners.contains(listener)){
			nmeaListeners.add(listener);
		}
		return true;
	}

	public void removeNmeaListener(NmeaListener listener){
		nmeaListeners.remove(listener);
	}

	private void notifyNmeaSentence(final String nmeaSentence){
		if (enabled){
			final String recognizedSentence = parser.parseNmeaSentence(nmeaSentence);
			final long timestamp = System.currentTimeMillis();
			if (recognizedSentence != null){
				Log.e("BT test", "NMEA : "+timestamp+" "+recognizedSentence);
				synchronized(nmeaListeners) {
					for(final NmeaListener listener : nmeaListeners){
						notificationPool.execute(new Runnable(){
							@Override
							public void run() {
								listener.onNmeaReceived(timestamp, nmeaSentence);
							}					 
						});
					}
				}
			}
		}
	}	
}