diff options
author | Linux Build Service Account <lnxbuild@localhost> | 2014-07-01 14:17:56 -0700 |
---|---|---|
committer | Gerrit - the friendly Code Review server <code-review@localhost> | 2014-07-01 14:17:56 -0700 |
commit | dcab7d3674bc83bd8d466f46910e9eb11c61d077 (patch) | |
tree | c4fa6a754be24845052d33c026b207982d11a910 /src | |
parent | 9bc9335ffc7c91e86d4ae8af7c3b64dbdf03dec5 (diff) | |
parent | 2a2e8d78744cf3c979760fba92f25ecbefb0eec4 (diff) | |
download | android_packages_apps_BluetoothExt-dcab7d3674bc83bd8d466f46910e9eb11c61d077.tar.gz android_packages_apps_BluetoothExt-dcab7d3674bc83bd8d466f46910e9eb11c61d077.tar.bz2 android_packages_apps_BluetoothExt-dcab7d3674bc83bd8d466f46910e9eb11c61d077.zip |
Merge "Bluetooth: Integrates LPP changes into pxp monitor servcie"
Diffstat (limited to 'src')
3 files changed, 235 insertions, 23 deletions
diff --git a/src/org/codeaurora/bluetooth/pxpservice/BluetoothRssiMonitorCallback.java b/src/org/codeaurora/bluetooth/pxpservice/BluetoothRssiMonitorCallback.java new file mode 100644 index 0000000..dc10007 --- /dev/null +++ b/src/org/codeaurora/bluetooth/pxpservice/BluetoothRssiMonitorCallback.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2014, The Linux Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * * Neither the name of The Linux Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package android.bluetooth; + +/** @hide */ +public abstract class BluetoothRssiMonitorCallback { + /** @hide */ + public void onStarted() { + } + + /** @hide */ + public void onStopped() { + } + + /** @hide */ + public void onReadRssiThreshold(int thresh_min, int thresh_max, int alert, int status) { + } + + /** @hide */ + public void onAlert(int evtType, int rssi) { + } +}; diff --git a/src/org/codeaurora/bluetooth/pxpservice/DeviceProperties.java b/src/org/codeaurora/bluetooth/pxpservice/DeviceProperties.java index 902852b..43902f5 100644 --- a/src/org/codeaurora/bluetooth/pxpservice/DeviceProperties.java +++ b/src/org/codeaurora/bluetooth/pxpservice/DeviceProperties.java @@ -29,9 +29,19 @@ package org.codeaurora.bluetooth.pxpservice; +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.QBluetoothAdapter; import android.bluetooth.BluetoothGatt; +import android.bluetooth.BluetoothDevice; +import android.bluetooth.IBluetoothManager; +import android.bluetooth.BluetoothManager; +import android.content.Context; +import android.bluetooth.BluetoothRssiMonitorCallback; + import android.bluetooth.BluetoothGattCharacteristic; -import android.bluetooth.BluetoothLwPwrProximityMonitor; +import android.util.Log; +import java.util.Timer; +import java.util.TimerTask; /** * A Device Properties class. It contains information about bluetooth device @@ -39,18 +49,34 @@ import android.bluetooth.BluetoothLwPwrProximityMonitor; */ public class DeviceProperties { + private static final boolean DBG = true; + + private static final String TAG = "PxpdeviceProp"; + public BluetoothGatt gatt = null; public String deviceAddress = null; + public BluetoothManager mManager = null; + + public BluetoothAdapter mAdapter = null; + + public QBluetoothAdapter mQAdapter = null; + + public BluetoothDevice mDevice = null; + + private Context mContext = null; + + public int mState = 0; + + private BluetoothRssiMonitorCallback mMonitorCbk; + public BluetoothGattCharacteristic iasAlertLevelCh = null; public BluetoothGattCharacteristic llsAlertLevelCh = null; public BluetoothGattCharacteristic txPowerLevelCh = null; - public BluetoothLwPwrProximityMonitor qcRssiProximityMonitor = null; - public int pathLossAlertLevel = PxpConsts.ALERT_LEVEL_NO; public int maxPathLossThreshold = 147; @@ -84,12 +110,25 @@ public class DeviceProperties { public boolean failedReadAlertLevel = false; public boolean failedReadTxPowerLevel = false; + /* command status */ + /** @hide */ + public static final int COMMAND_STATUS_SUCCESS = 0x00; + /** @hide */ + public static final int COMMAND_STATUS_FAILED = 0x01; + + private final Object mStateLock = new Object(); + /* Monitor state constants */ + private static final int MONITOR_STATE_IDLE = 0; + private static final int MONITOR_STATE_STARTING = 1; + private static final int MONITOR_STATE_STOPPING = 2; + private static final int MONITOR_STATE_STARTED = 3; + private static final int MONITOR_STATE_CLOSED = 4; + public void reset() { iasAlertLevelCh = null; llsAlertLevelCh = null; txPowerLevelCh = null; - // rssiCallback = null; pathLossAlertLevel = PxpConsts.ALERT_LEVEL_NO; maxPathLossThreshold = 147; minPathLossThreshold = 100; @@ -106,5 +145,87 @@ public class DeviceProperties { startDiscoverServices = false; failedReadAlertLevel = false; failedReadTxPowerLevel = false; + mState = MONITOR_STATE_CLOSED; + } + + public void BluetoothLwPwrProximityMonitor (Context cxt, BluetoothDevice device, BluetoothRssiMonitorCallback cbk) { + mContext = cxt; + mState = MONITOR_STATE_CLOSED; + mMonitorCbk = cbk; + + try { + mDevice = device; + } catch (IllegalArgumentException e) { + mDevice = null; + if (DBG) Log.e(TAG, "", e); + } + + mQAdapter = QBluetoothAdapter.getDefaultAdapter(); + if (mDevice != null && mQAdapter != null) { + mState = MONITOR_STATE_IDLE; + mQAdapter.registerLppClient(mLPProxymityMonitorCallback, mDevice.getAddress(), true); + } else { + mDevice = null; + mQAdapter = null; + } + } + + public final QBluetoothAdapter.LeLppCallback mLPProxymityMonitorCallback = new QBluetoothAdapter.LeLppCallback() { + + /* @hide */ + public void onWriteRssiThreshold(int status) { + if (DBG) Log.d(TAG, "onWriteRssiThreshold() status=" + status); + synchronized (mStateLock) { + if (mState == MONITOR_STATE_STARTING){ + if (status == BluetoothGatt.GATT_SUCCESS){ + if (mQAdapter != null) { + mQAdapter.enableRssiMonitor(this, true); + } + } else { + mState = MONITOR_STATE_STOPPING; + } + } + } + } + /** @hide */ + public void onReadRssiThreshold(int low, int upper, int alert, int status) { + if (DBG) Log.d(TAG, "onReadRssiThreshold() LowerLimit=" + low + + ", UpperLimit=" + upper + ", Alert=" + alert + ", status=" + status); + if (mMonitorCbk != null) { + mMonitorCbk.onReadRssiThreshold(low, upper, alert, (status == 0)?COMMAND_STATUS_SUCCESS:COMMAND_STATUS_FAILED); + } + } + + /** @hide */ + public void onEnableRssiMonitor(int enable, int status) { + if (DBG) Log.d(TAG, "onEnableRssiMonitor() enable=" + enable + ", status=" + status); + synchronized(mStateLock) { + if (mState == MONITOR_STATE_STARTING) { + if (status == BluetoothGatt.GATT_SUCCESS && (enable != 0)) { + mState = MONITOR_STATE_STARTED; + if (DBG) Log.d(TAG, "Monitor is started successfully"); + } + } + } + + if (mState == MONITOR_STATE_STARTED && mMonitorCbk != null) { + if (DBG) Log.d(TAG, "Notify users that monitor has been started successfully"); + mMonitorCbk.onStarted(); + } + + } + /** @hide */ + public void onRssiThresholdEvent(int evtType, int rssi) { + if (DBG) Log.d(TAG, "onRssiThresholdEvent() event=" + evtType + ", rssi=" + rssi); + if (mMonitorCbk != null) mMonitorCbk.onAlert(evtType, rssi); + } + + /** @hide */ + public boolean onUpdateLease() { + if (DBG) Log.d(TAG, "onUpdateLease()"); + synchronized(mStateLock) { + return (mState != MONITOR_STATE_IDLE && mState != MONITOR_STATE_CLOSED); + } } + }; } diff --git a/src/org/codeaurora/bluetooth/pxpservice/PxpMonitorService.java b/src/org/codeaurora/bluetooth/pxpservice/PxpMonitorService.java index e91a157..c44be86 100644 --- a/src/org/codeaurora/bluetooth/pxpservice/PxpMonitorService.java +++ b/src/org/codeaurora/bluetooth/pxpservice/PxpMonitorService.java @@ -30,6 +30,7 @@ package org.codeaurora.bluetooth.pxpservice; import android.app.Service; import android.bluetooth.BluetoothAdapter; +import android.bluetooth.QBluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCallback; @@ -37,6 +38,7 @@ import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattService; import android.bluetooth.BluetoothManager; import android.bluetooth.BluetoothProfile; +import android.bluetooth.BluetoothRssiMonitorCallback; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -61,8 +63,6 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import android.bluetooth.BluetoothLwPwrProximityMonitor; -import android.bluetooth.BluetoothRssiMonitorCallback; /** * A Proximity Monitor Service. It interacts with the BLE device via the Android @@ -72,6 +72,7 @@ import android.bluetooth.BluetoothRssiMonitorCallback; public class PxpMonitorService extends Service { private static final String TAG = PxpMonitorService.class.getSimpleName(); + private static final boolean DBG = true; public static final String DEVICE_CONNECTED = "android.bluetooth.action.DEVICE_CONNECTED"; @@ -117,6 +118,30 @@ public class PxpMonitorService extends Service { private Map<BluetoothDevice, DeviceProperties> mHashMapDevice = null; private Queue<BluetoothGattCharacteristic> mReadQueue = new ArrayDeque<BluetoothGattCharacteristic>(); + private boolean mAutoConnect = false; + public boolean isWriteThresholdNeedToBeCalled = false; + /* Monitor state constants */ + private static final int MONITOR_STATE_IDLE = 0; + private static final int MONITOR_STATE_STARTING = 1; + private static final int MONITOR_STATE_STOPPING = 2; + private static final int MONITOR_STATE_STARTED = 3; + private static final int MONITOR_STATE_CLOSED = 4; + /* constants for rssi threshold event */ + /** @hide */ + public static final int RSSI_MONITOR_DISABLED = 0x00; + /** @hide */ + public static final int RSSI_HIGH_ALERT = 0x01; + /** @hide */ + public static final int RSSI_MILD_ALERT = 0x02; + /** @hide */ + public static final int RSSI_NO_ALERT = 0x03; + + /* command status */ + /** @hide */ + public static final int COMMAND_STATUS_SUCCESS = 0x00; + /** @hide */ + public static final int COMMAND_STATUS_FAILED = 0x01; + /** * Implements callback methods for GATT events defined by the Bluetooth Low @@ -658,8 +683,8 @@ public class PxpMonitorService extends Service { deviceProp.deviceAddress = address; - deviceProp.qcRssiProximityMonitor = new BluetoothLwPwrProximityMonitor(this, address, - new QcBluetoothMonitorRssiCallback(leDevice)); + deviceProp.BluetoothLwPwrProximityMonitor(this, leDevice, new QcBluetoothMonitorRssiCallback(leDevice)); + deviceProp.mState = MONITOR_STATE_IDLE; mHashMapDevice.put(leDevice, deviceProp); Log.d(TAG, "device added"); @@ -697,15 +722,14 @@ public class PxpMonitorService extends Service { deviceProp.reset(); deviceProp.disconnect = true; - // close hardware rssi monitor - deviceProp.qcRssiProximityMonitor.close(); + deviceProp.mQAdapter.registerLppClient(deviceProp.mLPProxymityMonitorCallback, leDevice.getAddress(), false); + deviceProp.mState = MONITOR_STATE_CLOSED; deviceProp.gatt.disconnect(); if (deviceProp.gatt != null) { deviceProp.gatt.close(); deviceProp.gatt = null; } - } /** @@ -721,8 +745,17 @@ public class PxpMonitorService extends Service { for (BluetoothDevice device : mHashMapDevice.keySet()) { DeviceProperties deviceProp = mHashMapDevice.get(device); Log.d(TAG, "close qcproximty"); - deviceProp.qcRssiProximityMonitor.close(); + if (DBG) Log.d(TAG, "close()"); + if(MONITOR_STATE_CLOSED == deviceProp.mState) + return; + if(deviceProp.mState == MONITOR_STATE_STARTING || + deviceProp.mState == MONITOR_STATE_STARTED) { + deviceProp.mQAdapter.enableRssiMonitor(deviceProp.mLPProxymityMonitorCallback, false); + } + deviceProp.mQAdapter.registerLppClient(deviceProp.mLPProxymityMonitorCallback, device.getAddress(), false); + if (DBG) Log.d(TAG, "Monitor is closed"); + deviceProp.mState = MONITOR_STATE_CLOSED; } } @@ -839,25 +872,34 @@ public class PxpMonitorService extends Service { } } - if (deviceProp.pathLossAlertLevel != PxpConsts.ALERT_LEVEL_NO) { + if (deviceProp.pathLossAlertLevel != PxpConsts.ALERT_LEVEL_NO) + { Log.v(TAG, "deviceProp.mPathLossAlertLevel != PxpConsts.ALERT_LEVEL_NO"); int rssiMin = deviceProp.txPowerLevel - deviceProp.minPathLossThreshold; int rssiMax = deviceProp.txPowerLevel - deviceProp.maxPathLossThreshold; Log.d(TAG, "rssiMin::"+rssiMin); Log.d(TAG, "rssiMax::"+rssiMax); // start hardware rssi monitor - if (!deviceProp.qcRssiProximityMonitor.start(rssiMin, rssiMax)) { + if (!deviceProp.mQAdapter.writeRssiThreshold(deviceProp.mLPProxymityMonitorCallback, rssiMin, rssiMax)) { // start software method to monitor rssi startPathLossSwMonitor(); } + deviceProp.mState = MONITOR_STATE_STARTING; - } else { + } + else { // stop hardware rssi monitor - deviceProp.qcRssiProximityMonitor.stop(); + if(deviceProp.mState == MONITOR_STATE_STARTING || + deviceProp.mState == MONITOR_STATE_STARTED) { + deviceProp.mQAdapter.enableRssiMonitor(deviceProp.mLPProxymityMonitorCallback, false); + deviceProp.mState = MONITOR_STATE_STOPPING; + deviceProp.mQAdapter.registerLppClient(deviceProp.mLPProxymityMonitorCallback, leDevice.getAddress(), false); + if (DBG) Log.d(TAG, "Monitor is stopping"); + } // stop software method to monitor rssi stopPathLossSwMonitor(); - } + } return true; } @@ -1071,13 +1113,13 @@ public class PxpMonitorService extends Service { public int getAlertLevelValue(int evtType) { int alertLevelValue = 0; switch(evtType) { - case BluetoothLwPwrProximityMonitor.RSSI_NO_ALERT: + case RSSI_NO_ALERT: alertLevelValue = NO_ALERT; break; - case BluetoothLwPwrProximityMonitor.RSSI_MILD_ALERT: + case RSSI_MILD_ALERT: alertLevelValue = MILD_ALERT; break; - case BluetoothLwPwrProximityMonitor.RSSI_HIGH_ALERT: + case RSSI_HIGH_ALERT: alertLevelValue = HIGH_ALERT; break; } @@ -1113,7 +1155,7 @@ public class PxpMonitorService extends Service { DeviceProperties deviceProp = mHashMapDevice.get(mDevice); if(deviceProp.minPathLossThreshold == deviceProp.maxPathLossThreshold) { - if(evtType == BluetoothLwPwrProximityMonitor.RSSI_HIGH_ALERT) { + if(evtType == RSSI_HIGH_ALERT) { alertLevelValue = deviceProp.pathLossAlertLevel; } } @@ -1125,8 +1167,8 @@ public class PxpMonitorService extends Service { BluetoothGattCharacteristic.FORMAT_UINT8, 0); deviceProp.gatt.writeCharacteristic(deviceProp.iasAlertLevelCh); - if (evtType == BluetoothLwPwrProximityMonitor.RSSI_NO_ALERT - || evtType == BluetoothLwPwrProximityMonitor.RSSI_MONITOR_DISABLED) { + if (evtType == RSSI_NO_ALERT + || evtType == RSSI_MONITOR_DISABLED) { deviceProp.isAlerting = false; } else { |