summaryrefslogtreecommitdiffstats
path: root/core/java/android/bluetooth/BluetoothDevice.java
blob: 56b231fa6b13f53b2a8603213307061740833890 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.bluetooth;

import android.os.RemoteException;
import android.util.Log;

import java.io.UnsupportedEncodingException;

/**
 * The Android Bluetooth API is not finalized, and *will* change. Use at your
 * own risk.
 *
 * Manages the local Bluetooth device. Scan for devices, create bondings,
 * power up and down the adapter.
 *
 * @hide
 */
public class BluetoothDevice {
    /** Inquiry scan and page scan are both off.
     *  Device is neither discoverable nor connectable */
    public static final int SCAN_MODE_NONE = 0;
    /** Page scan is on, inquiry scan is off.
     *  Device is connectable, but not discoverable */
    public static final int SCAN_MODE_CONNECTABLE = 1;
    /** Page scan and inquiry scan are on.
     *  Device is connectable and discoverable */
    public static final int SCAN_MODE_CONNECTABLE_DISCOVERABLE = 3;

    public static final int RESULT_FAILURE = -1;
    public static final int RESULT_SUCCESS = 0;

    /** We do not have a link key for the remote device, and are therefore not
     * bonded */
    public static final int BOND_NOT_BONDED = 0;
    /** We have a link key for the remote device, and are probably bonded. */
    public static final int BOND_BONDED = 1;
    /** We are currently attempting bonding */
    public static final int BOND_BONDING = 2;

    //TODO: Unify these result codes in BluetoothResult or BluetoothError
    /** A bond attempt failed because pins did not match, or remote device did
     * not respond to pin request in time */
    public static final int UNBOND_REASON_AUTH_FAILED = 1;
    /** A bond attempt failed because the other side explicilty rejected
     * bonding */
    public static final int UNBOND_REASON_AUTH_REJECTED = 2;
    /** A bond attempt failed because we canceled the bonding process */
    public static final int UNBOND_REASON_AUTH_CANCELED = 3;
    /** A bond attempt failed because we could not contact the remote device */
    public static final int UNBOND_REASON_REMOTE_DEVICE_DOWN = 4;
    /** An existing bond was explicitly revoked */
    public static final int UNBOND_REASON_REMOVED = 5;

    private static final String TAG = "BluetoothDevice";
    
    private final IBluetoothDevice mService;
    /**
     * @hide - hide this because it takes a parameter of type
     * IBluetoothDevice, which is a System private class.
     * Also note that Context.getSystemService is a factory that
     * returns a BlueToothDevice. That is the right way to get
     * a BluetoothDevice.
     */
    public BluetoothDevice(IBluetoothDevice service) {
        mService = service;
    }

    /**
     * Get the current status of Bluetooth hardware.
     *
     * @return true if Bluetooth enabled, false otherwise.
     */
    public boolean isEnabled() {
        try {
            return mService.isEnabled();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }

    /**
     * Enable the Bluetooth device.
     * Turn on the underlying hardware.
     * This is an asynchronous call, BluetoothIntent.ENABLED_ACTION will be
     * sent if and when the device is successfully enabled.
     * @return false if we cannot enable the Bluetooth device. True does not
     * imply the device was enabled, it only implies that so far there were no
     * problems.
     */
    public boolean enable() {
        return enable(null);
    }

    /**
     * Enable the Bluetooth device.
     * Turns on the underlying hardware.
     * This is an asynchronous call. onEnableResult() of your callback will be
     * called when the call is complete, with either RESULT_SUCCESS or
     * RESULT_FAILURE.
     *
     * Your callback will be called from a binder thread, not the main thread.
     *
     * In addition to the callback, BluetoothIntent.ENABLED_ACTION will be
     * broadcast if the device is successfully enabled.
     *
     * @param callback Your callback, null is ok.
     * @return true if your callback was successfully registered, or false if
     * there was an error, implying your callback will never be called.
     */
    public boolean enable(IBluetoothDeviceCallback callback) {
        try {
            return mService.enable(callback);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }

    /**
     * Disable the Bluetooth device.
     * This turns off the underlying hardware.
     *
     * @return true if successful, false otherwise.
     */
    public boolean disable() {
        try {
            return mService.disable();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }

    public String getAddress() {
        try {
            return mService.getAddress();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }

    /**
     * Get the friendly Bluetooth name of this device.
     *
     * This name is visible to remote Bluetooth devices. Currently it is only
     * possible to retrieve the Bluetooth name when Bluetooth is enabled.
     *
     * @return the Bluetooth name, or null if there was a problem.
     */
    public String getName() {
        try {
            return mService.getName();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }

    /**
     * Set the friendly Bluetooth name of this device.
     *
     * This name is visible to remote Bluetooth devices. The Bluetooth Service
     * is responsible for persisting this name.
     *
     * @param name the name to set
     * @return     true, if the name was successfully set. False otherwise.
     */
    public boolean setName(String name) {
        try {
            return mService.setName(name);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }

    public String getVersion() {
        try {
            return mService.getVersion();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }
    public String getRevision() {
        try {
            return mService.getRevision();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }
    public String getManufacturer() {
        try {
            return mService.getManufacturer();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }
    public String getCompany() {
        try {
            return mService.getCompany();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }

    /**
     * Get the current scan mode.
     * Used to determine if the local device is connectable and/or discoverable
     * @return Scan mode, one of SCAN_MODE_* or an error code
     */
    public int getScanMode() {
        try {
            return mService.getScanMode();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return BluetoothError.ERROR_IPC;
    }

    /**
     * Set the current scan mode.
     * Used to make the local device connectable and/or discoverable
     * @param scanMode One of SCAN_MODE_*
     */
    public void setScanMode(int scanMode) {
        try {
            mService.setScanMode(scanMode);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
    }

    public int getDiscoverableTimeout() {
        try {
            return mService.getDiscoverableTimeout();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return -1;
    }
    public void setDiscoverableTimeout(int timeout) {
        try {
            mService.setDiscoverableTimeout(timeout);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
    }

    public boolean startDiscovery() {
        return startDiscovery(true);
    }
    public boolean startDiscovery(boolean resolveNames) {
        try {
            return mService.startDiscovery(resolveNames);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }

    public void cancelDiscovery() {
        try {
            mService.cancelDiscovery();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
    }

    public boolean isDiscovering() {
        try {
            return mService.isDiscovering();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }

    public boolean startPeriodicDiscovery() {
        try {
            return mService.startPeriodicDiscovery();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }
    public boolean stopPeriodicDiscovery() {
        try {
            return mService.stopPeriodicDiscovery();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }
    public boolean isPeriodicDiscovery() {
        try {
            return mService.isPeriodicDiscovery();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }

    public String[] listRemoteDevices() {
        try {
            return mService.listRemoteDevices();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }

    /**
     * List remote devices that have a low level (ACL) connection.
     *
     * RFCOMM, SDP and L2CAP are all built on ACL connections. Devices can have
     * an ACL connection even when not paired - this is common for SDP queries
     * or for in-progress pairing requests.
     *
     * In most cases you probably want to test if a higher level protocol is
     * connected, rather than testing ACL connections.
     *
     * @return bluetooth hardware addresses of remote devices with a current
     *         ACL connection. Array size is 0 if no devices have a
     *         connection. Null on error.
     */
    public String[] listAclConnections() {
        try {
            return mService.listAclConnections();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }

    /**
     * Check if a specified remote device has a low level (ACL) connection.
     *
     * RFCOMM, SDP and L2CAP are all built on ACL connections. Devices can have
     * an ACL connection even when not paired - this is common for SDP queries
     * or for in-progress pairing requests.
     *
     * In most cases you probably want to test if a higher level protocol is
     * connected, rather than testing ACL connections.
     *
     * @param address the Bluetooth hardware address you want to check.
     * @return true if there is an ACL connection, false otherwise and on
     *         error.
     */
    public boolean isAclConnected(String address) {
        try {
            return mService.isAclConnected(address);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }

    /**
     * Perform a low level (ACL) disconnection of a remote device.
     *
     * This forcably disconnects the ACL layer connection to a remote device,
     * which will cause all RFCOMM, SDP and L2CAP connections to this remote
     * device to close.
     *
     * @param address the Bluetooth hardware address you want to disconnect.
     * @return true if the device was disconnected, false otherwise and on
     *         error.
     */
    public boolean disconnectRemoteDeviceAcl(String address) {
        try {
            return mService.disconnectRemoteDeviceAcl(address);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }

    /**
     * Create a bonding with a remote bluetooth device.
     *
     * This is an asynchronous call. The result of this bonding attempt can be
     * observed through BluetoothIntent.BOND_STATE_CHANGED_ACTION intents.
     *
     * @param address the remote device Bluetooth address.
     * @return false If there was an immediate problem creating the bonding,
     *         true otherwise.
     */
    public boolean createBond(String address) {
        try {
            return mService.createBond(address);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }

    /**
     * Cancel an in-progress bonding request started with createBond.
     */
    public boolean cancelBondProcess(String address) {
        try {
            return mService.cancelBondProcess(address);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }

    /**
     * Remove an already exisiting bonding (delete the link key).
     */
    public boolean removeBond(String address) {
        try {
            return mService.removeBond(address);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }

    /**
     * List remote devices that are bonded (paired) to the local device.
     *
     * Bonding (pairing) is the process by which the user enters a pin code for
     * the device, which generates a shared link key, allowing for
     * authentication and encryption of future connections. In Android we
     * require bonding before RFCOMM or SCO connections can be made to a remote
     * device.
     *
     * This function lists which remote devices we have a link key for. It does
     * not cause any RF transmission, and does not check if the remote device
     * still has it's link key with us. If the other side no longer has its
     * link key then the RFCOMM or SCO connection attempt will result in an
     * error.
     *
     * This function does not check if the remote device is in range.
     *
     * Remote devices that have an in-progress bonding attempt are not
     * returned.
     *
     * @return bluetooth hardware addresses of remote devices that are
     *         bonded. Array size is 0 if no devices are bonded. Null on error.
     */
    public String[] listBonds() {
        try {
            return mService.listBonds();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }

    /**
     * Get the bonding state of a remote device.
     *
     * Result is one of:
     * BluetoothError.*
     * BOND_*
     *
     * @param address Bluetooth hardware address of the remote device to check.
     * @return Result code
     */
    public int getBondState(String address) {
        try {
            return mService.getBondState(address);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return BluetoothError.ERROR_IPC;
    }

    public String getRemoteName(String address) {
        try {
            return mService.getRemoteName(address);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }

    public String getRemoteVersion(String address) {
        try {
            return mService.getRemoteVersion(address);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }
    public String getRemoteRevision(String address) {
        try {
            return mService.getRemoteRevision(address);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }
    public String getRemoteManufacturer(String address) {
        try {
            return mService.getRemoteManufacturer(address);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }
    public String getRemoteCompany(String address) {
        try {
            return mService.getRemoteCompany(address);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }

    /**
     * Returns the RFCOMM channel associated with the 16-byte UUID on
     * the remote Bluetooth address.
     *
     * Performs a SDP ServiceSearchAttributeRequest transaction. The provided
     * uuid is verified in the returned record. If there was a problem, or the
     * specified uuid does not exist, -1 is returned.
     */
    public boolean getRemoteServiceChannel(String address, short uuid16,
            IBluetoothDeviceCallback callback) {
        try {
            return mService.getRemoteServiceChannel(address, uuid16, callback);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }

    /**
     * Get the major, minor and servics classes of a remote device.
     * These classes are encoded as a 32-bit integer. See BluetoothClass.
     * @param address remote device
     * @return 32-bit class suitable for use with BluetoothClass.
     */
    public int getRemoteClass(String address) {
        try {
            return mService.getRemoteClass(address);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return BluetoothClass.ERROR;
    }

    public byte[] getRemoteFeatures(String address) {
        try {
            return mService.getRemoteFeatures(address);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }
    public String lastSeen(String address) {
        try {
            return mService.lastSeen(address);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }
    public String lastUsed(String address) {
        try {
            return mService.lastUsed(address);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }

    public boolean setPin(String address, byte[] pin) {
        try {
            return mService.setPin(address, pin);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }
    public boolean cancelPin(String address) {
        try {
            return mService.cancelPin(address);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }

    /**
     * Check that a pin is valid and convert to byte array.
     *
     * Bluetooth pin's are 1 to 16 bytes of UTF8 characters.
     * @param pin pin as java String
     * @return the pin code as a UTF8 byte array, or null if it is an invalid
     *         Bluetooth pin.
     */
    public static byte[] convertPinToBytes(String pin) {
        if (pin == null) {
            return null;
        }
        byte[] pinBytes;
        try {
            pinBytes = pin.getBytes("UTF8");
        } catch (UnsupportedEncodingException uee) {
            Log.e(TAG, "UTF8 not supported?!?");  // this should not happen
            return null;
        }
        if (pinBytes.length <= 0 || pinBytes.length > 16) {
            return null;
        }
        return pinBytes;
    }
    

    private static final int ADDRESS_LENGTH = 17;
    /** Sanity check a bluetooth address, such as "00:43:A8:23:10:F0" */
    public static boolean checkBluetoothAddress(String address) {
        if (address == null || address.length() != ADDRESS_LENGTH) {
            return false;
        }
        for (int i = 0; i < ADDRESS_LENGTH; i++) {
            char c = address.charAt(i);
            switch (i % 3) {
            case 0:
            case 1:
                if (Character.digit(c, 16) != -1) {
                    break;  // hex character, OK
                }
                return false;
            case 2:
                if (c == ':') {
                    break;  // OK
                }
                return false;
            }
        }
        return true;
    }
}