RemoteDevices.java revision 0322ae1f5e8ea55c391145e7925cb5962a97c11f
1ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh/*
2ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh * Copyright (C) 2012 Google Inc.
3ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh */
4ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
5ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshpackage com.android.bluetooth.btservice;
6ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
7ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.bluetooth.BluetoothAdapter;
8ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.bluetooth.BluetoothClass;
9ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.bluetooth.BluetoothDevice;
10ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.content.Context;
11ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.content.Intent;
12ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.os.Handler;
13ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.os.Message;
14ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.os.ParcelUuid;
15ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.util.Log;
16ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
17ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport com.android.bluetooth.Utils;
18ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport com.android.bluetooth.btservice.RemoteDevices.DeviceProperties;
19ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
20ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport java.util.ArrayList;
21ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport java.util.HashMap;
22ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport java.util.LinkedList;
23ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
24ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
25ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshfinal class RemoteDevices {
26ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private static final boolean DBG = true;
27ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private static final String TAG = "BluetoothRemoteDevices";
28ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
2974ae04c73312403e89db0f8e9bd9601d403b4783fredc
30ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private static BluetoothAdapter mAdapter;
31ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private static AdapterService mAdapterService;
32ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private static ArrayList<BluetoothDevice> mSdpTracker;
33ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
34ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private Object mObject = new Object();
35ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
36ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private static final int UUID_INTENT_DELAY = 6000;
37ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private static final int MESSAGE_UUID_INTENT = 1;
38ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
39ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private HashMap<BluetoothDevice, DeviceProperties> mDevices;
40ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
4174ae04c73312403e89db0f8e9bd9601d403b4783fredc    RemoteDevices(AdapterService service) {
42ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        mAdapter = BluetoothAdapter.getDefaultAdapter();
43ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        mAdapterService = service;
44ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        mSdpTracker = new ArrayList<BluetoothDevice>();
45ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        mDevices = new HashMap<BluetoothDevice, DeviceProperties>();
46ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
47ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
48ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
4974ae04c73312403e89db0f8e9bd9601d403b4783fredc    void cleanup() {
506654f5c903de510a70f9e72cd5ad7837b615d93ffredc        mSdpTracker.clear();
5131ba132491053bc86d419a7d51fc04af3299c076fredc        mSdpTracker = null;
526654f5c903de510a70f9e72cd5ad7837b615d93ffredc        mDevices.clear();
5331ba132491053bc86d419a7d51fc04af3299c076fredc        mDevices = null;
5431ba132491053bc86d419a7d51fc04af3299c076fredc        mAdapterService = null;
5531ba132491053bc86d419a7d51fc04af3299c076fredc        mAdapter= null;
566654f5c903de510a70f9e72cd5ad7837b615d93ffredc    }
576654f5c903de510a70f9e72cd5ad7837b615d93ffredc
58ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    public Object Clone() throws CloneNotSupportedException {
59ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        throw new CloneNotSupportedException();
60ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
61ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
62ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    DeviceProperties getDeviceProperties(BluetoothDevice device) {
63ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        synchronized (mDevices) {
64ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            return mDevices.get(device);
65ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
66ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
67ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
68ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    BluetoothDevice getDevice(byte[] address) {
69ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        for (BluetoothDevice dev : mDevices.keySet()) {
70ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            if (dev.getAddress().equals(Utils.getAddressStringFromByte(address))) {
71ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return dev;
72ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
73ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
74ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        return null;
75ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
76ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
77ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    DeviceProperties addDeviceProperties(byte[] address) {
78ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        synchronized (mDevices) {
79ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            DeviceProperties prop = new DeviceProperties();
80ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            BluetoothDevice device =
81ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                    mAdapter.getRemoteDevice(Utils.getAddressStringFromByte(address));
8274a598e0a8d50343227e3f96bbe56c6be1c240f9Ganesh Ganapathi Batta            prop.mAddress = address;
83ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            mDevices.put(device, prop);
84ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            return prop;
85ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
86ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
87ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
88ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    class DeviceProperties {
89ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private String mName;
90ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private byte[] mAddress;
91ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private int mBluetoothClass;
92ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private short mRssi;
93ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private ParcelUuid[] mUuids;
94ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private int mDeviceType;
95ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private String mAlias;
96ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private int mBondState;
97ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
98ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        DeviceProperties() {
99ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            mBondState = BluetoothDevice.BOND_NONE;
100ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
101ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
102ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
103ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return the mName
104ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
105ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        String getName() {
106ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
107ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mName;
108ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
109ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
110ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
111ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
112ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return the mClass
113ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
114ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        int getBluetoothClass() {
115ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
116ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mBluetoothClass;
117ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
118ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
119ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
120ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
121ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return the mUuids
122ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
123ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        ParcelUuid[] getUuids() {
124ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
125ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mUuids;
126ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
127ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
128ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
129ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
130ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return the mAddress
131ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
132ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        byte[] getAddress() {
133ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
134ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mAddress;
135ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
136ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
137ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
138ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
139ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return mRssi
140ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
141ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        short getRssi() {
142ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
143ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mRssi;
144ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
145ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
146ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
147ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
148ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         *
149ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return mDeviceType
150ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
151ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        int getDeviceType() {
152ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
153ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mDeviceType;
154ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
155ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
156ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
157ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
158ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return the mAlias
159ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
160ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        String getAlias() {
161ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
162ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mAlias;
163ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
164ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
165ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
166ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
167ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @param mAlias the mAlias to set
168ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
169ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        void setAlias(String mAlias) {
170ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
171ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                mAdapterService.setDevicePropertyNative(mAddress,
172ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                    AbstractionLayer.BT_PROPERTY_REMOTE_FRIENDLY_NAME, mAlias.getBytes());
173ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
174ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
175ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
176ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
177ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @param mBondState the mBondState to set
178ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
179ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        void setBondState(int mBondState) {
180ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
181ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                this.mBondState = mBondState;
182d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                if (mBondState == BluetoothDevice.BOND_NONE)
183d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                {
184d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                    /* Clearing the Uuids local copy when the device is unpaired. If not cleared,
185d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                    cachedBluetoothDevice issued a connect using the local cached copy of uuids,
186d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                    without waiting for the ACTION_UUID intent.
187d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                    This was resulting in multiple calls to connect().*/
188d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                    mUuids = null;
189d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                }
190ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
191ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
192ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
193ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
194ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return the mBondState
195ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
196ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        int getBondState() {
197ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
198ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mBondState;
199ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
200ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
201ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
202ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
203ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
204ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private void sendUuidIntent(BluetoothDevice device) {
205ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        DeviceProperties prop = getDeviceProperties(device);
206ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Intent intent = new Intent(BluetoothDevice.ACTION_UUID);
207ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
208c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc        intent.putExtra(BluetoothDevice.EXTRA_UUID, prop == null? null: prop.mUuids);
20974ae04c73312403e89db0f8e9bd9601d403b4783fredc        mAdapterService.sendBroadcast(intent, AdapterService.BLUETOOTH_ADMIN_PERM);
210c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc
211c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc        //Remove the outstanding UUID request
212c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc        mSdpTracker.remove(device);
213ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
214ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
2156de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera    private void sendDisplayPinIntent(byte[] address, int pin) {
2166de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera        Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);
2176de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, getDevice(address));
2186de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera        intent.putExtra(BluetoothDevice.EXTRA_PAIRING_KEY, pin);
2196de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera        intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
2206de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera                    BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN);
22174ae04c73312403e89db0f8e9bd9601d403b4783fredc        mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_ADMIN_PERM);
2226de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera    }
2236de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera
224ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    void devicePropertyChangedCallback(byte[] address, int[] types, byte[][] values) {
225ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Intent intent;
226ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        byte[] val;
227ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        int type;
228ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        BluetoothDevice bdDevice = getDevice(address);
229ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        DeviceProperties device;
230ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        if (bdDevice == null) {
231ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            device = addDeviceProperties(address);
232ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            bdDevice = getDevice(address);
233ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        } else {
234ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            device = getDeviceProperties(bdDevice);
235ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
236ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
237ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        for (int j = 0; j < types.length; j++) {
238ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            type = types[j];
239ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            val = values[j];
240caa3723eaa20e57aac086d54a41106108503debczzy            if(val.length <= 0)
241caa3723eaa20e57aac086d54a41106108503debczzy                errorLog("devicePropertyChangedCallback: bdDevice: " + bdDevice + ", value is empty for type: " + type);
242caa3723eaa20e57aac086d54a41106108503debczzy            else {
243caa3723eaa20e57aac086d54a41106108503debczzy                synchronized(mObject) {
244caa3723eaa20e57aac086d54a41106108503debczzy                    switch (type) {
245caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_BDNAME:
246caa3723eaa20e57aac086d54a41106108503debczzy                            device.mName = new String(val);
247caa3723eaa20e57aac086d54a41106108503debczzy                            intent = new Intent(BluetoothDevice.ACTION_NAME_CHANGED);
248caa3723eaa20e57aac086d54a41106108503debczzy                            intent.putExtra(BluetoothDevice.EXTRA_DEVICE, bdDevice);
249caa3723eaa20e57aac086d54a41106108503debczzy                            intent.putExtra(BluetoothDevice.EXTRA_NAME, device.mName);
250caa3723eaa20e57aac086d54a41106108503debczzy                            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
251caa3723eaa20e57aac086d54a41106108503debczzy                            mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);
252caa3723eaa20e57aac086d54a41106108503debczzy                            debugLog("Remote Device name is: " + device.mName);
253caa3723eaa20e57aac086d54a41106108503debczzy                            break;
254caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_REMOTE_FRIENDLY_NAME:
255caa3723eaa20e57aac086d54a41106108503debczzy                            if (device.mAlias != null) {
256caa3723eaa20e57aac086d54a41106108503debczzy                                System.arraycopy(val, 0, device.mAlias, 0, val.length);
257caa3723eaa20e57aac086d54a41106108503debczzy                            }
2580322ae1f5e8ea55c391145e7925cb5962a97c11fGanesh Ganapathi Batta                            else {
2590322ae1f5e8ea55c391145e7925cb5962a97c11fGanesh Ganapathi Batta                                device.mAlias = new String(val);
2600322ae1f5e8ea55c391145e7925cb5962a97c11fGanesh Ganapathi Batta                            }
261caa3723eaa20e57aac086d54a41106108503debczzy                            break;
262caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_BDADDR:
263caa3723eaa20e57aac086d54a41106108503debczzy                            device.mAddress = val;
264caa3723eaa20e57aac086d54a41106108503debczzy                            debugLog("Remote Address is:" + Utils.getAddressStringFromByte(val));
265caa3723eaa20e57aac086d54a41106108503debczzy                            break;
266caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_CLASS_OF_DEVICE:
267caa3723eaa20e57aac086d54a41106108503debczzy                            device.mBluetoothClass =  Utils.byteArrayToInt(val);
268caa3723eaa20e57aac086d54a41106108503debczzy                            intent = new Intent(BluetoothDevice.ACTION_CLASS_CHANGED);
269caa3723eaa20e57aac086d54a41106108503debczzy                            intent.putExtra(BluetoothDevice.EXTRA_DEVICE, bdDevice);
270caa3723eaa20e57aac086d54a41106108503debczzy                            intent.putExtra(BluetoothDevice.EXTRA_CLASS,
271caa3723eaa20e57aac086d54a41106108503debczzy                                    new BluetoothClass(device.mBluetoothClass));
272caa3723eaa20e57aac086d54a41106108503debczzy                            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
273caa3723eaa20e57aac086d54a41106108503debczzy                            mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);
274caa3723eaa20e57aac086d54a41106108503debczzy                            debugLog("Remote class is:" + device.mBluetoothClass);
275caa3723eaa20e57aac086d54a41106108503debczzy                            break;
276caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_UUIDS:
277caa3723eaa20e57aac086d54a41106108503debczzy                            int numUuids = val.length/AbstractionLayer.BT_UUID_SIZE;
278caa3723eaa20e57aac086d54a41106108503debczzy                            device.mUuids = Utils.byteArrayToUuid(val);
279caa3723eaa20e57aac086d54a41106108503debczzy                            sendUuidIntent(bdDevice);
280caa3723eaa20e57aac086d54a41106108503debczzy                            break;
281caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_TYPE_OF_DEVICE:
282caa3723eaa20e57aac086d54a41106108503debczzy                            device.mDeviceType = Utils.byteArrayToInt(val);
283caa3723eaa20e57aac086d54a41106108503debczzy                            break;
284caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_REMOTE_RSSI:
285caa3723eaa20e57aac086d54a41106108503debczzy                            device.mRssi = Utils.byteArrayToShort(val);
286caa3723eaa20e57aac086d54a41106108503debczzy                            break;
287caa3723eaa20e57aac086d54a41106108503debczzy                    }
288ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                }
289ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
290ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
291ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
292ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
293ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    void deviceFoundCallback(byte[] address) {
294ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        // The device properties are already registered - we can send the intent
295ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        // now
296ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        BluetoothDevice device = getDevice(address);
297ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        debugLog("deviceFoundCallback: Remote Address is:" + device);
298ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        DeviceProperties deviceProp = getDeviceProperties(device);
299ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        if (deviceProp == null) {
300ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            errorLog("Device Properties is null for Device:" + device);
301ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            return;
302ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
303ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
304ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Intent intent = new Intent(BluetoothDevice.ACTION_FOUND);
305ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
306ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_CLASS,
307ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                new BluetoothClass(Integer.valueOf(deviceProp.mBluetoothClass)));
308ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_RSSI, deviceProp.mRssi);
309ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_NAME, deviceProp.mName);
310ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
31174ae04c73312403e89db0f8e9bd9601d403b4783fredc        mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);
312ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
313ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
314ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    void pinRequestCallback(byte[] address, byte[] name, int cod) {
315ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        //TODO(BT): Get wakelock and update name and cod
3161444b5b09d07b1ad5ec2ce89b4267484be25e8bfJaikumar Ganesh        BluetoothDevice bdDevice = getDevice(address);
3171444b5b09d07b1ad5ec2ce89b4267484be25e8bfJaikumar Ganesh        if (bdDevice == null) {
3181444b5b09d07b1ad5ec2ce89b4267484be25e8bfJaikumar Ganesh            addDeviceProperties(address);
3191444b5b09d07b1ad5ec2ce89b4267484be25e8bfJaikumar Ganesh        }
3206de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera        BluetoothClass btClass = bdDevice.getBluetoothClass();
3216de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera        int btDeviceClass = btClass.getDeviceClass();
3226de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera        if (btDeviceClass == BluetoothClass.Device.PERIPHERAL_KEYBOARD ||
3236de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            btDeviceClass == BluetoothClass.Device.PERIPHERAL_KEYBOARD_POINTING) {
3246de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            // Its a keyboard. Follow the HID spec recommendation of creating the
3256de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            // passkey and displaying it to the user. If the keyboard doesn't follow
3266de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            // the spec recommendation, check if the keyboard has a fixed PIN zero
3276de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            // and pair.
3286de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            //TODO: Add sFixedPinZerosAutoPairKeyboard() and maintain list of devices that have fixed pin
3296de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            /*if (mAdapterService.isFixedPinZerosAutoPairKeyboard(address)) {
3306de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera                               mAdapterService.setPin(address, BluetoothDevice.convertPinToBytes("0000"));
3316de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera                               return;
3326de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera                     }*/
3336de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            // Generate a variable PIN. This is not truly random but good enough.
3346de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            int pin = (int) Math.floor(Math.random() * 1000000);
3356de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            sendDisplayPinIntent(address, pin);
3366de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            return;
3376de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera        }
338ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        infoLog("pinRequestCallback: " + address + " name:" + name + " cod:" +
339ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                cod);
340ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);
341ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, getDevice(address));
342ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
343ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                BluetoothDevice.PAIRING_VARIANT_PIN);
34474ae04c73312403e89db0f8e9bd9601d403b4783fredc        mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_ADMIN_PERM);
345ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        return;
346ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
347ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
348ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    void sspRequestCallback(byte[] address, byte[] name, int cod, int pairingVariant,
349ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            int passkey) {
350ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        //TODO(BT): Get wakelock and update name and cod
3511444b5b09d07b1ad5ec2ce89b4267484be25e8bfJaikumar Ganesh        BluetoothDevice bdDevice = getDevice(address);
3521444b5b09d07b1ad5ec2ce89b4267484be25e8bfJaikumar Ganesh        if (bdDevice == null) {
3531444b5b09d07b1ad5ec2ce89b4267484be25e8bfJaikumar Ganesh            addDeviceProperties(address);
3541444b5b09d07b1ad5ec2ce89b4267484be25e8bfJaikumar Ganesh        }
3551444b5b09d07b1ad5ec2ce89b4267484be25e8bfJaikumar Ganesh
356ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        infoLog("sspRequestCallback: " + address + " name: " + name + " cod: " +
357ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                cod + " pairingVariant " + pairingVariant + " passkey: " + passkey);
358ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        int variant;
359ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        boolean displayPasskey = false;
360ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        if (pairingVariant == AbstractionLayer.BT_SSP_VARIANT_PASSKEY_CONFIRMATION) {
361ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            variant = BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION;
362ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            displayPasskey = true;
363ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        } else if (pairingVariant == AbstractionLayer.BT_SSP_VARIANT_CONSENT) {
364b01cf4299897d227c7dbcbe4981b63a1ffe4ffcaSreenidhi T            variant = BluetoothDevice.PAIRING_VARIANT_CONSENT;
365ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        } else if (pairingVariant == AbstractionLayer.BT_SSP_VARIANT_PASSKEY_ENTRY) {
366ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            variant = BluetoothDevice.PAIRING_VARIANT_PASSKEY;
367b7e70fcf8923b9452074b2b4ee685da424cc7d93Kausik Sinnaswamy        } else if (pairingVariant == AbstractionLayer.BT_SSP_VARIANT_PASSKEY_NOTIFICATION) {
368581bb31a8165ff0f9c7d638cfe4a81aaaafa2dedJaikumar Ganesh            variant = BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY;
369581bb31a8165ff0f9c7d638cfe4a81aaaafa2dedJaikumar Ganesh	    displayPasskey = true;
370ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        } else {
371ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            errorLog("SSP Pairing variant not present");
372ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            return;
373ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
374ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        BluetoothDevice device = getDevice(address);
375ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        if (device == null) {
376ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh           warnLog("Device is not known for:" + Utils.getAddressStringFromByte(address));
377ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh           addDeviceProperties(address);
378ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh           device = getDevice(address);
379ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
380ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);
381ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
382ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        if (displayPasskey) {
383ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            intent.putExtra(BluetoothDevice.EXTRA_PAIRING_KEY, passkey);
384ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
385ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, variant);
38674ae04c73312403e89db0f8e9bd9601d403b4783fredc        mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_ADMIN_PERM);
387ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
388ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
38901a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy    void aclStateChangeCallback(int status, byte[] address, int newState) {
39001a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        BluetoothDevice device = getDevice(address);
39101a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy
39201a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        if (device == null) {
39301a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy            errorLog("aclStateChangeCallback: Device is NULL");
39401a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy            return;
39501a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        }
39601a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy
39701a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        Intent intent = null;
39801a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        if (newState == AbstractionLayer.BT_ACL_STATE_CONNECTED) {
39901a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy            intent = new Intent(BluetoothDevice.ACTION_ACL_CONNECTED);
40001a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy            debugLog("aclStateChangeCallback: State:Connected to Device:" + device);
40101a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        } else {
40201a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy            intent = new Intent(BluetoothDevice.ACTION_ACL_DISCONNECTED);
40301a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy            debugLog("aclStateChangeCallback: State:DisConnected to Device:" + device);
40401a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        }
40501a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
40601a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
40774ae04c73312403e89db0f8e9bd9601d403b4783fredc        mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);
40801a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy    }
409ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
410c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc    void fetchUuids(BluetoothDevice device) {
411ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        if (mSdpTracker.contains(device)) return;
412ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        mSdpTracker.add(device);
413ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
414ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Message message = mHandler.obtainMessage(MESSAGE_UUID_INTENT);
415ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        message.obj = device;
416ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        mHandler.sendMessageDelayed(message, UUID_INTENT_DELAY);
417ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
418c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc        //mAdapterService.getDevicePropertyNative(Utils.getBytesFromAddress(device.getAddress()), AbstractionLayer.BT_PROPERTY_UUIDS);
419c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc        mAdapterService.getRemoteServicesNative(Utils.getBytesFromAddress(device.getAddress()));
420ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
421ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
422ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private final Handler mHandler = new Handler() {
423ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        @Override
424ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        public void handleMessage(Message msg) {
425ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            switch (msg.what) {
426ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            case MESSAGE_UUID_INTENT:
427ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                BluetoothDevice device = (BluetoothDevice)msg.obj;
428ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                if (device != null) {
429ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                    sendUuidIntent(device);
430ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                }
431ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                break;
432ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
433ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
434ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    };
435ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
436ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private void errorLog(String msg) {
437ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Log.e(TAG, msg);
438ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
439ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
440ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private void debugLog(String msg) {
441ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        if (DBG) Log.e(TAG, msg);
442ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
443ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
444ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private void infoLog(String msg) {
445ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        if (DBG) Log.i(TAG, msg);
446ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
447ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
448ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private void warnLog(String msg) {
449ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Log.w(TAG, msg);
450ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
45174ae04c73312403e89db0f8e9bd9601d403b4783fredc
452ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh}
453