AdapterProperties.java revision 86c29fe88456bdcfbd4334647b04ef81ff384a06
1/*
2 * Copyright (C) 2012 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.bluetooth.btservice;
18
19import android.bluetooth.BluetoothA2dp;
20import android.bluetooth.BluetoothA2dpSink;
21import android.bluetooth.BluetoothAdapter;
22import android.bluetooth.BluetoothDevice;
23import android.bluetooth.BluetoothHeadset;
24import android.bluetooth.BluetoothHeadsetClient;
25import android.bluetooth.BluetoothProfile;
26import android.content.BroadcastReceiver;
27import android.content.Context;
28import android.content.Intent;
29import android.content.IntentFilter;
30import android.os.ParcelUuid;
31import android.os.UserHandle;
32import android.util.Log;
33import android.util.Pair;
34
35import com.android.bluetooth.Utils;
36import com.android.bluetooth.btservice.RemoteDevices.DeviceProperties;
37
38import java.util.HashMap;
39import java.util.concurrent.CopyOnWriteArrayList;
40
41class AdapterProperties {
42    private static final boolean DBG = true;
43    private static final boolean VDBG = false;
44    private static final String TAG = "BluetoothAdapterProperties";
45
46    private static final int BD_ADDR_LEN = 6; // 6 bytes
47    private volatile String mName;
48    private volatile byte[] mAddress;
49    private volatile int mBluetoothClass;
50    private volatile int mScanMode;
51    private volatile int mDiscoverableTimeout;
52    private volatile ParcelUuid[] mUuids;
53    private CopyOnWriteArrayList<BluetoothDevice> mBondedDevices = new CopyOnWriteArrayList<BluetoothDevice>();
54
55    private int mProfilesConnecting, mProfilesConnected, mProfilesDisconnecting;
56    private HashMap<Integer, Pair<Integer, Integer>> mProfileConnectionState;
57
58
59    private volatile int mConnectionState = BluetoothAdapter.STATE_DISCONNECTED;
60    private volatile int mState = BluetoothAdapter.STATE_OFF;
61
62    private AdapterService mService;
63    private boolean mDiscovering;
64    private RemoteDevices mRemoteDevices;
65    private BluetoothAdapter mAdapter;
66    //TODO - all hw capabilities to be exposed as a class
67    private int mNumOfAdvertisementInstancesSupported;
68    private boolean mRpaOffloadSupported;
69    private int mNumOfOffloadedIrkSupported;
70    private int mNumOfOffloadedScanFilterSupported;
71    private int mOffloadedScanResultStorageBytes;
72    private int mVersSupported;
73    private int mTotNumOfTrackableAdv;
74    private boolean mIsExtendedScanSupported;
75    private boolean mIsDebugLogSupported;
76    private boolean mIsActivityAndEnergyReporting;
77
78    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
79        @Override
80        public void onReceive(Context context, Intent intent) {
81            Log.d(TAG, "Received intent " + intent);
82            if (BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) {
83                sendConnectionStateChange(BluetoothProfile.HEADSET, intent);
84            } else if (BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) {
85                sendConnectionStateChange(BluetoothProfile.A2DP, intent);
86            } else if (BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED.equals(
87                               intent.getAction())) {
88                sendConnectionStateChange(BluetoothProfile.HEADSET_CLIENT, intent);
89            } else if (BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED.equals(
90                               intent.getAction())) {
91                sendConnectionStateChange(BluetoothProfile.A2DP_SINK, intent);
92            }
93        }
94    };
95
96    // Lock for all getters and setters.
97    // If finer grained locking is needer, more locks
98    // can be added here.
99    private Object mObject = new Object();
100
101    public AdapterProperties(AdapterService service) {
102        mService = service;
103        mAdapter = BluetoothAdapter.getDefaultAdapter();
104    }
105    public void init(RemoteDevices remoteDevices) {
106        if (mProfileConnectionState ==null) {
107            mProfileConnectionState = new HashMap<Integer, Pair<Integer, Integer>>();
108        } else {
109            mProfileConnectionState.clear();
110        }
111        mRemoteDevices = remoteDevices;
112
113        IntentFilter filter = new IntentFilter();
114        filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
115        filter.addAction(BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED);
116        filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
117        filter.addAction(BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED);
118        filter.addAction(BluetoothDevice.ACTION_UUID);
119        mService.registerReceiver(mReceiver, filter);
120    }
121
122    public void cleanup() {
123        mRemoteDevices = null;
124        if (mProfileConnectionState != null) {
125            mProfileConnectionState.clear();
126            mProfileConnectionState = null;
127        }
128        mService.unregisterReceiver(mReceiver);
129        mService = null;
130        mBondedDevices.clear();
131    }
132
133    @Override
134    public Object clone() throws CloneNotSupportedException {
135        throw new CloneNotSupportedException();
136    }
137
138    /**
139     * @return the mName
140     */
141    String getName() {
142        return mName;
143    }
144
145    /**
146     * Set the local adapter property - name
147     * @param name the name to set
148     */
149    boolean setName(String name) {
150        synchronized (mObject) {
151            return mService.setAdapterPropertyNative(
152                    AbstractionLayer.BT_PROPERTY_BDNAME, name.getBytes());
153        }
154    }
155
156    /**
157     * @return the mClass
158     */
159    int getBluetoothClass() {
160        return mBluetoothClass;
161    }
162
163    /**
164     * @return the mScanMode
165     */
166    int getScanMode() {
167        return mScanMode;
168    }
169
170    /**
171     * Set the local adapter property - scanMode
172     *
173     * @param scanMode the ScanMode to set
174     */
175    boolean setScanMode(int scanMode) {
176        synchronized (mObject) {
177            return mService.setAdapterPropertyNative(
178                    AbstractionLayer.BT_PROPERTY_ADAPTER_SCAN_MODE, Utils.intToByteArray(scanMode));
179        }
180    }
181
182    /**
183     * @return the mUuids
184     */
185    ParcelUuid[] getUuids() {
186        return mUuids;
187    }
188
189    /**
190     * Set local adapter UUIDs.
191     *
192     * @param uuids the uuids to be set.
193     */
194    boolean setUuids(ParcelUuid[] uuids) {
195        synchronized (mObject) {
196            return mService.setAdapterPropertyNative(
197                    AbstractionLayer.BT_PROPERTY_UUIDS, Utils.uuidsToByteArray(uuids));
198        }
199    }
200
201    /**
202     * @return the mAddress
203     */
204    byte[] getAddress() {
205        return mAddress;
206    }
207
208    /**
209     * @param mConnectionState the mConnectionState to set
210     */
211    void setConnectionState(int mConnectionState) {
212        this.mConnectionState = mConnectionState;
213    }
214
215    /**
216     * @return the mConnectionState
217     */
218    int getConnectionState() {
219        return mConnectionState;
220    }
221
222    /**
223     * @param mState the mState to set
224     */
225    void setState(int mState) {
226        debugLog("Setting state to " + mState);
227        this.mState = mState;
228    }
229
230    /**
231     * @return the mState
232     */
233    int getState() {
234        return mState;
235    }
236
237    /**
238     * @return the mNumOfAdvertisementInstancesSupported
239     */
240    int getNumOfAdvertisementInstancesSupported() {
241        return mNumOfAdvertisementInstancesSupported;
242    }
243
244    /**
245     * @return the mRpaOffloadSupported
246     */
247    boolean isRpaOffloadSupported() {
248        return mRpaOffloadSupported;
249    }
250
251    /**
252     * @return the mNumOfOffloadedIrkSupported
253     */
254    int getNumOfOffloadedIrkSupported() {
255        return mNumOfOffloadedIrkSupported;
256    }
257
258    /**
259     * @return the mNumOfOffloadedScanFilterSupported
260     */
261    int getNumOfOffloadedScanFilterSupported() {
262        return mNumOfOffloadedScanFilterSupported;
263    }
264
265    /**
266     * @return the mOffloadedScanResultStorageBytes
267     */
268    int getOffloadedScanResultStorage() {
269        return mOffloadedScanResultStorageBytes;
270    }
271
272    /**
273     * @return tx/rx/idle activity and energy info
274     */
275    boolean isActivityAndEnergyReportingSupported() {
276        return mIsActivityAndEnergyReporting;
277    }
278
279    /**
280     * @return total number of trackable advertisements
281     */
282    int getTotalNumOfTrackableAdvertisements() {
283        return mTotNumOfTrackableAdv;
284    }
285
286    /**
287     * @return the mBondedDevices
288     */
289    BluetoothDevice[] getBondedDevices() {
290        BluetoothDevice[] bondedDeviceList = new BluetoothDevice[0];
291        try {
292            bondedDeviceList = mBondedDevices.toArray(bondedDeviceList);
293        } catch(ArrayStoreException ee) {
294            errorLog("Error retrieving bonded device array");
295        }
296        infoLog("getBondedDevices: length=" + bondedDeviceList.length);
297        return bondedDeviceList;
298    }
299
300    // This function shall be invoked from BondStateMachine whenever the bond
301    // state changes.
302    void onBondStateChanged(BluetoothDevice device, int state)
303    {
304        if(device == null)
305            return;
306        try {
307            byte[] addrByte = Utils.getByteAddress(device);
308            DeviceProperties prop = mRemoteDevices.getDeviceProperties(device);
309            if (prop == null)
310                prop = mRemoteDevices.addDeviceProperties(addrByte);
311            prop.setBondState(state);
312
313            if (state == BluetoothDevice.BOND_BONDED) {
314                // add if not already in list
315                if(!mBondedDevices.contains(device)) {
316                    debugLog("Adding bonded device:" +  device);
317                    mBondedDevices.add(device);
318                }
319            } else if (state == BluetoothDevice.BOND_NONE) {
320                // remove device from list
321                if (mBondedDevices.remove(device))
322                    debugLog("Removing bonded device:" +  device);
323                else
324                    debugLog("Failed to remove device: " + device);
325            }
326        }
327        catch(Exception ee) {
328            Log.e(TAG, "Exception in onBondStateChanged : ", ee);
329        }
330    }
331
332    int getDiscoverableTimeout() {
333        return mDiscoverableTimeout;
334    }
335
336    boolean setDiscoverableTimeout(int timeout) {
337        synchronized (mObject) {
338            return mService.setAdapterPropertyNative(
339                    AbstractionLayer.BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT,
340                    Utils.intToByteArray(timeout));
341        }
342    }
343
344    int getProfileConnectionState(int profile) {
345        synchronized (mObject) {
346            Pair<Integer, Integer> p = mProfileConnectionState.get(profile);
347            if (p != null) return p.first;
348            return BluetoothProfile.STATE_DISCONNECTED;
349        }
350    }
351
352    boolean isDiscovering() {
353        return mDiscovering;
354    }
355
356    private void sendConnectionStateChange(int profile, Intent connIntent) {
357        BluetoothDevice device = connIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
358        int prevState = connIntent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, -1);
359        int state = connIntent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
360        sendConnectionStateChange(device, profile, state, prevState);
361    }
362    void sendConnectionStateChange(BluetoothDevice device, int profile, int state, int prevState) {
363        if (!validateProfileConnectionState(state) ||
364                !validateProfileConnectionState(prevState)) {
365            // Previously, an invalid state was broadcast anyway,
366            // with the invalid state converted to -1 in the intent.
367            // Better to log an error and not send an intent with
368            // invalid contents or set mAdapterConnectionState to -1.
369            errorLog("Error in sendConnectionStateChange: "
370                    + "prevState " + prevState + " state " + state);
371            return;
372        }
373
374        synchronized (mObject) {
375            updateProfileConnectionState(profile, state, prevState);
376
377            if (updateCountersAndCheckForConnectionStateChange(state, prevState)) {
378                setConnectionState(state);
379
380                Intent intent = new Intent(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
381                intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
382                intent.putExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
383                        convertToAdapterState(state));
384                intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_CONNECTION_STATE,
385                        convertToAdapterState(prevState));
386                intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
387                mService.sendBroadcastAsUser(intent, UserHandle.ALL,
388                        mService.BLUETOOTH_PERM);
389                Log.d(TAG, "CONNECTION_STATE_CHANGE: " + device + ": "
390                        + prevState + " -> " + state);
391            }
392        }
393    }
394
395    private boolean validateProfileConnectionState(int state) {
396        return (state == BluetoothProfile.STATE_DISCONNECTED ||
397                state == BluetoothProfile.STATE_CONNECTING ||
398                state == BluetoothProfile.STATE_CONNECTED ||
399                state == BluetoothProfile.STATE_DISCONNECTING);
400    }
401
402
403    private int convertToAdapterState(int state) {
404        switch (state) {
405            case BluetoothProfile.STATE_DISCONNECTED:
406                return BluetoothAdapter.STATE_DISCONNECTED;
407            case BluetoothProfile.STATE_DISCONNECTING:
408                return BluetoothAdapter.STATE_DISCONNECTING;
409            case BluetoothProfile.STATE_CONNECTED:
410                return BluetoothAdapter.STATE_CONNECTED;
411            case BluetoothProfile.STATE_CONNECTING:
412                return BluetoothAdapter.STATE_CONNECTING;
413        }
414        Log.e(TAG, "Error in convertToAdapterState");
415        return -1;
416    }
417
418    private boolean updateCountersAndCheckForConnectionStateChange(int state, int prevState) {
419        switch (prevState) {
420            case BluetoothProfile.STATE_CONNECTING:
421                mProfilesConnecting--;
422                break;
423
424            case BluetoothProfile.STATE_CONNECTED:
425                mProfilesConnected--;
426                break;
427
428            case BluetoothProfile.STATE_DISCONNECTING:
429                mProfilesDisconnecting--;
430                break;
431        }
432
433        switch (state) {
434            case BluetoothProfile.STATE_CONNECTING:
435                mProfilesConnecting++;
436                return (mProfilesConnected == 0 && mProfilesConnecting == 1);
437
438            case BluetoothProfile.STATE_CONNECTED:
439                mProfilesConnected++;
440                return (mProfilesConnected == 1);
441
442            case BluetoothProfile.STATE_DISCONNECTING:
443                mProfilesDisconnecting++;
444                return (mProfilesConnected == 0 && mProfilesDisconnecting == 1);
445
446            case BluetoothProfile.STATE_DISCONNECTED:
447                return (mProfilesConnected == 0 && mProfilesConnecting == 0);
448
449            default:
450                return true;
451        }
452    }
453
454    private void updateProfileConnectionState(int profile, int newState, int oldState) {
455        // mProfileConnectionState is a hashmap -
456        // <Integer, Pair<Integer, Integer>>
457        // The key is the profile, the value is a pair. first element
458        // is the state and the second element is the number of devices
459        // in that state.
460        int numDev = 1;
461        int newHashState = newState;
462        boolean update = true;
463
464        // The following conditions are considered in this function:
465        // 1. If there is no record of profile and state - update
466        // 2. If a new device's state is current hash state - increment
467        //    number of devices in the state.
468        // 3. If a state change has happened to Connected or Connecting
469        //    (if current state is not connected), update.
470        // 4. If numDevices is 1 and that device state is being updated, update
471        // 5. If numDevices is > 1 and one of the devices is changing state,
472        //    decrement numDevices but maintain oldState if it is Connected or
473        //    Connecting
474        Pair<Integer, Integer> stateNumDev = mProfileConnectionState.get(profile);
475        if (stateNumDev != null) {
476            int currHashState = stateNumDev.first;
477            numDev = stateNumDev.second;
478
479            if (newState == currHashState) {
480                numDev ++;
481            } else if (newState == BluetoothProfile.STATE_CONNECTED ||
482                   (newState == BluetoothProfile.STATE_CONNECTING &&
483                    currHashState != BluetoothProfile.STATE_CONNECTED)) {
484                 numDev = 1;
485            } else if (numDev == 1 && oldState == currHashState) {
486                 update = true;
487            } else if (numDev > 1 && oldState == currHashState) {
488                 numDev --;
489
490                 if (currHashState == BluetoothProfile.STATE_CONNECTED ||
491                     currHashState == BluetoothProfile.STATE_CONNECTING) {
492                    newHashState = currHashState;
493                 }
494            } else {
495                 update = false;
496            }
497        }
498
499        if (update) {
500            mProfileConnectionState.put(profile, new Pair<Integer, Integer>(newHashState,
501                    numDev));
502        }
503    }
504
505    void adapterPropertyChangedCallback(int[] types, byte[][] values) {
506        Intent intent;
507        int type;
508        byte[] val;
509        for (int i = 0; i < types.length; i++) {
510            val = values[i];
511            type = types[i];
512            infoLog("adapterPropertyChangedCallback with type:" + type + " len:" + val.length);
513            synchronized (mObject) {
514                switch (type) {
515                    case AbstractionLayer.BT_PROPERTY_BDNAME:
516                        mName = new String(val);
517                        intent = new Intent(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
518                        intent.putExtra(BluetoothAdapter.EXTRA_LOCAL_NAME, mName);
519                        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
520                        mService.sendBroadcastAsUser(intent, UserHandle.ALL,
521                                 mService.BLUETOOTH_PERM);
522                        debugLog("Name is: " + mName);
523                        break;
524                    case AbstractionLayer.BT_PROPERTY_BDADDR:
525                        mAddress = val;
526                        debugLog("Address is:" + Utils.getAddressStringFromByte(mAddress));
527                        break;
528                    case AbstractionLayer.BT_PROPERTY_CLASS_OF_DEVICE:
529                        mBluetoothClass = Utils.byteArrayToInt(val, 0);
530                        debugLog("BT Class:" + mBluetoothClass);
531                        break;
532                    case AbstractionLayer.BT_PROPERTY_ADAPTER_SCAN_MODE:
533                        int mode = Utils.byteArrayToInt(val, 0);
534                        mScanMode = mService.convertScanModeFromHal(mode);
535                        intent = new Intent(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
536                        intent.putExtra(BluetoothAdapter.EXTRA_SCAN_MODE, mScanMode);
537                        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
538                        mService.sendBroadcast(intent, mService.BLUETOOTH_PERM);
539                        debugLog("Scan Mode:" + mScanMode);
540                        if (mBluetoothDisabling) {
541                            mBluetoothDisabling=false;
542                            mService.startBluetoothDisable();
543                        }
544                        break;
545                    case AbstractionLayer.BT_PROPERTY_UUIDS:
546                        mUuids = Utils.byteArrayToUuid(val);
547                        break;
548                    case AbstractionLayer.BT_PROPERTY_ADAPTER_BONDED_DEVICES:
549                        int number = val.length/BD_ADDR_LEN;
550                        byte[] addrByte = new byte[BD_ADDR_LEN];
551                        for (int j = 0; j < number; j++) {
552                            System.arraycopy(val, j * BD_ADDR_LEN, addrByte, 0, BD_ADDR_LEN);
553                            onBondStateChanged(mAdapter.getRemoteDevice(
554                                               Utils.getAddressStringFromByte(addrByte)),
555                                               BluetoothDevice.BOND_BONDED);
556                        }
557                        break;
558                    case AbstractionLayer.BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT:
559                        mDiscoverableTimeout = Utils.byteArrayToInt(val, 0);
560                        debugLog("Discoverable Timeout:" + mDiscoverableTimeout);
561                        break;
562
563                    case AbstractionLayer.BT_PROPERTY_LOCAL_LE_FEATURES:
564                        updateFeatureSupport(val);
565                        break;
566
567                    default:
568                        errorLog("Property change not handled in Java land:" + type);
569                }
570            }
571        }
572    }
573
574    void updateFeatureSupport(byte[] val) {
575        mVersSupported = ((0xFF & ((int)val[1])) << 8)
576                            + (0xFF & ((int)val[0]));
577        mNumOfAdvertisementInstancesSupported = (0xFF & ((int)val[3]));
578        mRpaOffloadSupported = ((0xFF & ((int)val[4]))!= 0);
579        mNumOfOffloadedIrkSupported =  (0xFF & ((int)val[5]));
580        mNumOfOffloadedScanFilterSupported = (0xFF & ((int)val[6]));
581        mIsActivityAndEnergyReporting = ((0xFF & ((int)val[7])) != 0);
582        mOffloadedScanResultStorageBytes = ((0xFF & ((int)val[9])) << 8)
583                            + (0xFF & ((int)val[8]));
584        mTotNumOfTrackableAdv = ((0xFF & ((int)val[11])) << 8)
585                            + (0xFF & ((int)val[10]));
586        mIsExtendedScanSupported = ((0xFF & ((int)val[12])) != 0);
587        mIsDebugLogSupported = ((0xFF & ((int)val[13])) != 0);
588
589        Log.d(TAG, "BT_PROPERTY_LOCAL_LE_FEATURES: update from BT controller"
590                + " mNumOfAdvertisementInstancesSupported = "
591                + mNumOfAdvertisementInstancesSupported
592                + " mRpaOffloadSupported = " + mRpaOffloadSupported
593                + " mNumOfOffloadedIrkSupported = "
594                + mNumOfOffloadedIrkSupported
595                + " mNumOfOffloadedScanFilterSupported = "
596                + mNumOfOffloadedScanFilterSupported
597                + " mOffloadedScanResultStorageBytes= "
598                + mOffloadedScanResultStorageBytes
599                + " mIsActivityAndEnergyReporting = "
600                + mIsActivityAndEnergyReporting
601                +" mVersSupported = "
602                + mVersSupported
603                + " mTotNumOfTrackableAdv = "
604                + mTotNumOfTrackableAdv
605                + " mIsExtendedScanSupported = "
606                + mIsExtendedScanSupported
607                + " mIsDebugLogSupported = "
608                + mIsDebugLogSupported
609                );
610    }
611
612    void onBluetoothReady() {
613        Log.d(TAG, "ScanMode =  " + mScanMode );
614        Log.d(TAG, "State =  " + getState() );
615
616        // When BT is being turned on, all adapter properties will be sent in 1
617        // callback. At this stage, set the scan mode.
618        synchronized (mObject) {
619            if (getState() == BluetoothAdapter.STATE_TURNING_ON &&
620                    mScanMode == BluetoothAdapter.SCAN_MODE_NONE) {
621                    /* mDiscoverableTimeout is part of the
622                       adapterPropertyChangedCallback received before
623                       onBluetoothReady */
624                    if (mDiscoverableTimeout != 0)
625                      setScanMode(AbstractionLayer.BT_SCAN_MODE_CONNECTABLE);
626                    else /* if timeout == never (0) at startup */
627                      setScanMode(AbstractionLayer.BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
628                    /* though not always required, this keeps NV up-to date on first-boot after flash */
629                    setDiscoverableTimeout(mDiscoverableTimeout);
630            }
631        }
632    }
633
634    private boolean mBluetoothDisabling = false;
635
636    void onBleDisable() {
637        // Sequence BLE_ON to STATE_OFF - that is _complete_ OFF state.
638        // When BT disable is invoked, set the scan_mode to NONE
639        // so no incoming connections are possible
640        debugLog("onBleDisable");
641        if (getState() == BluetoothAdapter.STATE_BLE_TURNING_OFF) {
642           setScanMode(AbstractionLayer.BT_SCAN_MODE_NONE);
643        }
644    }
645
646    void onBluetoothDisable() {
647        // From STATE_ON to BLE_ON
648        // When BT disable is invoked, set the scan_mode to NONE
649        // so no incoming connections are possible
650
651        //Set flag to indicate we are disabling. When property change of scan mode done
652        //continue with disable sequence
653        debugLog("onBluetoothDisable()");
654        mBluetoothDisabling = true;
655        if (getState() == BluetoothAdapter.STATE_TURNING_OFF) {
656            // Turn off any Device Search/Inquiry
657            mService.cancelDiscovery();
658            setScanMode(AbstractionLayer.BT_SCAN_MODE_NONE);
659        }
660    }
661
662    void discoveryStateChangeCallback(int state) {
663        infoLog("Callback:discoveryStateChangeCallback with state:" + state);
664        synchronized (mObject) {
665            Intent intent;
666            if (state == AbstractionLayer.BT_DISCOVERY_STOPPED) {
667                mDiscovering = false;
668                intent = new Intent(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
669                mService.sendBroadcast(intent, mService.BLUETOOTH_PERM);
670            } else if (state == AbstractionLayer.BT_DISCOVERY_STARTED) {
671                mDiscovering = true;
672                intent = new Intent(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
673                mService.sendBroadcast(intent, mService.BLUETOOTH_PERM);
674            }
675        }
676    }
677
678    private void infoLog(String msg) {
679        if (VDBG) Log.i(TAG, msg);
680    }
681
682    private void debugLog(String msg) {
683        if (DBG) Log.d(TAG, msg);
684    }
685
686    private void errorLog(String msg) {
687        Log.e(TAG, msg);
688    }
689}
690