Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Backends/BluetoothBackend/BluetoothLink.java
Show All 30 Lines | |||||
31 | import org.json.JSONException; | 31 | import org.json.JSONException; | ||
32 | import org.json.JSONObject; | 32 | import org.json.JSONObject; | ||
33 | import org.kde.kdeconnect.Backends.BaseLink; | 33 | import org.kde.kdeconnect.Backends.BaseLink; | ||
34 | import org.kde.kdeconnect.Backends.BasePairingHandler; | 34 | import org.kde.kdeconnect.Backends.BasePairingHandler; | ||
35 | import org.kde.kdeconnect.Device; | 35 | import org.kde.kdeconnect.Device; | ||
36 | import org.kde.kdeconnect.Helpers.SecurityHelpers.RsaHelper; | 36 | import org.kde.kdeconnect.Helpers.SecurityHelpers.RsaHelper; | ||
37 | import org.kde.kdeconnect.NetworkPackage; | 37 | import org.kde.kdeconnect.NetworkPackage; | ||
38 | 38 | | |||
39 | import java.io.*; | 39 | import java.io.IOException; | ||
40 | import java.io.InputStream; | ||||
41 | import java.io.InputStreamReader; | ||||
42 | import java.io.OutputStream; | ||||
43 | import java.io.Reader; | ||||
40 | import java.nio.charset.Charset; | 44 | import java.nio.charset.Charset; | ||
41 | import java.security.PublicKey; | 45 | import java.security.PublicKey; | ||
42 | import java.util.UUID; | 46 | import java.util.UUID; | ||
43 | 47 | | |||
44 | @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) | 48 | @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) | ||
45 | public class BluetoothLink extends BaseLink { | 49 | public class BluetoothLink extends BaseLink { | ||
46 | private final BluetoothSocket socket; | 50 | private final BluetoothSocket socket; | ||
47 | private final BluetoothLinkProvider linkProvider; | 51 | private final BluetoothLinkProvider linkProvider; | ||
Show All 35 Lines | 83 | private void processMessage(String message) { | |||
83 | } catch (JSONException e) { | 87 | } catch (JSONException e) { | ||
84 | Log.e("BluetoothLink/receiving", "Unable to parse message.", e); | 88 | Log.e("BluetoothLink/receiving", "Unable to parse message.", e); | ||
85 | return; | 89 | return; | ||
86 | } | 90 | } | ||
87 | 91 | | |||
88 | if (np.getType().equals(NetworkPackage.PACKAGE_TYPE_ENCRYPTED)) { | 92 | if (np.getType().equals(NetworkPackage.PACKAGE_TYPE_ENCRYPTED)) { | ||
89 | try { | 93 | try { | ||
90 | np = RsaHelper.decrypt(np, privateKey); | 94 | np = RsaHelper.decrypt(np, privateKey); | ||
91 | } catch(Exception e) { | 95 | } catch (Exception e) { | ||
92 | Log.e("BluetoothLink/receiving", "Exception decrypting the package", e); | 96 | Log.e("BluetoothLink/receiving", "Exception decrypting the package", e); | ||
93 | } | 97 | } | ||
94 | } | 98 | } | ||
95 | 99 | | |||
96 | if (np.hasPayloadTransferInfo()) { | 100 | if (np.hasPayloadTransferInfo()) { | ||
97 | BluetoothSocket transferSocket = null; | 101 | BluetoothSocket transferSocket = null; | ||
98 | try { | 102 | try { | ||
99 | UUID transferUuid = UUID.fromString(np.getPayloadTransferInfo().getString("uuid")); | 103 | UUID transferUuid = UUID.fromString(np.getPayloadTransferInfo().getString("uuid")); | ||
100 | transferSocket = socket.getRemoteDevice().createRfcommSocketToServiceRecord(transferUuid); | 104 | transferSocket = socket.getRemoteDevice().createRfcommSocketToServiceRecord(transferUuid); | ||
101 | transferSocket.connect(); | 105 | transferSocket.connect(); | ||
102 | np.setPayload(transferSocket.getInputStream(), np.getPayloadSize()); | 106 | np.setPayload(transferSocket.getInputStream(), np.getPayloadSize()); | ||
103 | } catch (Exception e) { | 107 | } catch (Exception e) { | ||
104 | if (transferSocket != null) { | 108 | if (transferSocket != null) { | ||
105 | try { transferSocket.close(); } catch(IOException ignored) { } | 109 | try { | ||
110 | transferSocket.close(); | ||||
111 | } catch (IOException ignored) { | ||||
112 | } | ||||
106 | } | 113 | } | ||
107 | Log.e("BluetoothLink/receiving", "Unable to get payload", e); | 114 | Log.e("BluetoothLink/receiving", "Unable to get payload", e); | ||
108 | } | 115 | } | ||
109 | } | 116 | } | ||
110 | 117 | | |||
111 | packageReceived(np); | 118 | packageReceived(np); | ||
112 | } | 119 | } | ||
113 | }); | 120 | }); | ||
Show All 28 Lines | 142 | public void disconnect() { | |||
142 | } catch (IOException e) { | 149 | } catch (IOException e) { | ||
143 | } | 150 | } | ||
144 | linkProvider.disconnectedLink(this, getDeviceId(), socket); | 151 | linkProvider.disconnectedLink(this, getDeviceId(), socket); | ||
145 | } | 152 | } | ||
146 | 153 | | |||
147 | private void sendMessage(NetworkPackage np) throws JSONException, IOException { | 154 | private void sendMessage(NetworkPackage np) throws JSONException, IOException { | ||
148 | byte[] message = np.serialize().getBytes(Charset.forName("UTF-8")); | 155 | byte[] message = np.serialize().getBytes(Charset.forName("UTF-8")); | ||
149 | OutputStream socket = this.socket.getOutputStream(); | 156 | OutputStream socket = this.socket.getOutputStream(); | ||
150 | Log.i("BluetoothLink","Beginning to send message"); | 157 | Log.i("BluetoothLink", "Beginning to send message"); | ||
151 | socket.write(message); | 158 | socket.write(message); | ||
152 | Log.i("BluetoothLink","Finished sending message"); | 159 | Log.i("BluetoothLink", "Finished sending message"); | ||
153 | } | 160 | } | ||
154 | 161 | | |||
155 | @Override | 162 | @Override | ||
156 | public boolean sendPackage(NetworkPackage np, Device.SendPackageStatusCallback callback) { | 163 | public boolean sendPackage(NetworkPackage np, Device.SendPackageStatusCallback callback) { | ||
157 | return sendPackageInternal(np, callback, null); | 164 | return sendPackageInternal(np, callback, null); | ||
158 | } | 165 | } | ||
159 | 166 | | |||
160 | @Override | 167 | @Override | ||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Line(s) | 216 | while ((bytesRead = stream.read(buffer)) != -1) { | |||
214 | } | 221 | } | ||
215 | } | 222 | } | ||
216 | transferSocket.getOutputStream().flush(); | 223 | transferSocket.getOutputStream().flush(); | ||
217 | stream.close(); | 224 | stream.close(); | ||
218 | } catch (Exception e) { | 225 | } catch (Exception e) { | ||
219 | callback.onFailure(e); | 226 | callback.onFailure(e); | ||
220 | return false; | 227 | return false; | ||
221 | } finally { | 228 | } finally { | ||
222 | try { transferSocket.close(); } catch (IOException ignored) { } | 229 | try { | ||
230 | transferSocket.close(); | ||||
231 | } catch (IOException ignored) { | ||||
232 | } | ||||
223 | } | 233 | } | ||
224 | } | 234 | } | ||
225 | 235 | | |||
226 | callback.onSuccess(); | 236 | callback.onSuccess(); | ||
227 | return true; | 237 | return true; | ||
228 | } catch (Exception e) { | 238 | } catch (Exception e) { | ||
229 | callback.onFailure(e); | 239 | callback.onFailure(e); | ||
230 | return false; | 240 | return false; | ||
Show All 18 Lines |