OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.device.bluetooth; | 5 package org.chromium.device.bluetooth; |
6 | 6 |
7 import android.annotation.TargetApi; | 7 import android.annotation.TargetApi; |
8 import android.bluetooth.BluetoothDevice; | 8 import android.bluetooth.BluetoothDevice; |
9 import android.content.Context; | 9 import android.content.Context; |
10 import android.os.Build; | 10 import android.os.Build; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 mWrapperToChromeDescriptorsMap = new HashMap<Wrappers.BluetoothGattDescr
iptorWrapper, | 52 mWrapperToChromeDescriptorsMap = new HashMap<Wrappers.BluetoothGattDescr
iptorWrapper, |
53 ChromeBluetoothRemoteGattDescriptor>(); | 53 ChromeBluetoothRemoteGattDescriptor>(); |
54 Log.v(TAG, "ChromeBluetoothDevice created."); | 54 Log.v(TAG, "ChromeBluetoothDevice created."); |
55 } | 55 } |
56 | 56 |
57 /** | 57 /** |
58 * Handles C++ object being destroyed. | 58 * Handles C++ object being destroyed. |
59 */ | 59 */ |
60 @CalledByNative | 60 @CalledByNative |
61 private void onBluetoothDeviceAndroidDestruction() { | 61 private void onBluetoothDeviceAndroidDestruction() { |
62 disconnectGatt(); | 62 if (mBluetoothGatt != null) { |
| 63 mBluetoothGatt.close(); |
| 64 mBluetoothGatt = null; |
| 65 } |
63 mNativeBluetoothDeviceAndroid = 0; | 66 mNativeBluetoothDeviceAndroid = 0; |
64 } | 67 } |
65 | 68 |
66 // -------------------------------------------------------------------------
-------------------- | 69 // -------------------------------------------------------------------------
-------------------- |
67 // BluetoothDeviceAndroid methods implemented in java: | 70 // BluetoothDeviceAndroid methods implemented in java: |
68 | 71 |
69 // Implements BluetoothDeviceAndroid::Create. | 72 // Implements BluetoothDeviceAndroid::Create. |
70 // 'Object' type must be used because inner class Wrappers.BluetoothDeviceWr
apper reference is | 73 // 'Object' type must be used because inner class Wrappers.BluetoothDeviceWr
apper reference is |
71 // not handled by jni_generator.py JavaToJni. http://crbug.com/505554 | 74 // not handled by jni_generator.py JavaToJni. http://crbug.com/505554 |
72 @CalledByNative | 75 @CalledByNative |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 private String[] getUuids() { | 115 private String[] getUuids() { |
113 // TODO(scheib): return merged list of UUIDs from scan results and, | 116 // TODO(scheib): return merged list of UUIDs from scan results and, |
114 // after a device is connected, discoverServices. crbug.com/508648 | 117 // after a device is connected, discoverServices. crbug.com/508648 |
115 return mUuidsFromScan.toArray(new String[mUuidsFromScan.size()]); | 118 return mUuidsFromScan.toArray(new String[mUuidsFromScan.size()]); |
116 } | 119 } |
117 | 120 |
118 // Implements BluetoothDeviceAndroid::CreateGattConnectionImpl. | 121 // Implements BluetoothDeviceAndroid::CreateGattConnectionImpl. |
119 @CalledByNative | 122 @CalledByNative |
120 private void createGattConnectionImpl(Context context) { | 123 private void createGattConnectionImpl(Context context) { |
121 Log.i(TAG, "connectGatt"); | 124 Log.i(TAG, "connectGatt"); |
| 125 |
| 126 if (mBluetoothGatt != null) mBluetoothGatt.close(); |
| 127 |
122 // autoConnect set to false as under experimentation using autoConnect f
ailed to complete | 128 // autoConnect set to false as under experimentation using autoConnect f
ailed to complete |
123 // connections. | 129 // connections. |
124 mBluetoothGatt = | 130 mBluetoothGatt = |
125 mDevice.connectGatt(context, false /* autoConnect */, mBluetooth
GattCallbackImpl); | 131 mDevice.connectGatt(context, false /* autoConnect */, mBluetooth
GattCallbackImpl); |
126 } | 132 } |
127 | 133 |
128 // Implements BluetoothDeviceAndroid::DisconnectGatt. | 134 // Implements BluetoothDeviceAndroid::DisconnectGatt. |
129 @CalledByNative | 135 @CalledByNative |
130 private void disconnectGatt() { | 136 private void disconnectGatt() { |
131 Log.i(TAG, "BluetoothGatt.disconnect"); | 137 Log.i(TAG, "BluetoothGatt.disconnect"); |
132 if (mBluetoothGatt != null) mBluetoothGatt.disconnect(); | 138 if (mBluetoothGatt != null) mBluetoothGatt.disconnect(); |
133 } | 139 } |
134 | 140 |
135 // Implements BluetoothDeviceAndroid::GetDeviceName. | 141 // Implements BluetoothDeviceAndroid::GetDeviceName. |
136 @CalledByNative | 142 @CalledByNative |
137 private String getDeviceName() { | 143 private String getDeviceName() { |
138 return mDevice.getName(); | 144 return mDevice.getName(); |
139 } | 145 } |
140 | 146 |
141 // Implements callbacks related to a GATT connection. | 147 // Implements callbacks related to a GATT connection. |
142 private class BluetoothGattCallbackImpl extends Wrappers.BluetoothGattCallba
ckWrapper { | 148 private class BluetoothGattCallbackImpl extends Wrappers.BluetoothGattCallba
ckWrapper { |
143 @Override | 149 @Override |
144 public void onConnectionStateChange(final int status, final int newState
) { | 150 public void onConnectionStateChange(final int status, final int newState
) { |
145 Log.i(TAG, "onConnectionStateChange status:%d newState:%s", status, | 151 Log.i(TAG, "onConnectionStateChange status:%d newState:%s", status, |
146 (newState == android.bluetooth.BluetoothProfile.STATE_CONNEC
TED) | 152 (newState == android.bluetooth.BluetoothProfile.STATE_CONNEC
TED) |
147 ? "Connected" | 153 ? "Connected" |
148 : "Disconnected"); | 154 : "Disconnected"); |
149 if (newState == android.bluetooth.BluetoothProfile.STATE_CONNECTED)
{ | 155 if (newState == android.bluetooth.BluetoothProfile.STATE_CONNECTED)
{ |
150 mBluetoothGatt.discoverServices(); | 156 mBluetoothGatt.discoverServices(); |
| 157 } else if (newState == android.bluetooth.BluetoothProfile.STATE_DISC
ONNECTED) { |
| 158 if (mBluetoothGatt != null) { |
| 159 mBluetoothGatt.close(); |
| 160 mBluetoothGatt = null; |
| 161 } |
151 } | 162 } |
152 ThreadUtils.runOnUiThread(new Runnable() { | 163 ThreadUtils.runOnUiThread(new Runnable() { |
153 @Override | 164 @Override |
154 public void run() { | 165 public void run() { |
155 if (mNativeBluetoothDeviceAndroid != 0) { | 166 if (mNativeBluetoothDeviceAndroid != 0) { |
156 nativeOnConnectionStateChange(mNativeBluetoothDeviceAndr
oid, status, | 167 nativeOnConnectionStateChange(mNativeBluetoothDeviceAndr
oid, status, |
157 newState == android.bluetooth.BluetoothProfile.S
TATE_CONNECTED); | 168 newState == android.bluetooth.BluetoothProfile.S
TATE_CONNECTED); |
158 } | 169 } |
159 } | 170 } |
160 }); | 171 }); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 long nativeBluetoothDeviceAndroid, int status, boolean connected); | 265 long nativeBluetoothDeviceAndroid, int status, boolean connected); |
255 | 266 |
256 // Binds to BluetoothDeviceAndroid::CreateGattRemoteService. | 267 // Binds to BluetoothDeviceAndroid::CreateGattRemoteService. |
257 // TODO(http://crbug.com/505554): Replace 'Object' with specific type when J
NI fixed. | 268 // TODO(http://crbug.com/505554): Replace 'Object' with specific type when J
NI fixed. |
258 private native void nativeCreateGattRemoteService(long nativeBluetoothDevice
Android, | 269 private native void nativeCreateGattRemoteService(long nativeBluetoothDevice
Android, |
259 String instanceId, Object bluetoothGattServiceWrapper); | 270 String instanceId, Object bluetoothGattServiceWrapper); |
260 | 271 |
261 // Binds to BluetoothDeviceAndroid::GattServicesDiscovered. | 272 // Binds to BluetoothDeviceAndroid::GattServicesDiscovered. |
262 private native void nativeOnGattServicesDiscovered(long nativeBluetoothDevic
eAndroid); | 273 private native void nativeOnGattServicesDiscovered(long nativeBluetoothDevic
eAndroid); |
263 } | 274 } |
OLD | NEW |