1ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh/*
2ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu * Copyright (C) 2012 The Android Open Source Project
3ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu *
4ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu * Licensed under the Apache License, Version 2.0 (the "License");
5ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu * you may not use this file except in compliance with the License.
6ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu * You may obtain a copy of the License at
7ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu *
8ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu *      http://www.apache.org/licenses/LICENSE-2.0
9ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu *
10ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu * Unless required by applicable law or agreed to in writing, software
11ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu * distributed under the License is distributed on an "AS IS" BASIS,
12ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu * See the License for the specific language governing permissions and
14ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu * limitations under the License.
15ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh */
16ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
17ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshpackage com.android.bluetooth.btservice;
18ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
19ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.bluetooth.BluetoothAdapter;
20ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.bluetooth.BluetoothClass;
21ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.bluetooth.BluetoothDevice;
22ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.content.Context;
23ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.content.Intent;
24ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.os.Handler;
25ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.os.Message;
26ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.os.ParcelUuid;
27ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.util.Log;
28ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
29ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport com.android.bluetooth.Utils;
30ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport com.android.bluetooth.btservice.RemoteDevices.DeviceProperties;
31ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
32ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport java.util.ArrayList;
33ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport java.util.HashMap;
34ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport java.util.LinkedList;
35ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
36ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
37ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshfinal class RemoteDevices {
38fd1da115cbf09b7dd9bca3c7d3a4fb816a835dc5Matthew Xie    private static final boolean DBG = false;
39ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private static final String TAG = "BluetoothRemoteDevices";
40ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
4174ae04c73312403e89db0f8e9bd9601d403b4783fredc
42ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private static BluetoothAdapter mAdapter;
43ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private static AdapterService mAdapterService;
44ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private static ArrayList<BluetoothDevice> mSdpTracker;
45ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
46ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private Object mObject = new Object();
47ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
48ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private static final int UUID_INTENT_DELAY = 6000;
49ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private static final int MESSAGE_UUID_INTENT = 1;
50ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
51ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private HashMap<BluetoothDevice, DeviceProperties> mDevices;
52ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
5374ae04c73312403e89db0f8e9bd9601d403b4783fredc    RemoteDevices(AdapterService service) {
54ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        mAdapter = BluetoothAdapter.getDefaultAdapter();
55ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        mAdapterService = service;
56ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        mSdpTracker = new ArrayList<BluetoothDevice>();
57ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        mDevices = new HashMap<BluetoothDevice, DeviceProperties>();
58ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
59ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
60ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
6174ae04c73312403e89db0f8e9bd9601d403b4783fredc    void cleanup() {
6215d36984a79d6e35c659edb0efdf929f0b526bd5Fred        if (mSdpTracker !=null)
6315d36984a79d6e35c659edb0efdf929f0b526bd5Fred            mSdpTracker.clear();
6415d36984a79d6e35c659edb0efdf929f0b526bd5Fred
6515d36984a79d6e35c659edb0efdf929f0b526bd5Fred        if (mDevices != null)
6615d36984a79d6e35c659edb0efdf929f0b526bd5Fred            mDevices.clear();
676654f5c903de510a70f9e72cd5ad7837b615d93ffredc    }
686654f5c903de510a70f9e72cd5ad7837b615d93ffredc
69ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    public Object Clone() throws CloneNotSupportedException {
70ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        throw new CloneNotSupportedException();
71ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
72ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
73ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    DeviceProperties getDeviceProperties(BluetoothDevice device) {
74ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        synchronized (mDevices) {
75ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            return mDevices.get(device);
76ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
77ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
78ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
79ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    BluetoothDevice getDevice(byte[] address) {
80ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        for (BluetoothDevice dev : mDevices.keySet()) {
81ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            if (dev.getAddress().equals(Utils.getAddressStringFromByte(address))) {
82ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return dev;
83ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
84ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
85ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        return null;
86ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
87ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
88ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    DeviceProperties addDeviceProperties(byte[] address) {
89ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        synchronized (mDevices) {
90ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            DeviceProperties prop = new DeviceProperties();
91ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            BluetoothDevice device =
92ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                    mAdapter.getRemoteDevice(Utils.getAddressStringFromByte(address));
9374a598e0a8d50343227e3f96bbe56c6be1c240f9Ganesh Ganapathi Batta            prop.mAddress = address;
94ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            mDevices.put(device, prop);
95ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            return prop;
96ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
97ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
98ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
99ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    class DeviceProperties {
100ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private String mName;
101ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private byte[] mAddress;
102ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private int mBluetoothClass;
103ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private short mRssi;
104ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private ParcelUuid[] mUuids;
105ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private int mDeviceType;
106ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private String mAlias;
107ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private int mBondState;
108ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
109ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        DeviceProperties() {
110ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            mBondState = BluetoothDevice.BOND_NONE;
111ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
112ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
113ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
114ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return the mName
115ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
116ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        String getName() {
117ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
118ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mName;
119ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
120ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
121ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
122ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
123ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return the mClass
124ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
125ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        int getBluetoothClass() {
126ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
127ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mBluetoothClass;
128ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
129ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
130ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
131ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
132ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return the mUuids
133ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
134ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        ParcelUuid[] getUuids() {
135ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
136ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mUuids;
137ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
138ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
139ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
140ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
141ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return the mAddress
142ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
143ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        byte[] getAddress() {
144ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
145ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mAddress;
146ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
147ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
148ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
149ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
150ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return mRssi
151ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
152ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        short getRssi() {
153ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
154ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mRssi;
155ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
156ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
157ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
158ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
159ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return mDeviceType
160ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
161ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        int getDeviceType() {
162ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
163ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mDeviceType;
164ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
165ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
166ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
167ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
168ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return the mAlias
169ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
170ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        String getAlias() {
171ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
172ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mAlias;
173ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
174ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
175ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
176ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
177ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @param mAlias the mAlias to set
178ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
179ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        void setAlias(String mAlias) {
180ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
181ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                mAdapterService.setDevicePropertyNative(mAddress,
182ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                    AbstractionLayer.BT_PROPERTY_REMOTE_FRIENDLY_NAME, mAlias.getBytes());
183ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
184ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
185ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
186ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
187ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @param mBondState the mBondState to set
188ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
189ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        void setBondState(int mBondState) {
190ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
191ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                this.mBondState = mBondState;
192d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                if (mBondState == BluetoothDevice.BOND_NONE)
193d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                {
194d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                    /* Clearing the Uuids local copy when the device is unpaired. If not cleared,
195d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                    cachedBluetoothDevice issued a connect using the local cached copy of uuids,
196d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                    without waiting for the ACTION_UUID intent.
197d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                    This was resulting in multiple calls to connect().*/
198d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                    mUuids = null;
199d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                }
200ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
201ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
202ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
203ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
204ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return the mBondState
205ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
206ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        int getBondState() {
207ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
208ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mBondState;
209ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
210ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
211ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
212ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
213ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
214ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private void sendUuidIntent(BluetoothDevice device) {
215ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        DeviceProperties prop = getDeviceProperties(device);
216ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Intent intent = new Intent(BluetoothDevice.ACTION_UUID);
217ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
218c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc        intent.putExtra(BluetoothDevice.EXTRA_UUID, prop == null? null: prop.mUuids);
21974ae04c73312403e89db0f8e9bd9601d403b4783fredc        mAdapterService.sendBroadcast(intent, AdapterService.BLUETOOTH_ADMIN_PERM);
220c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc
221c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc        //Remove the outstanding UUID request
222c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc        mSdpTracker.remove(device);
223ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
224ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
2256de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera    private void sendDisplayPinIntent(byte[] address, int pin) {
2266de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera        Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);
2276de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, getDevice(address));
2286de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera        intent.putExtra(BluetoothDevice.EXTRA_PAIRING_KEY, pin);
2296de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera        intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
2306de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera                    BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN);
23174ae04c73312403e89db0f8e9bd9601d403b4783fredc        mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_ADMIN_PERM);
2326de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera    }
2336de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera
234ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    void devicePropertyChangedCallback(byte[] address, int[] types, byte[][] values) {
235ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Intent intent;
236ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        byte[] val;
237ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        int type;
238ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        BluetoothDevice bdDevice = getDevice(address);
239ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        DeviceProperties device;
240ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        if (bdDevice == null) {
241ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            device = addDeviceProperties(address);
242ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            bdDevice = getDevice(address);
243ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        } else {
244ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            device = getDeviceProperties(bdDevice);
245ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
246ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
247ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        for (int j = 0; j < types.length; j++) {
248ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            type = types[j];
249ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            val = values[j];
250caa3723eaa20e57aac086d54a41106108503debczzy            if(val.length <= 0)
251caa3723eaa20e57aac086d54a41106108503debczzy                errorLog("devicePropertyChangedCallback: bdDevice: " + bdDevice + ", value is empty for type: " + type);
252caa3723eaa20e57aac086d54a41106108503debczzy            else {
253caa3723eaa20e57aac086d54a41106108503debczzy                synchronized(mObject) {
254caa3723eaa20e57aac086d54a41106108503debczzy                    switch (type) {
255caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_BDNAME:
256caa3723eaa20e57aac086d54a41106108503debczzy                            device.mName = new String(val);
257caa3723eaa20e57aac086d54a41106108503debczzy                            intent = new Intent(BluetoothDevice.ACTION_NAME_CHANGED);
258caa3723eaa20e57aac086d54a41106108503debczzy                            intent.putExtra(BluetoothDevice.EXTRA_DEVICE, bdDevice);
259caa3723eaa20e57aac086d54a41106108503debczzy                            intent.putExtra(BluetoothDevice.EXTRA_NAME, device.mName);
260caa3723eaa20e57aac086d54a41106108503debczzy                            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
261caa3723eaa20e57aac086d54a41106108503debczzy                            mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);
262caa3723eaa20e57aac086d54a41106108503debczzy                            debugLog("Remote Device name is: " + device.mName);
263caa3723eaa20e57aac086d54a41106108503debczzy                            break;
264caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_REMOTE_FRIENDLY_NAME:
265caa3723eaa20e57aac086d54a41106108503debczzy                            if (device.mAlias != null) {
266caa3723eaa20e57aac086d54a41106108503debczzy                                System.arraycopy(val, 0, device.mAlias, 0, val.length);
267caa3723eaa20e57aac086d54a41106108503debczzy                            }
2680322ae1f5e8ea55c391145e7925cb5962a97c11fGanesh Ganapathi Batta                            else {
2690322ae1f5e8ea55c391145e7925cb5962a97c11fGanesh Ganapathi Batta                                device.mAlias = new String(val);
2700322ae1f5e8ea55c391145e7925cb5962a97c11fGanesh Ganapathi Batta                            }
271caa3723eaa20e57aac086d54a41106108503debczzy                            break;
272caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_BDADDR:
273caa3723eaa20e57aac086d54a41106108503debczzy                            device.mAddress = val;
274caa3723eaa20e57aac086d54a41106108503debczzy                            debugLog("Remote Address is:" + Utils.getAddressStringFromByte(val));
275caa3723eaa20e57aac086d54a41106108503debczzy                            break;
276caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_CLASS_OF_DEVICE:
277caa3723eaa20e57aac086d54a41106108503debczzy                            device.mBluetoothClass =  Utils.byteArrayToInt(val);
278caa3723eaa20e57aac086d54a41106108503debczzy                            intent = new Intent(BluetoothDevice.ACTION_CLASS_CHANGED);
279caa3723eaa20e57aac086d54a41106108503debczzy                            intent.putExtra(BluetoothDevice.EXTRA_DEVICE, bdDevice);
280caa3723eaa20e57aac086d54a41106108503debczzy                            intent.putExtra(BluetoothDevice.EXTRA_CLASS,
281caa3723eaa20e57aac086d54a41106108503debczzy                                    new BluetoothClass(device.mBluetoothClass));
282caa3723eaa20e57aac086d54a41106108503debczzy                            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
283caa3723eaa20e57aac086d54a41106108503debczzy                            mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);
284caa3723eaa20e57aac086d54a41106108503debczzy                            debugLog("Remote class is:" + device.mBluetoothClass);
285caa3723eaa20e57aac086d54a41106108503debczzy                            break;
286caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_UUIDS:
287caa3723eaa20e57aac086d54a41106108503debczzy                            int numUuids = val.length/AbstractionLayer.BT_UUID_SIZE;
288caa3723eaa20e57aac086d54a41106108503debczzy                            device.mUuids = Utils.byteArrayToUuid(val);
289caa3723eaa20e57aac086d54a41106108503debczzy                            sendUuidIntent(bdDevice);
290caa3723eaa20e57aac086d54a41106108503debczzy                            break;
291caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_TYPE_OF_DEVICE:
292d235269f06e99d76a0f63c8fe9e224e0f951f799Matthew Xie                            // The device type from hal layer, defined in bluetooth.h,
293d235269f06e99d76a0f63c8fe9e224e0f951f799Matthew Xie                            // matches the type defined in BluetoothDevice.java
294caa3723eaa20e57aac086d54a41106108503debczzy                            device.mDeviceType = Utils.byteArrayToInt(val);
295caa3723eaa20e57aac086d54a41106108503debczzy                            break;
296caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_REMOTE_RSSI:
2973cedf3d57aff20e1876a5b450f41b05e975cb8e0Matthew Xie                            // RSSI from hal is in one byte
2983cedf3d57aff20e1876a5b450f41b05e975cb8e0Matthew Xie                            device.mRssi = val[0];
299caa3723eaa20e57aac086d54a41106108503debczzy                            break;
300caa3723eaa20e57aac086d54a41106108503debczzy                    }
301ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                }
302ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
303ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
304ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
305ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
306ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    void deviceFoundCallback(byte[] address) {
307ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        // The device properties are already registered - we can send the intent
308ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        // now
309ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        BluetoothDevice device = getDevice(address);
310ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        debugLog("deviceFoundCallback: Remote Address is:" + device);
311ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        DeviceProperties deviceProp = getDeviceProperties(device);
312ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        if (deviceProp == null) {
313ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            errorLog("Device Properties is null for Device:" + device);
314ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            return;
315ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
316ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
317ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Intent intent = new Intent(BluetoothDevice.ACTION_FOUND);
318ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
319ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_CLASS,
320ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                new BluetoothClass(Integer.valueOf(deviceProp.mBluetoothClass)));
321ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_RSSI, deviceProp.mRssi);
322ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_NAME, deviceProp.mName);
323ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
32474ae04c73312403e89db0f8e9bd9601d403b4783fredc        mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);
325ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
326ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
327ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    void pinRequestCallback(byte[] address, byte[] name, int cod) {
328ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        //TODO(BT): Get wakelock and update name and cod
3291444b5b09d07b1ad5ec2ce89b4267484be25e8bfJaikumar Ganesh        BluetoothDevice bdDevice = getDevice(address);
3301444b5b09d07b1ad5ec2ce89b4267484be25e8bfJaikumar Ganesh        if (bdDevice == null) {
3311444b5b09d07b1ad5ec2ce89b4267484be25e8bfJaikumar Ganesh            addDeviceProperties(address);
3321444b5b09d07b1ad5ec2ce89b4267484be25e8bfJaikumar Ganesh        }
3336de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera        BluetoothClass btClass = bdDevice.getBluetoothClass();
3346de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera        int btDeviceClass = btClass.getDeviceClass();
3356de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera        if (btDeviceClass == BluetoothClass.Device.PERIPHERAL_KEYBOARD ||
3366de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            btDeviceClass == BluetoothClass.Device.PERIPHERAL_KEYBOARD_POINTING) {
3376de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            // Its a keyboard. Follow the HID spec recommendation of creating the
3386de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            // passkey and displaying it to the user. If the keyboard doesn't follow
3396de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            // the spec recommendation, check if the keyboard has a fixed PIN zero
3406de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            // and pair.
3416de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            //TODO: Add sFixedPinZerosAutoPairKeyboard() and maintain list of devices that have fixed pin
3426de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            /*if (mAdapterService.isFixedPinZerosAutoPairKeyboard(address)) {
3436de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera                               mAdapterService.setPin(address, BluetoothDevice.convertPinToBytes("0000"));
3446de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera                               return;
3456de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera                     }*/
3466de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            // Generate a variable PIN. This is not truly random but good enough.
3476de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            int pin = (int) Math.floor(Math.random() * 1000000);
3486de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            sendDisplayPinIntent(address, pin);
3496de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera            return;
3506de8229571ce56dcb0010c63cbef65e01ecd5c2aPriti Aghera        }
351ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        infoLog("pinRequestCallback: " + address + " name:" + name + " cod:" +
352ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                cod);
353ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);
354ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, getDevice(address));
355ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
356ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                BluetoothDevice.PAIRING_VARIANT_PIN);
35774ae04c73312403e89db0f8e9bd9601d403b4783fredc        mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_ADMIN_PERM);
358ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        return;
359ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
360ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
361ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    void sspRequestCallback(byte[] address, byte[] name, int cod, int pairingVariant,
362ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            int passkey) {
363ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        //TODO(BT): Get wakelock and update name and cod
3641444b5b09d07b1ad5ec2ce89b4267484be25e8bfJaikumar Ganesh        BluetoothDevice bdDevice = getDevice(address);
3651444b5b09d07b1ad5ec2ce89b4267484be25e8bfJaikumar Ganesh        if (bdDevice == null) {
3661444b5b09d07b1ad5ec2ce89b4267484be25e8bfJaikumar Ganesh            addDeviceProperties(address);
3671444b5b09d07b1ad5ec2ce89b4267484be25e8bfJaikumar Ganesh        }
3681444b5b09d07b1ad5ec2ce89b4267484be25e8bfJaikumar Ganesh
369ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        infoLog("sspRequestCallback: " + address + " name: " + name + " cod: " +
370ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                cod + " pairingVariant " + pairingVariant + " passkey: " + passkey);
371ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        int variant;
372ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        boolean displayPasskey = false;
373ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        if (pairingVariant == AbstractionLayer.BT_SSP_VARIANT_PASSKEY_CONFIRMATION) {
374ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            variant = BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION;
375ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            displayPasskey = true;
376ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        } else if (pairingVariant == AbstractionLayer.BT_SSP_VARIANT_CONSENT) {
377b01cf4299897d227c7dbcbe4981b63a1ffe4ffcaSreenidhi T            variant = BluetoothDevice.PAIRING_VARIANT_CONSENT;
378ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        } else if (pairingVariant == AbstractionLayer.BT_SSP_VARIANT_PASSKEY_ENTRY) {
379ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            variant = BluetoothDevice.PAIRING_VARIANT_PASSKEY;
380b7e70fcf8923b9452074b2b4ee685da424cc7d93Kausik Sinnaswamy        } else if (pairingVariant == AbstractionLayer.BT_SSP_VARIANT_PASSKEY_NOTIFICATION) {
381581bb31a8165ff0f9c7d638cfe4a81aaaafa2dedJaikumar Ganesh            variant = BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY;
382581bb31a8165ff0f9c7d638cfe4a81aaaafa2dedJaikumar Ganesh	    displayPasskey = true;
383ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        } else {
384ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            errorLog("SSP Pairing variant not present");
385ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            return;
386ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
387ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        BluetoothDevice device = getDevice(address);
388ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        if (device == null) {
389ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh           warnLog("Device is not known for:" + Utils.getAddressStringFromByte(address));
390ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh           addDeviceProperties(address);
391ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh           device = getDevice(address);
392ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
393ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);
394ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
395ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        if (displayPasskey) {
396ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            intent.putExtra(BluetoothDevice.EXTRA_PAIRING_KEY, passkey);
397ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
398ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, variant);
39974ae04c73312403e89db0f8e9bd9601d403b4783fredc        mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_ADMIN_PERM);
400ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
401ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
40201a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy    void aclStateChangeCallback(int status, byte[] address, int newState) {
40301a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        BluetoothDevice device = getDevice(address);
40401a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy
40501a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        if (device == null) {
40601a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy            errorLog("aclStateChangeCallback: Device is NULL");
40701a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy            return;
40801a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        }
40901a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy
41001a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        Intent intent = null;
41101a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        if (newState == AbstractionLayer.BT_ACL_STATE_CONNECTED) {
41201a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy            intent = new Intent(BluetoothDevice.ACTION_ACL_CONNECTED);
41301a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy            debugLog("aclStateChangeCallback: State:Connected to Device:" + device);
41401a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        } else {
41501a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy            intent = new Intent(BluetoothDevice.ACTION_ACL_DISCONNECTED);
41601a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy            debugLog("aclStateChangeCallback: State:DisConnected to Device:" + device);
41701a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        }
41801a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
41901a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
42074ae04c73312403e89db0f8e9bd9601d403b4783fredc        mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);
42101a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy    }
422ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
423c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc    void fetchUuids(BluetoothDevice device) {
424ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        if (mSdpTracker.contains(device)) return;
425ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        mSdpTracker.add(device);
426ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
427ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Message message = mHandler.obtainMessage(MESSAGE_UUID_INTENT);
428ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        message.obj = device;
429ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        mHandler.sendMessageDelayed(message, UUID_INTENT_DELAY);
430ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
431c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc        //mAdapterService.getDevicePropertyNative(Utils.getBytesFromAddress(device.getAddress()), AbstractionLayer.BT_PROPERTY_UUIDS);
432c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc        mAdapterService.getRemoteServicesNative(Utils.getBytesFromAddress(device.getAddress()));
433ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
434ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
435ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private final Handler mHandler = new Handler() {
436ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        @Override
437ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        public void handleMessage(Message msg) {
438ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            switch (msg.what) {
439ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            case MESSAGE_UUID_INTENT:
440ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                BluetoothDevice device = (BluetoothDevice)msg.obj;
441ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                if (device != null) {
442ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                    sendUuidIntent(device);
443ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                }
444ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                break;
445ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
446ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
447ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    };
448ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
449ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private void errorLog(String msg) {
450ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Log.e(TAG, msg);
451ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
452ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
453ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private void debugLog(String msg) {
4546458cffaca286611e898c75ad86e2d98c89008b6Matthew Xie        if (DBG) Log.d(TAG, msg);
455ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
456ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
457ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private void infoLog(String msg) {
458ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        if (DBG) Log.i(TAG, msg);
459ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
460ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
461ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private void warnLog(String msg) {
462ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Log.w(TAG, msg);
463ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
46474ae04c73312403e89db0f8e9bd9601d403b4783fredc
465ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh}
466