RemoteDevices.java revision 7aa3a71e508e4f95a0a74f41ab8389979809191b
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.bluetooth.btservice;
18
19import android.bluetooth.BluetoothAdapter;
20import android.bluetooth.BluetoothClass;
21import android.bluetooth.BluetoothDevice;
22import android.content.Context;
23import android.content.Intent;
24import android.os.Handler;
25import android.os.Message;
26import android.os.ParcelUuid;
27import android.util.Log;
28
29import com.android.bluetooth.Utils;
30import com.android.bluetooth.btservice.RemoteDevices.DeviceProperties;
31
32import java.util.concurrent.atomic.AtomicInteger;
33import java.util.ArrayList;
34import java.util.Arrays;
35import java.util.HashMap;
36import java.util.LinkedList;
37
38
39final class RemoteDevices {
40    private static final boolean DBG = false;
41    private static final String TAG = "BluetoothRemoteDevices";
42
43
44    private static BluetoothAdapter mAdapter;
45    private static AdapterService mAdapterService;
46    private static ArrayList<BluetoothDevice> mSdpTracker;
47
48    private Object mObject = new Object();
49
50    private static final int UUID_INTENT_DELAY = 6000;
51    private static final int MESSAGE_UUID_INTENT = 1;
52
53    private HashMap<BluetoothDevice, DeviceProperties> mDevices;
54
55    RemoteDevices(AdapterService service) {
56        mAdapter = BluetoothAdapter.getDefaultAdapter();
57        mAdapterService = service;
58        mSdpTracker = new ArrayList<BluetoothDevice>();
59        mDevices = new HashMap<BluetoothDevice, DeviceProperties>();
60    }
61
62
63    void cleanup() {
64        if (mSdpTracker !=null)
65            mSdpTracker.clear();
66
67        if (mDevices != null)
68            mDevices.clear();
69    }
70
71    public Object Clone() throws CloneNotSupportedException {
72        throw new CloneNotSupportedException();
73    }
74
75    DeviceProperties getDeviceProperties(BluetoothDevice device) {
76        synchronized (mDevices) {
77            return mDevices.get(device);
78        }
79    }
80
81    BluetoothDevice getDevice(byte[] address) {
82        for (BluetoothDevice dev : mDevices.keySet()) {
83            if (dev.getAddress().equals(Utils.getAddressStringFromByte(address))) {
84                return dev;
85            }
86        }
87        return null;
88    }
89
90    DeviceProperties addDeviceProperties(byte[] address) {
91        synchronized (mDevices) {
92            DeviceProperties prop = new DeviceProperties();
93            BluetoothDevice device =
94                    mAdapter.getRemoteDevice(Utils.getAddressStringFromByte(address));
95            prop.mAddress = address;
96            mDevices.put(device, prop);
97            return prop;
98        }
99    }
100
101    class DeviceProperties {
102        private String mName;
103        private byte[] mAddress;
104        private int mBluetoothClass;
105        private short mRssi;
106        private ParcelUuid[] mUuids;
107        private int mDeviceType;
108        private String mAlias;
109        private int mBondState;
110        private AtomicInteger mOpenAclConnectionCount;
111
112        DeviceProperties() {
113            mBondState = BluetoothDevice.BOND_NONE;
114            mOpenAclConnectionCount = new AtomicInteger(0);
115        }
116
117        /**
118         * @return the mName
119         */
120        String getName() {
121            synchronized (mObject) {
122                return mName;
123            }
124        }
125
126        /**
127         * @return the mClass
128         */
129        int getBluetoothClass() {
130            synchronized (mObject) {
131                return mBluetoothClass;
132            }
133        }
134
135        /**
136         * @return the mUuids
137         */
138        ParcelUuid[] getUuids() {
139            synchronized (mObject) {
140                return mUuids;
141            }
142        }
143
144        /**
145         * @return the mAddress
146         */
147        byte[] getAddress() {
148            synchronized (mObject) {
149                return mAddress;
150            }
151        }
152
153        /**
154         * @return mRssi
155         */
156        short getRssi() {
157            synchronized (mObject) {
158                return mRssi;
159            }
160        }
161
162        /**
163         * @return mDeviceType
164         */
165        int getDeviceType() {
166            synchronized (mObject) {
167                return mDeviceType;
168            }
169        }
170
171        /**
172         * @return the mAlias
173         */
174        String getAlias() {
175            synchronized (mObject) {
176                return mAlias;
177            }
178        }
179
180        /**
181         * @param mAlias the mAlias to set
182         */
183        void setAlias(String mAlias) {
184            synchronized (mObject) {
185                mAdapterService.setDevicePropertyNative(mAddress,
186                    AbstractionLayer.BT_PROPERTY_REMOTE_FRIENDLY_NAME, mAlias.getBytes());
187            }
188        }
189
190        /**
191         * @param mBondState the mBondState to set
192         */
193        void setBondState(int mBondState) {
194            synchronized (mObject) {
195                this.mBondState = mBondState;
196                if (mBondState == BluetoothDevice.BOND_NONE)
197                {
198                    /* Clearing the Uuids local copy when the device is unpaired. If not cleared,
199                    cachedBluetoothDevice issued a connect using the local cached copy of uuids,
200                    without waiting for the ACTION_UUID intent.
201                    This was resulting in multiple calls to connect().*/
202                    mUuids = null;
203                }
204            }
205        }
206
207        /**
208         * @return the mBondState
209         */
210        int getBondState() {
211            synchronized (mObject) {
212                return mBondState;
213            }
214        }
215
216        int getOpenAclConnectionCount() {
217            return mOpenAclConnectionCount.get();
218        }
219    }
220
221    private void sendUuidIntent(BluetoothDevice device) {
222        DeviceProperties prop = getDeviceProperties(device);
223        Intent intent = new Intent(BluetoothDevice.ACTION_UUID);
224        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
225        intent.putExtra(BluetoothDevice.EXTRA_UUID, prop == null? null: prop.mUuids);
226        mAdapterService.initProfilePriorities(device, prop.mUuids);
227        mAdapterService.sendBroadcast(intent, AdapterService.BLUETOOTH_ADMIN_PERM);
228
229        //Remove the outstanding UUID request
230        mSdpTracker.remove(device);
231    }
232
233    private void sendDisplayPinIntent(byte[] address, int pin) {
234        Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);
235        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, getDevice(address));
236        intent.putExtra(BluetoothDevice.EXTRA_PAIRING_KEY, pin);
237        intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
238                    BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN);
239        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
240        mAdapterService.sendOrderedBroadcast(intent, mAdapterService.BLUETOOTH_ADMIN_PERM);
241    }
242
243    void devicePropertyChangedCallback(byte[] address, int[] types, byte[][] values) {
244        Intent intent;
245        byte[] val;
246        int type;
247        BluetoothDevice bdDevice = getDevice(address);
248        DeviceProperties device;
249        if (bdDevice == null) {
250            device = addDeviceProperties(address);
251            bdDevice = getDevice(address);
252        } else {
253            device = getDeviceProperties(bdDevice);
254        }
255
256        for (int j = 0; j < types.length; j++) {
257            type = types[j];
258            val = values[j];
259            if(val.length <= 0)
260                errorLog("devicePropertyChangedCallback: bdDevice: " + bdDevice + ", value is empty for type: " + type);
261            else {
262                synchronized(mObject) {
263                    switch (type) {
264                        case AbstractionLayer.BT_PROPERTY_BDNAME:
265                            device.mName = new String(val);
266                            intent = new Intent(BluetoothDevice.ACTION_NAME_CHANGED);
267                            intent.putExtra(BluetoothDevice.EXTRA_DEVICE, bdDevice);
268                            intent.putExtra(BluetoothDevice.EXTRA_NAME, device.mName);
269                            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
270                            mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);
271                            debugLog("Remote Device name is: " + device.mName);
272                            break;
273                        case AbstractionLayer.BT_PROPERTY_REMOTE_FRIENDLY_NAME:
274                            if (device.mAlias != null) {
275                                System.arraycopy(val, 0, device.mAlias, 0, val.length);
276                            }
277                            else {
278                                device.mAlias = new String(val);
279                            }
280                            break;
281                        case AbstractionLayer.BT_PROPERTY_BDADDR:
282                            device.mAddress = val;
283                            debugLog("Remote Address is:" + Utils.getAddressStringFromByte(val));
284                            break;
285                        case AbstractionLayer.BT_PROPERTY_CLASS_OF_DEVICE:
286                            device.mBluetoothClass =  Utils.byteArrayToInt(val);
287                            intent = new Intent(BluetoothDevice.ACTION_CLASS_CHANGED);
288                            intent.putExtra(BluetoothDevice.EXTRA_DEVICE, bdDevice);
289                            intent.putExtra(BluetoothDevice.EXTRA_CLASS,
290                                    new BluetoothClass(device.mBluetoothClass));
291                            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
292                            mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);
293                            debugLog("Remote class is:" + device.mBluetoothClass);
294                            break;
295                        case AbstractionLayer.BT_PROPERTY_UUIDS:
296                            int numUuids = val.length/AbstractionLayer.BT_UUID_SIZE;
297                            device.mUuids = Utils.byteArrayToUuid(val);
298                            sendUuidIntent(bdDevice);
299                            break;
300                        case AbstractionLayer.BT_PROPERTY_TYPE_OF_DEVICE:
301                            // The device type from hal layer, defined in bluetooth.h,
302                            // matches the type defined in BluetoothDevice.java
303                            device.mDeviceType = Utils.byteArrayToInt(val);
304                            break;
305                        case AbstractionLayer.BT_PROPERTY_REMOTE_RSSI:
306                            // RSSI from hal is in one byte
307                            device.mRssi = val[0];
308                            break;
309                    }
310                }
311            }
312        }
313    }
314
315    void deviceFoundCallback(byte[] address) {
316        // The device properties are already registered - we can send the intent
317        // now
318        BluetoothDevice device = getDevice(address);
319        debugLog("deviceFoundCallback: Remote Address is:" + device);
320        DeviceProperties deviceProp = getDeviceProperties(device);
321        if (deviceProp == null) {
322            errorLog("Device Properties is null for Device:" + device);
323            return;
324        }
325
326        Intent intent = new Intent(BluetoothDevice.ACTION_FOUND);
327        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
328        intent.putExtra(BluetoothDevice.EXTRA_CLASS,
329                new BluetoothClass(Integer.valueOf(deviceProp.mBluetoothClass)));
330        intent.putExtra(BluetoothDevice.EXTRA_RSSI, deviceProp.mRssi);
331        intent.putExtra(BluetoothDevice.EXTRA_NAME, deviceProp.mName);
332
333        mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);
334    }
335
336    void pinRequestCallback(byte[] address, byte[] name, int cod) {
337        //TODO(BT): Get wakelock and update name and cod
338        BluetoothDevice bdDevice = getDevice(address);
339        if (bdDevice == null) {
340            addDeviceProperties(address);
341        }
342        BluetoothClass btClass = bdDevice.getBluetoothClass();
343        int btDeviceClass = btClass.getDeviceClass();
344        if (btDeviceClass == BluetoothClass.Device.PERIPHERAL_KEYBOARD ||
345            btDeviceClass == BluetoothClass.Device.PERIPHERAL_KEYBOARD_POINTING) {
346            // Its a keyboard. Follow the HID spec recommendation of creating the
347            // passkey and displaying it to the user. If the keyboard doesn't follow
348            // the spec recommendation, check if the keyboard has a fixed PIN zero
349            // and pair.
350            //TODO: Add sFixedPinZerosAutoPairKeyboard() and maintain list of devices that have fixed pin
351            /*if (mAdapterService.isFixedPinZerosAutoPairKeyboard(address)) {
352                               mAdapterService.setPin(address, BluetoothDevice.convertPinToBytes("0000"));
353                               return;
354                     }*/
355            // Generate a variable PIN. This is not truly random but good enough.
356            int pin = (int) Math.floor(Math.random() * 1000000);
357            sendDisplayPinIntent(address, pin);
358            return;
359        }
360        infoLog("pinRequestCallback: " + address + " name:" + name + " cod:" +
361                cod);
362        Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);
363        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, getDevice(address));
364        intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
365                BluetoothDevice.PAIRING_VARIANT_PIN);
366        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
367        mAdapterService.sendOrderedBroadcast(intent, mAdapterService.BLUETOOTH_ADMIN_PERM);
368        return;
369    }
370
371    void sspRequestCallback(byte[] address, byte[] name, int cod, int pairingVariant,
372            int passkey) {
373        //TODO(BT): Get wakelock and update name and cod
374        BluetoothDevice bdDevice = getDevice(address);
375        if (bdDevice == null) {
376            addDeviceProperties(address);
377        }
378
379        infoLog("sspRequestCallback: " + address + " name: " + name + " cod: " +
380                cod + " pairingVariant " + pairingVariant + " passkey: " + passkey);
381        int variant;
382        boolean displayPasskey = false;
383        if (pairingVariant == AbstractionLayer.BT_SSP_VARIANT_PASSKEY_CONFIRMATION) {
384            variant = BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION;
385            displayPasskey = true;
386        } else if (pairingVariant == AbstractionLayer.BT_SSP_VARIANT_CONSENT) {
387            variant = BluetoothDevice.PAIRING_VARIANT_CONSENT;
388        } else if (pairingVariant == AbstractionLayer.BT_SSP_VARIANT_PASSKEY_ENTRY) {
389            variant = BluetoothDevice.PAIRING_VARIANT_PASSKEY;
390        } else if (pairingVariant == AbstractionLayer.BT_SSP_VARIANT_PASSKEY_NOTIFICATION) {
391            variant = BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY;
392            displayPasskey = true;
393        } else {
394            errorLog("SSP Pairing variant not present");
395            return;
396        }
397        BluetoothDevice device = getDevice(address);
398        if (device == null) {
399           warnLog("Device is not known for:" + Utils.getAddressStringFromByte(address));
400           addDeviceProperties(address);
401           device = getDevice(address);
402        }
403        Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);
404        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
405        if (displayPasskey) {
406            intent.putExtra(BluetoothDevice.EXTRA_PAIRING_KEY, passkey);
407        }
408        intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, variant);
409        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
410        mAdapterService.sendOrderedBroadcast(intent, mAdapterService.BLUETOOTH_ADMIN_PERM);
411    }
412
413    void aclStateChangeCallback(int status, byte[] address, int newState) {
414        BluetoothDevice device = getDevice(address);
415
416        if (device == null) {
417            errorLog("aclStateChangeCallback: Device is NULL");
418            return;
419        }
420
421        int openAclConnectionCount;
422        DeviceProperties prop = getDeviceProperties(device);
423        if (prop == null) {
424            errorLog("aclStateChangeCallback reported unknown device " + Arrays.toString(address));
425        }
426        Intent intent = null;
427        if (newState == AbstractionLayer.BT_ACL_STATE_CONNECTED) {
428            intent = new Intent(BluetoothDevice.ACTION_ACL_CONNECTED);
429            debugLog("aclStateChangeCallback: State:Connected to Device:" + device);
430            if (prop != null) {
431                prop.mOpenAclConnectionCount.incrementAndGet();
432            }
433        } else {
434            intent = new Intent(BluetoothDevice.ACTION_ACL_DISCONNECTED);
435            debugLog("aclStateChangeCallback: State:DisConnected to Device:" + device);
436            if (prop != null) {
437                prop.mOpenAclConnectionCount.decrementAndGet();
438            }
439        }
440        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
441        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
442        mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);
443    }
444
445    void fetchUuids(BluetoothDevice device) {
446        if (mSdpTracker.contains(device)) return;
447        mSdpTracker.add(device);
448
449        Message message = mHandler.obtainMessage(MESSAGE_UUID_INTENT);
450        message.obj = device;
451        mHandler.sendMessageDelayed(message, UUID_INTENT_DELAY);
452
453        //mAdapterService.getDevicePropertyNative(Utils.getBytesFromAddress(device.getAddress()), AbstractionLayer.BT_PROPERTY_UUIDS);
454        mAdapterService.getRemoteServicesNative(Utils.getBytesFromAddress(device.getAddress()));
455    }
456
457    private final Handler mHandler = new Handler() {
458        @Override
459        public void handleMessage(Message msg) {
460            switch (msg.what) {
461            case MESSAGE_UUID_INTENT:
462                BluetoothDevice device = (BluetoothDevice)msg.obj;
463                if (device != null) {
464                    sendUuidIntent(device);
465                }
466                break;
467            }
468        }
469    };
470
471    private void errorLog(String msg) {
472        Log.e(TAG, msg);
473    }
474
475    private void debugLog(String msg) {
476        if (DBG) Log.d(TAG, msg);
477    }
478
479    private void infoLog(String msg) {
480        if (DBG) Log.i(TAG, msg);
481    }
482
483    private void warnLog(String msg) {
484        Log.w(TAG, msg);
485    }
486
487}
488