RemoteDevices.java revision f19f1ac64a5fefb248ab15b918d009b926c99dde
1ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh/*
22c0df0560ad5ae3fd6022b17b17a6a70143e216aHemant Gupta * Copyright (C) 2012-2014 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.Intent;
23ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.os.Handler;
24ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.os.Message;
25ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.os.ParcelUuid;
26ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport android.util.Log;
27ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
28ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport com.android.bluetooth.Utils;
29ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
307aa3a71e508e4f95a0a74f41ab8389979809191bJay Civelliimport java.util.concurrent.atomic.AtomicInteger;
31ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport java.util.ArrayList;
32ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshimport java.util.HashMap;
33ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
34ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
35ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganeshfinal class RemoteDevices {
36fd1da115cbf09b7dd9bca3c7d3a4fb816a835dc5Matthew Xie    private static final boolean DBG = false;
37ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private static final String TAG = "BluetoothRemoteDevices";
38ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
3974ae04c73312403e89db0f8e9bd9601d403b4783fredc
40ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private static BluetoothAdapter mAdapter;
41ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private static AdapterService mAdapterService;
42ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private static ArrayList<BluetoothDevice> mSdpTracker;
43ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private Object mObject = new Object();
44ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
45ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private static final int UUID_INTENT_DELAY = 6000;
46ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private static final int MESSAGE_UUID_INTENT = 1;
47ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
48ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private HashMap<BluetoothDevice, DeviceProperties> mDevices;
49ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
5074ae04c73312403e89db0f8e9bd9601d403b4783fredc    RemoteDevices(AdapterService service) {
51ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        mAdapter = BluetoothAdapter.getDefaultAdapter();
52ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        mAdapterService = service;
53ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        mSdpTracker = new ArrayList<BluetoothDevice>();
54ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        mDevices = new HashMap<BluetoothDevice, DeviceProperties>();
55ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
56ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
57ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
5874ae04c73312403e89db0f8e9bd9601d403b4783fredc    void cleanup() {
5915d36984a79d6e35c659edb0efdf929f0b526bd5Fred        if (mSdpTracker !=null)
6015d36984a79d6e35c659edb0efdf929f0b526bd5Fred            mSdpTracker.clear();
6115d36984a79d6e35c659edb0efdf929f0b526bd5Fred
6215d36984a79d6e35c659edb0efdf929f0b526bd5Fred        if (mDevices != null)
6315d36984a79d6e35c659edb0efdf929f0b526bd5Fred            mDevices.clear();
646654f5c903de510a70f9e72cd5ad7837b615d93ffredc    }
656654f5c903de510a70f9e72cd5ad7837b615d93ffredc
663a91ac3eefe23a22aa0abc1a89ac253a2b0dbae9Nick Kralevich    @Override
673a91ac3eefe23a22aa0abc1a89ac253a2b0dbae9Nick Kralevich    public Object clone() throws CloneNotSupportedException {
68ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        throw new CloneNotSupportedException();
69ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
70ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
71ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    DeviceProperties getDeviceProperties(BluetoothDevice device) {
72ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        synchronized (mDevices) {
73ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            return mDevices.get(device);
74ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
75ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
76ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
77ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    BluetoothDevice getDevice(byte[] address) {
78ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        for (BluetoothDevice dev : mDevices.keySet()) {
79ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            if (dev.getAddress().equals(Utils.getAddressStringFromByte(address))) {
80ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return dev;
81ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
82ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
83ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        return null;
84ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
85ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
86ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    DeviceProperties addDeviceProperties(byte[] address) {
87ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        synchronized (mDevices) {
88ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            DeviceProperties prop = new DeviceProperties();
89ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            BluetoothDevice device =
90ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                    mAdapter.getRemoteDevice(Utils.getAddressStringFromByte(address));
9174a598e0a8d50343227e3f96bbe56c6be1c240f9Ganesh Ganapathi Batta            prop.mAddress = address;
92ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            mDevices.put(device, prop);
93ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            return prop;
94ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
95ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
96ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
97ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    class DeviceProperties {
98ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private String mName;
99ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private byte[] mAddress;
100ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private int mBluetoothClass;
101ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private short mRssi;
102ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private ParcelUuid[] mUuids;
103ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private int mDeviceType;
104ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private String mAlias;
105ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        private int mBondState;
106ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
107ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        DeviceProperties() {
108ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            mBondState = BluetoothDevice.BOND_NONE;
109ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
110ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
111ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
112ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return the mName
113ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
114ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        String getName() {
115ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
116ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mName;
117ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
118ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
119ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
120ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
121ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return the mClass
122ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
123ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        int getBluetoothClass() {
124ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
125ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mBluetoothClass;
126ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
127ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
128ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
129ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
130ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return the mUuids
131ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
132ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        ParcelUuid[] getUuids() {
133ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
134ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mUuids;
135ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
136ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
137ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
138ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
139ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return the mAddress
140ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
141ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        byte[] getAddress() {
142ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
143ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mAddress;
144ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
145ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
146ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
147ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
148ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return mRssi
149ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
150ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        short getRssi() {
151ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
152ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mRssi;
153ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
154ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
155ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
156ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
157ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return mDeviceType
158ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
159ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        int getDeviceType() {
160ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
161ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mDeviceType;
162ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
163ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
164ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
165ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
166ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return the mAlias
167ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
168ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        String getAlias() {
169ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
170ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mAlias;
171ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
172ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
173ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
174ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
175ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @param mAlias the mAlias to set
176ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
177ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        void setAlias(String mAlias) {
178ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
17974277677d2a51d7c8ff60a778889f71e69763c21Matthew Xie                this.mAlias = mAlias;
180ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                mAdapterService.setDevicePropertyNative(mAddress,
181ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                    AbstractionLayer.BT_PROPERTY_REMOTE_FRIENDLY_NAME, mAlias.getBytes());
182ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
183ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
184ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
185ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
186ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @param mBondState the mBondState to set
187ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
188ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        void setBondState(int mBondState) {
189ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
190ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                this.mBondState = mBondState;
191d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                if (mBondState == BluetoothDevice.BOND_NONE)
192d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                {
193d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                    /* Clearing the Uuids local copy when the device is unpaired. If not cleared,
194d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                    cachedBluetoothDevice issued a connect using the local cached copy of uuids,
195d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                    without waiting for the ACTION_UUID intent.
196d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                    This was resulting in multiple calls to connect().*/
197d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                    mUuids = null;
198d807ba48ed4418c1a60549abe1c3a98ced8c428dSreenidhi T                }
199ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
200ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
201ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
202ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        /**
203ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         * @return the mBondState
204ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh         */
205ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        int getBondState() {
206ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            synchronized (mObject) {
207ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                return mBondState;
208ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
209ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
2107aa3a71e508e4f95a0a74f41ab8389979809191bJay Civelli    }
211ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
212ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private void sendUuidIntent(BluetoothDevice device) {
213ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        DeviceProperties prop = getDeviceProperties(device);
214ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Intent intent = new Intent(BluetoothDevice.ACTION_UUID);
215ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
216c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc        intent.putExtra(BluetoothDevice.EXTRA_UUID, prop == null? null: prop.mUuids);
21734409f35db6e9ca74f727e3fe92141388ae5b069Ganesh Ganapathi Batta        mAdapterService.initProfilePriorities(device, prop.mUuids);
21874ae04c73312403e89db0f8e9bd9601d403b4783fredc        mAdapterService.sendBroadcast(intent, AdapterService.BLUETOOTH_ADMIN_PERM);
219c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc
220c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc        //Remove the outstanding UUID request
221c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc        mSdpTracker.remove(device);
222ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
223ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
22411b41d8123241ba8e693e21c4db308bacff23f57Mudumba Ananth
225ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    void devicePropertyChangedCallback(byte[] address, int[] types, byte[][] values) {
226ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Intent intent;
227ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        byte[] val;
228ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        int type;
229ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        BluetoothDevice bdDevice = getDevice(address);
230ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        DeviceProperties device;
231ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        if (bdDevice == null) {
232ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            device = addDeviceProperties(address);
233ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            bdDevice = getDevice(address);
234ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        } else {
235ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            device = getDeviceProperties(bdDevice);
236ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
237ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
238ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        for (int j = 0; j < types.length; j++) {
239ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            type = types[j];
240ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            val = values[j];
241caa3723eaa20e57aac086d54a41106108503debczzy            if(val.length <= 0)
2425a60e47497f21f64e6d79420dc4c56c1907df22akschulz                errorLog("devicePropertyChangedCallback: bdDevice: " + bdDevice
243bbb4110b455b3aa29106d5b4f0a37e1be8e09475Casper Bonde                        + ", value is empty for type: " + type);
244caa3723eaa20e57aac086d54a41106108503debczzy            else {
245caa3723eaa20e57aac086d54a41106108503debczzy                synchronized(mObject) {
246caa3723eaa20e57aac086d54a41106108503debczzy                    switch (type) {
247caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_BDNAME:
248caa3723eaa20e57aac086d54a41106108503debczzy                            device.mName = new String(val);
249caa3723eaa20e57aac086d54a41106108503debczzy                            intent = new Intent(BluetoothDevice.ACTION_NAME_CHANGED);
250caa3723eaa20e57aac086d54a41106108503debczzy                            intent.putExtra(BluetoothDevice.EXTRA_DEVICE, bdDevice);
251caa3723eaa20e57aac086d54a41106108503debczzy                            intent.putExtra(BluetoothDevice.EXTRA_NAME, device.mName);
252caa3723eaa20e57aac086d54a41106108503debczzy                            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
253caa3723eaa20e57aac086d54a41106108503debczzy                            mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);
254caa3723eaa20e57aac086d54a41106108503debczzy                            debugLog("Remote Device name is: " + device.mName);
255caa3723eaa20e57aac086d54a41106108503debczzy                            break;
256caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_REMOTE_FRIENDLY_NAME:
257caa3723eaa20e57aac086d54a41106108503debczzy                            if (device.mAlias != null) {
258caa3723eaa20e57aac086d54a41106108503debczzy                                System.arraycopy(val, 0, device.mAlias, 0, val.length);
259caa3723eaa20e57aac086d54a41106108503debczzy                            }
2600322ae1f5e8ea55c391145e7925cb5962a97c11fGanesh Ganapathi Batta                            else {
2610322ae1f5e8ea55c391145e7925cb5962a97c11fGanesh Ganapathi Batta                                device.mAlias = new String(val);
2620322ae1f5e8ea55c391145e7925cb5962a97c11fGanesh Ganapathi Batta                            }
263caa3723eaa20e57aac086d54a41106108503debczzy                            break;
264caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_BDADDR:
265caa3723eaa20e57aac086d54a41106108503debczzy                            device.mAddress = val;
266caa3723eaa20e57aac086d54a41106108503debczzy                            debugLog("Remote Address is:" + Utils.getAddressStringFromByte(val));
267caa3723eaa20e57aac086d54a41106108503debczzy                            break;
268caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_CLASS_OF_DEVICE:
269caa3723eaa20e57aac086d54a41106108503debczzy                            device.mBluetoothClass =  Utils.byteArrayToInt(val);
270caa3723eaa20e57aac086d54a41106108503debczzy                            intent = new Intent(BluetoothDevice.ACTION_CLASS_CHANGED);
271caa3723eaa20e57aac086d54a41106108503debczzy                            intent.putExtra(BluetoothDevice.EXTRA_DEVICE, bdDevice);
272caa3723eaa20e57aac086d54a41106108503debczzy                            intent.putExtra(BluetoothDevice.EXTRA_CLASS,
273caa3723eaa20e57aac086d54a41106108503debczzy                                    new BluetoothClass(device.mBluetoothClass));
274caa3723eaa20e57aac086d54a41106108503debczzy                            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
275caa3723eaa20e57aac086d54a41106108503debczzy                            mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);
276caa3723eaa20e57aac086d54a41106108503debczzy                            debugLog("Remote class is:" + device.mBluetoothClass);
277caa3723eaa20e57aac086d54a41106108503debczzy                            break;
278caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_UUIDS:
279caa3723eaa20e57aac086d54a41106108503debczzy                            int numUuids = val.length/AbstractionLayer.BT_UUID_SIZE;
280caa3723eaa20e57aac086d54a41106108503debczzy                            device.mUuids = Utils.byteArrayToUuid(val);
281caa3723eaa20e57aac086d54a41106108503debczzy                            sendUuidIntent(bdDevice);
282caa3723eaa20e57aac086d54a41106108503debczzy                            break;
283caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_TYPE_OF_DEVICE:
284d235269f06e99d76a0f63c8fe9e224e0f951f799Matthew Xie                            // The device type from hal layer, defined in bluetooth.h,
285d235269f06e99d76a0f63c8fe9e224e0f951f799Matthew Xie                            // matches the type defined in BluetoothDevice.java
286caa3723eaa20e57aac086d54a41106108503debczzy                            device.mDeviceType = Utils.byteArrayToInt(val);
287caa3723eaa20e57aac086d54a41106108503debczzy                            break;
288caa3723eaa20e57aac086d54a41106108503debczzy                        case AbstractionLayer.BT_PROPERTY_REMOTE_RSSI:
2893cedf3d57aff20e1876a5b450f41b05e975cb8e0Matthew Xie                            // RSSI from hal is in one byte
2903cedf3d57aff20e1876a5b450f41b05e975cb8e0Matthew Xie                            device.mRssi = val[0];
291caa3723eaa20e57aac086d54a41106108503debczzy                            break;
292caa3723eaa20e57aac086d54a41106108503debczzy                    }
293ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                }
294ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
295ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
296ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
297ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
298ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    void deviceFoundCallback(byte[] address) {
299ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        // The device properties are already registered - we can send the intent
300ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        // now
301ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        BluetoothDevice device = getDevice(address);
302ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        debugLog("deviceFoundCallback: Remote Address is:" + device);
303ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        DeviceProperties deviceProp = getDeviceProperties(device);
304ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        if (deviceProp == null) {
305ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            errorLog("Device Properties is null for Device:" + device);
306ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            return;
307ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
308ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
309ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Intent intent = new Intent(BluetoothDevice.ACTION_FOUND);
310ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
311ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_CLASS,
312ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                new BluetoothClass(Integer.valueOf(deviceProp.mBluetoothClass)));
313ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_RSSI, deviceProp.mRssi);
314ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_NAME, deviceProp.mName);
315ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
31674ae04c73312403e89db0f8e9bd9601d403b4783fredc        mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);
317ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
318ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
31901a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy    void aclStateChangeCallback(int status, byte[] address, int newState) {
32001a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        BluetoothDevice device = getDevice(address);
32101a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy
32201a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        if (device == null) {
32301a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy            errorLog("aclStateChangeCallback: Device is NULL");
32401a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy            return;
32501a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        }
326f19f1ac64a5fefb248ab15b918d009b926c99ddeNitin Arora        int state = mAdapterService.getState();
327f19f1ac64a5fefb248ab15b918d009b926c99ddeNitin Arora        Log.e(TAG, "state" + state + "newState" + newState);
32801a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy
3297aa3a71e508e4f95a0a74f41ab8389979809191bJay Civelli        DeviceProperties prop = getDeviceProperties(device);
3307aa3a71e508e4f95a0a74f41ab8389979809191bJay Civelli        if (prop == null) {
331bbb4110b455b3aa29106d5b4f0a37e1be8e09475Casper Bonde //         errorLog("aclStateChangeCallback reported unknown device " + Arrays.toString(address));
3327aa3a71e508e4f95a0a74f41ab8389979809191bJay Civelli        }
33301a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        Intent intent = null;
33401a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        if (newState == AbstractionLayer.BT_ACL_STATE_CONNECTED) {
335f19f1ac64a5fefb248ab15b918d009b926c99ddeNitin Arora            if (state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_TURNING_ON) {
336f19f1ac64a5fefb248ab15b918d009b926c99ddeNitin Arora                intent = new Intent(BluetoothDevice.ACTION_ACL_CONNECTED);
337f19f1ac64a5fefb248ab15b918d009b926c99ddeNitin Arora            } else if (state == BluetoothAdapter.STATE_BLE_ON || state == BluetoothAdapter.STATE_BLE_TURNING_ON) {
338f19f1ac64a5fefb248ab15b918d009b926c99ddeNitin Arora                intent = new Intent(BluetoothAdapter.ACTION_BLE_ACL_CONNECTED);
339f19f1ac64a5fefb248ab15b918d009b926c99ddeNitin Arora            }
34001a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy            debugLog("aclStateChangeCallback: State:Connected to Device:" + device);
34101a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        } else {
342f19f1ac64a5fefb248ab15b918d009b926c99ddeNitin Arora            if (device.getBondState() == BluetoothDevice.BOND_BONDING) {
343f19f1ac64a5fefb248ab15b918d009b926c99ddeNitin Arora                /*Broadcasting PAIRING_CANCEL intent as well in this case*/
344f19f1ac64a5fefb248ab15b918d009b926c99ddeNitin Arora                intent = new Intent(BluetoothDevice.ACTION_PAIRING_CANCEL);
345f19f1ac64a5fefb248ab15b918d009b926c99ddeNitin Arora                intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
346f19f1ac64a5fefb248ab15b918d009b926c99ddeNitin Arora                mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);
347f19f1ac64a5fefb248ab15b918d009b926c99ddeNitin Arora            }
348f19f1ac64a5fefb248ab15b918d009b926c99ddeNitin Arora            if (state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_TURNING_OFF) {
349f19f1ac64a5fefb248ab15b918d009b926c99ddeNitin Arora                intent = new Intent(BluetoothDevice.ACTION_ACL_DISCONNECTED);
350f19f1ac64a5fefb248ab15b918d009b926c99ddeNitin Arora            } else if (state == BluetoothAdapter.STATE_BLE_ON || state == BluetoothAdapter.STATE_BLE_TURNING_OFF) {
351f19f1ac64a5fefb248ab15b918d009b926c99ddeNitin Arora                intent = new Intent(BluetoothAdapter.ACTION_BLE_ACL_DISCONNECTED);
352f19f1ac64a5fefb248ab15b918d009b926c99ddeNitin Arora            }
35301a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy            debugLog("aclStateChangeCallback: State:DisConnected to Device:" + device);
35401a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        }
35501a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
35601a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
35774ae04c73312403e89db0f8e9bd9601d403b4783fredc        mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);
35801a8cf98f070a6996b2e8974edc229ac402f3f0cKausik Sinnaswamy    }
359ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
3602c0df0560ad5ae3fd6022b17b17a6a70143e216aHemant Gupta
3612c0df0560ad5ae3fd6022b17b17a6a70143e216aHemant Gupta
362c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc    void fetchUuids(BluetoothDevice device) {
363ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        if (mSdpTracker.contains(device)) return;
364ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        mSdpTracker.add(device);
365ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
366ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Message message = mHandler.obtainMessage(MESSAGE_UUID_INTENT);
367ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        message.obj = device;
368ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        mHandler.sendMessageDelayed(message, UUID_INTENT_DELAY);
369ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
370c55ac7d42cf2d78c7edc67abf6c66813245b2c93fredc        mAdapterService.getRemoteServicesNative(Utils.getBytesFromAddress(device.getAddress()));
371ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
372ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
373ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private final Handler mHandler = new Handler() {
374ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        @Override
375ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        public void handleMessage(Message msg) {
376ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            switch (msg.what) {
377ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            case MESSAGE_UUID_INTENT:
378ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                BluetoothDevice device = (BluetoothDevice)msg.obj;
379ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                if (device != null) {
380ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                    sendUuidIntent(device);
381ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                }
382ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh                break;
383ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh            }
384ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        }
385ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    };
386ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
387ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private void errorLog(String msg) {
388ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Log.e(TAG, msg);
389ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
390ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
391ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private void debugLog(String msg) {
3926458cffaca286611e898c75ad86e2d98c89008b6Matthew Xie        if (DBG) Log.d(TAG, msg);
393ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
394ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
395ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private void infoLog(String msg) {
396ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        if (DBG) Log.i(TAG, msg);
397ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
398ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh
399ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    private void warnLog(String msg) {
400ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh        Log.w(TAG, msg);
401ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh    }
40274ae04c73312403e89db0f8e9bd9601d403b4783fredc
403ff4f17bf64978d0738c66e1b6dd70be8664efc24Jaikumar Ganesh}
404