summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTreeHugger Robot2017-03-23 16:44:36 -0500
committerAndroid (Google) Code Review2017-03-23 16:44:37 -0500
commit3730201787b2cdab9166466f93d88e87e41149a8 (patch)
tree9957a61c37b1cbbfd02a9151a35f00d2cf5b4c0c /obd2-lib
parentefc36bc33992bc81901f119f3a31b4a4137ae949 (diff)
parent7888bdcca6495cf47e6ff63cdc904c4771069406 (diff)
downloadplatform-packages-services-car-3730201787b2cdab9166466f93d88e87e41149a8.tar.gz
platform-packages-services-car-3730201787b2cdab9166466f93d88e87e41149a8.tar.xz
platform-packages-services-car-3730201787b2cdab9166466f93d88e87e41149a8.zip
Merge "Add a Bluetooth-based transport to obd2_lib"
Diffstat (limited to 'obd2-lib')
-rw-r--r--obd2-lib/src/com/android/car/obd2/Obd2Connection.java2
-rw-r--r--obd2-lib/src/com/android/car/obd2/connections/BluetoothConnection.java125
2 files changed, 127 insertions, 0 deletions
diff --git a/obd2-lib/src/com/android/car/obd2/Obd2Connection.java b/obd2-lib/src/com/android/car/obd2/Obd2Connection.java
index 62b070a0..bfdb9c0f 100644
--- a/obd2-lib/src/com/android/car/obd2/Obd2Connection.java
+++ b/obd2-lib/src/com/android/car/obd2/Obd2Connection.java
@@ -36,6 +36,8 @@ public class Obd2Connection {
36 36
37 boolean reconnect(); 37 boolean reconnect();
38 38
39 boolean isConnected();
40
39 InputStream getInputStream(); 41 InputStream getInputStream();
40 42
41 OutputStream getOutputStream(); 43 OutputStream getOutputStream();
diff --git a/obd2-lib/src/com/android/car/obd2/connections/BluetoothConnection.java b/obd2-lib/src/com/android/car/obd2/connections/BluetoothConnection.java
new file mode 100644
index 00000000..2d1a13e1
--- /dev/null
+++ b/obd2-lib/src/com/android/car/obd2/connections/BluetoothConnection.java
@@ -0,0 +1,125 @@
1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.car.obd2.connections;
18
19import android.bluetooth.BluetoothAdapter;
20import android.bluetooth.BluetoothDevice;
21import android.bluetooth.BluetoothSocket;
22import android.util.Log;
23import com.android.car.obd2.Obd2Connection;
24import java.io.IOException;
25import java.io.InputStream;
26import java.io.OutputStream;
27import java.util.Objects;
28import java.util.UUID;
29
30public class BluetoothConnection implements Obd2Connection.UnderlyingTransport {
31
32 /**
33 * This is the well-known UUID for the Bluetooth SPP (Serial Port Profile)
34 */
35 private static final UUID SERIAL_PORT_PROFILE = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
36
37 private final BluetoothDevice mDevice;
38 private BluetoothSocket mSocket = null;
39
40 public static final String TAG = BluetoothConnection.class.getSimpleName();
41
42 public BluetoothConnection(String bluetoothAddress) {
43 this(BluetoothAdapter.getDefaultAdapter().getRemoteDevice(bluetoothAddress));
44 }
45
46 public BluetoothConnection(BluetoothDevice device) {
47 mDevice = Objects.requireNonNull(device);
48 connect();
49 }
50
51 @Override
52 public String getAddress() {
53 return mDevice.getAddress();
54 }
55
56 /**
57 * Establish an RFCOMM connection to the remote device.
58 *
59 * Assumes there is no existing connection.
60 *
61 * This method may take time to return (or even not return in pathological cases).
62 * It is a good idea to wrap it in some kind of Promise-like object.
63 *
64 * @return true if it could connect, false otherwise
65 */
66 private boolean connect() {
67 try {
68 mSocket = mDevice.createRfcommSocketToServiceRecord(SERIAL_PORT_PROFILE);
69 mSocket.connect();
70 } catch (IOException e) {
71 Log.w(TAG, "BluetoothConnection couldn't be established due to an exception: " + e);
72 mSocket = null;
73 return false;
74 }
75 return mSocket.isConnected();
76 }
77
78 @Override
79 public boolean isConnected() {
80 return mSocket != null && mSocket.isConnected();
81 }
82
83 private void close() {
84 if (isConnected()) {
85 try {
86 mSocket.close();
87 } catch (IOException e) {
88 // we are letting go of the connection anyway, so log and continue
89 Log.w(TAG, "IOException during BluetoothSocket close(): " + e);
90 } finally {
91 mSocket = null;
92 }
93 }
94 }
95
96 @Override
97 public boolean reconnect() {
98 close();
99 return connect();
100 }
101
102 @Override
103 public InputStream getInputStream() {
104 if (isConnected()) {
105 try {
106 return mSocket.getInputStream();
107 } catch (IOException e) {
108 Log.w(TAG, "failed to get Bluetooth input stream: " + e);
109 }
110 }
111 return null;
112 }
113
114 @Override
115 public OutputStream getOutputStream() {
116 if (isConnected()) {
117 try {
118 return mSocket.getOutputStream();
119 } catch (IOException e) {
120 Log.w(TAG, "failed to get Bluetooth output stream: " + e);
121 }
122 }
123 return null;
124 }
125}