BluetoothDevice.java revision f242b7b931898856bcbcb7ec36cacf43098ba544
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* 2bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * Copyright (C) 2009 The Android Open Source Project 39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License. 69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at 79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and 149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License. 159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.bluetooth; 189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 19005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pellyimport android.annotation.SdkConstant; 20005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pellyimport android.annotation.SdkConstant.SdkConstantType; 21bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pellyimport android.os.IBinder; 22bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pellyimport android.os.Parcel; 23bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pellyimport android.os.Parcelable; 24aef439e6f825c0cb99a2ac08c8207f48b7a9fe10Nick Pellyimport android.os.ParcelUuid; 259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.RemoteException; 26bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pellyimport android.os.ServiceManager; 279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.Log; 289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 29bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pellyimport java.io.IOException; 309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.UnsupportedEncodingException; 311caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganeshimport java.util.UUID; 329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/** 34bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * Represents a remote Bluetooth device. 359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 3645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * <p>Use {@link BluetoothAdapter#getRemoteDevice} to create a {@link 3745e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * BluetoothDevice}. 3845e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * 3945e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * <p>This class is really just a thin wrapper for a Bluetooth hardware 4045e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * address. Objects of this class are immutable. Operations on this class 4145e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * are performed on the remote Bluetooth hardware address, using the 4245e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * {@link BluetoothAdapter} that was used to create this {@link 4345e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * BluetoothDevice}. 449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 45bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pellypublic final class BluetoothDevice implements Parcelable { 46bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly private static final String TAG = "BluetoothDevice"; 479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 48b24e11baac589fe16426f2d243b460ab84991c7bNick Pelly /** 49b24e11baac589fe16426f2d243b460ab84991c7bNick Pelly * Sentinel error value for this class. Guaranteed to not equal any other 50b24e11baac589fe16426f2d243b460ab84991c7bNick Pelly * integer constant in this class. Provided as a convenience for functions 51b24e11baac589fe16426f2d243b460ab84991c7bNick Pelly * that require a sentinel error value, for example: 52005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p><code>Intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, 53005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * BluetoothDevice.ERROR)</code> 54005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 55005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final int ERROR = Integer.MIN_VALUE; 56005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly 57005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 58005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Broadcast Action: Remote device discovered. 59005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Sent when a remote device is found during discovery. 60005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link 61005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * #EXTRA_CLASS}. Can contain the extra fields {@link #EXTRA_NAME} and/or 62005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * {@link #EXTRA_RSSI} if they are available. 63005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 64005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 65005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly // TODO: Change API to not broadcast RSSI if not available (incoming connection) 66005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 67005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String ACTION_FOUND = 68005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly "android.bluetooth.device.action.FOUND"; 69005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly 70005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 71005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Broadcast Action: Remote device disappeared. 72005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Sent when a remote device that was found in the last discovery is not 73005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * found in the current discovery. 74005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Always contains the extra field {@link #EXTRA_DEVICE}. 75005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 76005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * @hide 77005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 78005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 79005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String ACTION_DISAPPEARED = 80005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly "android.bluetooth.device.action.DISAPPEARED"; 81005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly 82005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 83005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Broadcast Action: Bluetooth class of a remote device has changed. 84005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link 85005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * #EXTRA_CLASS}. 86005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 87005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * @see {@link BluetoothClass} 88005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 89005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 90005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String ACTION_CLASS_CHANGED = 91005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly "android.bluetooth.device.action.CLASS_CHANGED"; 92005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly 93005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 94005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Broadcast Action: Indicates a low level (ACL) connection has been 95005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * established with a remote device. 96005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Always contains the extra field {@link #EXTRA_DEVICE}. 97005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>ACL connections are managed automatically by the Android Bluetooth 98005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * stack. 99005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 100005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 101005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 102005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String ACTION_ACL_CONNECTED = 103005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly "android.bluetooth.device.action.ACL_CONNECTED"; 104005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly 105005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 106005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Broadcast Action: Indicates that a low level (ACL) disconnection has 107005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * been requested for a remote device, and it will soon be disconnected. 108005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>This is useful for graceful disconnection. Applications should use 109005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * this intent as a hint to immediately terminate higher level connections 110005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * (RFCOMM, L2CAP, or profile connections) to the remote device. 111005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Always contains the extra field {@link #EXTRA_DEVICE}. 112005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 113005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 114005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 115005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String ACTION_ACL_DISCONNECT_REQUESTED = 116005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly "android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED"; 117005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly 118005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 119005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Broadcast Action: Indicates a low level (ACL) disconnection from a 120005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * remote device. 121005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Always contains the extra field {@link #EXTRA_DEVICE}. 122005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>ACL connections are managed automatically by the Android Bluetooth 123005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * stack. 124005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 125005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 126005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 127005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String ACTION_ACL_DISCONNECTED = 128005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly "android.bluetooth.device.action.ACL_DISCONNECTED"; 129005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly 130005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 131005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Broadcast Action: Indicates the friendly name of a remote device has 132005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * been retrieved for the first time, or changed since the last retrieval. 133005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link 134005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * #EXTRA_NAME}. 135005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 136005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 137005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 138005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String ACTION_NAME_CHANGED = 139005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly "android.bluetooth.device.action.NAME_CHANGED"; 140005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly 141005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 142005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Broadcast Action: Indicates a change in the bond state of a remote 143005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * device. For example, if a device is bonded (paired). 144005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Always contains the extra fields {@link #EXTRA_DEVICE}, {@link 145005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * #EXTRA_BOND_STATE} and {@link #EXTRA_PREVIOUS_BOND_STATE}. 146005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 147005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 148005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly // Note: When EXTRA_BOND_STATE is BOND_NONE then this will also 149005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly // contain a hidden extra field EXTRA_REASON with the result code. 150005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 151005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String ACTION_BOND_STATE_CHANGED = 152005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly "android.bluetooth.device.action.BOND_STATE_CHANGED"; 153005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly 154005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 155005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Used as a Parcelable {@link BluetoothDevice} extra field in every intent 156005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * broadcast by this class. It contains the {@link BluetoothDevice} that 157005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * the intent applies to. 158005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 159005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE"; 160005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly 161005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 162005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Used as a String extra field in {@link #ACTION_NAME_CHANGED} and {@link 163005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * #ACTION_FOUND} intents. It contains the friendly Bluetooth name. 164005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 165005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String EXTRA_NAME = "android.bluetooth.device.extra.NAME"; 166005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly 167005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 168005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Used as an optional short extra field in {@link #ACTION_FOUND} intents. 169005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Contains the RSSI value of the remote device as reported by the 170005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Bluetooth hardware. 171005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 172005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String EXTRA_RSSI = "android.bluetooth.device.extra.RSSI"; 173005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly 174005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 175005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Used as an Parcelable {@link BluetoothClass} extra field in {@link 176005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * #ACTION_FOUND} and {@link #ACTION_CLASS_CHANGED} intents. 177005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 178005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String EXTRA_CLASS = "android.bluetooth.device.extra.CLASS"; 179005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly 180005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 181005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Used as an int extra field in {@link #ACTION_BOND_STATE_CHANGED} intents. 182005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Contains the bond state of the remote device. 183005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Possible values are: 184005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * {@link #BOND_NONE}, 185005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * {@link #BOND_BONDING}, 186005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * {@link #BOND_BONDED}. 187005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 188005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String EXTRA_BOND_STATE = "android.bluetooth.device.extra.BOND_STATE"; 189005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 190005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Used as an int extra field in {@link #ACTION_BOND_STATE_CHANGED} intents. 191005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Contains the previous bond state of the remote device. 192005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Possible values are: 193005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * {@link #BOND_NONE}, 194005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * {@link #BOND_BONDING}, 195005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * {@link #BOND_BONDED}. 196005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 197005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String EXTRA_PREVIOUS_BOND_STATE = 198005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly "android.bluetooth.device.extra.PREVIOUS_BOND_STATE"; 199005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 200005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Indicates the remote device is not bonded (paired). 201005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>There is no shared link key with the remote device, so communication 202005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * (if it is allowed at all) will be unauthenticated and unencrypted. 203005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 204005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final int BOND_NONE = 10; 205005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 206005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Indicates bonding (pairing) is in progress with the remote device. 207005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 208005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final int BOND_BONDING = 11; 209005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 210005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Indicates the remote device is bonded (paired). 211005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>A shared link keys exists locally for the remote device, so 212005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * communication can be authenticated and encrypted. 213005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p><i>Being bonded (paired) with a remote device does not necessarily 214005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * mean the device is currently connected. It just means that the ponding 215005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * procedure was compeleted at some earlier time, and the link key is still 216005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * stored locally, ready to use on the next connection. 217005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * </i> 218005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 219005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final int BOND_BONDED = 12; 220005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly 221005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** @hide */ 222005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String EXTRA_REASON = "android.bluetooth.device.extra.REASON"; 223005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** @hide */ 224005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String EXTRA_PAIRING_VARIANT = 225005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly "android.bluetooth.device.extra.PAIRING_VARIANT"; 226005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** @hide */ 227005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String EXTRA_PASSKEY = "android.bluetooth.device.extra.PASSKEY"; 228005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly 229005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 2301caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * Broadcast Action: This intent is used to broadcast the {@link UUID} 231aef439e6f825c0cb99a2ac08c8207f48b7a9fe10Nick Pelly * wrapped as a {@link android.os.ParcelUuid} of the remote device after it 232aef439e6f825c0cb99a2ac08c8207f48b7a9fe10Nick Pelly * has been fetched. This intent is sent only when the UUIDs of the remote 233aef439e6f825c0cb99a2ac08c8207f48b7a9fe10Nick Pelly * device are requested to be fetched using Service Discovery Protocol 2341caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * <p> Always contains the extra field {@link #EXTRA_DEVICE} 2351caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * <p> Always contains the extra filed {@link #EXTRA_UUID} 2361caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 2371caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * @hide 2381caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh */ 2391caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 2401caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh public static final String ACTION_UUID = 2411caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh "android.bleutooth.device.action.UUID"; 2421caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh 2431caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh /** 244005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Broadcast Action: Indicates a failure to retrieve the name of a remote 245005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * device. 246005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Always contains the extra field {@link #EXTRA_DEVICE}. 247005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 248005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * @hide 249005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 250005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly //TODO: is this actually useful? 251005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 252005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String ACTION_NAME_FAILED = 253005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly "android.bluetooth.device.action.NAME_FAILED"; 254005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly 255005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** @hide */ 256005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 257005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String ACTION_PAIRING_REQUEST = 258005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly "android.bluetooth.device.action.PAIRING_REQUEST"; 259005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** @hide */ 260005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 261005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public static final String ACTION_PAIRING_CANCEL = 262005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly "android.bluetooth.device.action.PAIRING_CANCEL"; 263a4433af5ac677be7c1f63447c0cd78829bdee159Yue Lixin 264b24e11baac589fe16426f2d243b460ab84991c7bNick Pelly /** A bond attempt succeeded 265b24e11baac589fe16426f2d243b460ab84991c7bNick Pelly * @hide */ 266b24e11baac589fe16426f2d243b460ab84991c7bNick Pelly public static final int BOND_SUCCESS = 0; 2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** A bond attempt failed because pins did not match, or remote device did 26832d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh * not respond to pin request in time 26945e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @hide */ 2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static final int UNBOND_REASON_AUTH_FAILED = 1; 2719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** A bond attempt failed because the other side explicilty rejected 27245e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * bonding 27345e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @hide */ 2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static final int UNBOND_REASON_AUTH_REJECTED = 2; 27545e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** A bond attempt failed because we canceled the bonding process 27645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @hide */ 2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static final int UNBOND_REASON_AUTH_CANCELED = 3; 27845e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** A bond attempt failed because we could not contact the remote device 27945e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @hide */ 2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static final int UNBOND_REASON_REMOTE_DEVICE_DOWN = 4; 28145e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** A bond attempt failed because a discovery is in progress 28245e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @hide */ 2839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static final int UNBOND_REASON_DISCOVERY_IN_PROGRESS = 5; 28432d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh /** A bond attempt failed because of authentication timeout 28532d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh * @hide */ 28632d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh public static final int UNBOND_REASON_AUTH_TIMEOUT = 6; 28732d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh /** A bond attempt failed because of repeated attempts 28832d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh * @hide */ 28932d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh public static final int UNBOND_REASON_REPEATED_ATTEMPTS = 7; 29045e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** An existing bond was explicitly revoked 29145e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @hide */ 29232d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh public static final int UNBOND_REASON_REMOVED = 8; 2939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 29445e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** The user will be prompted to enter a pin 29545e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @hide */ 296b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh public static final int PAIRING_VARIANT_PIN = 0; 29745e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** The user will be prompted to enter a passkey 29845e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @hide */ 299b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh public static final int PAIRING_VARIANT_PASSKEY = 1; 30045e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** The user will be prompted to confirm the passkey displayed on the screen 30145e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @hide */ 30232d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh public static final int PAIRING_VARIANT_PASSKEY_CONFIRMATION = 2; 30332d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh /** The user will be prompted to accept or deny the incoming pairing request 30432d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh * @hide */ 30532d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh public static final int PAIRING_VARIANT_CONSENT = 3; 30632d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh /** The user will be prompted to enter the passkey displayed on remote device 30732d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh * @hide */ 30832d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh public static final int PAIRING_VARIANT_DISPLAY_PASSKEY = 4; 309b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh 3101caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh /** 3111caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * Used as an extra field in {@link #ACTION_UUID} intents, 312aef439e6f825c0cb99a2ac08c8207f48b7a9fe10Nick Pelly * Contains the {@link android.os.ParcelUuid}s of the remote device which 313aef439e6f825c0cb99a2ac08c8207f48b7a9fe10Nick Pelly * is a parcelable version of {@link UUID}. 3141caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * @hide 3151caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh */ 3161caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh public static final String EXTRA_UUID = "android.bluetooth.device.extra.UUID"; 3171caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh 31816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly /** 31916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * Lazy initialization. Guaranteed final after first object constructed, or 32016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * getService() called. 32116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * TODO: Unify implementation of sService amongst BluetoothFoo API's 32216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly */ 32316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly private static IBluetooth sService; 324bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly 325bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly private final String mAddress; 326d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh 32716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly /*package*/ static IBluetooth getService() { 328bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly synchronized (BluetoothDevice.class) { 329bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly if (sService == null) { 330f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly IBinder b = ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE); 331bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly if (b == null) { 332bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly throw new RuntimeException("Bluetooth service not available"); 333bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 334bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly sService = IBluetooth.Stub.asInterface(b); 335bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 336bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 33716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly return sService; 33816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly } 339bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly 34016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly /** 34116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * Create a new BluetoothDevice 34216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * Bluetooth MAC address must be upper case, such as "00:11:22:33:AA:BB", 34316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * and is validated in this constructor. 34416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * @param address valid Bluetooth MAC address 34516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * @throws RuntimeException Bluetooth is not available on this platform 34616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * @throws IllegalArgumentException address is invalid 34716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * @hide 34816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly */ 34916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly /*package*/ BluetoothDevice(String address) { 35016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly getService(); // ensures sService is initialized 351005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly if (!BluetoothAdapter.checkBluetoothAddress(address)) { 352bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly throw new IllegalArgumentException(address + " is not a valid Bluetooth address"); 353bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 354bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly 355bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly mAddress = address; 3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 358bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly @Override 359bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public boolean equals(Object o) { 360bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly if (o instanceof BluetoothDevice) { 361bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return mAddress.equals(((BluetoothDevice)o).getAddress()); 362bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return false; 3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 366bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly @Override 367bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public int hashCode() { 368bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return mAddress.hashCode(); 369105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project } 370105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project 37145e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** 37245e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * Returns a string representation of this BluetoothDevice. 37345e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * <p>Currently this is the Bluetooth hardware address, for example 37445e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * "00:11:22:AA:BB:CC". However, you should always use {@link #getAddress} 37545e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * if you explicitly require the Bluetooth hardware address in case the 37645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * {@link #toString} representation changes in the future. 37745e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @return string representation of this BluetoothDevice 37845e2704ff512d41e22af2801d76e96955469ce8dNick Pelly */ 379bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly @Override 380bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public String toString() { 381bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return mAddress; 3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 384bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public int describeContents() { 385bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return 0; 386bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 387bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly 388bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public static final Parcelable.Creator<BluetoothDevice> CREATOR = 389bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly new Parcelable.Creator<BluetoothDevice>() { 390bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public BluetoothDevice createFromParcel(Parcel in) { 391bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return new BluetoothDevice(in.readString()); 392bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 393bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public BluetoothDevice[] newArray(int size) { 394bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return new BluetoothDevice[size]; 395bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 396bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly }; 397bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly 398bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public void writeToParcel(Parcel out, int flags) { 399bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly out.writeString(mAddress); 4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 40245e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** 40345e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * Returns the hardware address of this BluetoothDevice. 40445e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * <p> For example, "00:11:22:AA:BB:CC". 40545e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @return Bluetooth hardware address as string 40645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly */ 4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public String getAddress() { 408bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return mAddress; 4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 41245e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * Get the friendly Bluetooth name of the remote device. 4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 41445e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * <p>The local adapter will automatically retrieve remote names when 41545e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * performing a device scan, and will cache them. This method just returns 41645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * the name for this device from the cache. 417de893f550301a60274e87aa8168225e7a7a42184Nick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 4189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return the Bluetooth name, or null if there was a problem. 4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 4219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public String getName() { 4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 423bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.getRemoteName(mAddress); 4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (RemoteException e) {Log.e(TAG, "", e);} 4259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return null; 4269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 429005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Start the bonding (pairing) process with the remote device. 430005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>This is an asynchronous call, it will return immediately. Register 431005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when 432005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * the bonding process completes, and its result. 433005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Android system services will handle the necessary user interactions 434005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * to confirm and complete the bonding process. 435005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 437005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * @return false on immediate error, true if bonding will begin 43818b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly * @hide 4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 440bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public boolean createBond() { 4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 442bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.createBond(mAddress); 4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (RemoteException e) {Log.e(TAG, "", e);} 4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return false; 4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 448005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Cancel an in-progress bonding request started with {@link #createBond}. 449005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 450005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * 451005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * @return true on sucess, false on error 45218b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly * @hide 4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 454bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public boolean cancelBondProcess() { 4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 456bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.cancelBondProcess(mAddress); 4579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (RemoteException e) {Log.e(TAG, "", e);} 4589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return false; 4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 462005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Remove bond (pairing) with the remote device. 463005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Delete the link key associated with the remote device, and 464005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * immediately terminate connections to that device that require 465005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * authentication and encryption. 466005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 468005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * @return true on sucess, false on error 46918b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly * @hide 4709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 471bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public boolean removeBond() { 4729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 473bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.removeBond(mAddress); 4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (RemoteException e) {Log.e(TAG, "", e);} 475bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return false; 4769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 479005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Get the bond state of the remote device. 480005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Possible values for the bond state are: 481005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * {@link #BOND_NONE}, 482005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * {@link #BOND_BONDING}, 483005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * {@link #BOND_BONDED}. 484005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. 4859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 486005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * @return the bond state 4879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 488bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public int getBondState() { 4899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 490bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.getBondState(mAddress); 4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (RemoteException e) {Log.e(TAG, "", e);} 492005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly return BOND_NONE; 493005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly } 494005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly 495005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 496005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Get the Bluetooth class of the remote device. 497005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. 498005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * 499005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * @return Bluetooth class object, or null on error 500005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 501005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public BluetoothClass getBluetoothClass() { 502005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly try { 503005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly int classInt = sService.getRemoteClass(mAddress); 504005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly if (classInt == BluetoothClass.ERROR) return null; 505005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly return new BluetoothClass(classInt); 506005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly } catch (RemoteException e) {Log.e(TAG, "", e);} 507005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly return null; 5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 510efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue /** 511efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue * Get trust state of a remote device. 512efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue * @hide 513efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue */ 514efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue public boolean getTrustState() { 515efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue try { 516efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue return sService.getTrustState(mAddress); 517efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue } catch (RemoteException e) { 518efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue Log.e(TAG, "", e); 519efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue } 520efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue return false; 521efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue } 522efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue 523efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue /** 524efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue * Set trust state for a remote device. 525efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue * @param value the trust state value (true or false) 526efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue * @hide 527efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue */ 528efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue public boolean setTrust(boolean value) { 529efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue try { 530efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue return sService.setTrust(mAddress, value); 531efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue } catch (RemoteException e) { 532efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue Log.e(TAG, "", e); 533efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue } 534efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue return false; 535efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue } 536efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue 53745e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** @hide */ 538dd0463aef18d251c741bfc9dc7a2787443ef36f1Jaikumar Ganesh public ParcelUuid[] getUuids() { 5399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 540bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.getRemoteUuids(mAddress); 5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (RemoteException e) {Log.e(TAG, "", e);} 5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return null; 5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 544d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh 5451caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh /** 5461caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * Perform a SDP query on the remote device to get the UUIDs 5471caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * supported. This API is asynchronous and an Intent is sent, 5481caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * with the UUIDs supported by the remote end. If there is an error 5491caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * in getting the SDP records or if the process takes a long time, 5501caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * an Intent is sent with the UUIDs that is currently present in the 5511caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * cache. Clients should use the {@link getUuids} to get UUIDs 5521caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * is SDP is not to be performed. 5531caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * 5541caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * @return False if the sanity check fails, True if the process 5551caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * of initiating an ACL connection to the remote device 5561caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * was started. 5571caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * @hide 5581caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh */ 5591caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh public boolean fetchUuidsWithSdp() { 5601caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh try { 56116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly return sService.fetchRemoteUuids(mAddress, null, null); 5621caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh } catch (RemoteException e) {Log.e(TAG, "", e);} 5631caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh return false; 5641caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh } 5651caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh 56645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** @hide */ 567dd0463aef18d251c741bfc9dc7a2787443ef36f1Jaikumar Ganesh public int getServiceChannel(ParcelUuid uuid) { 568d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh try { 569bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.getRemoteServiceChannel(mAddress, uuid); 570d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh } catch (RemoteException e) {Log.e(TAG, "", e);} 571b24e11baac589fe16426f2d243b460ab84991c7bNick Pelly return BluetoothDevice.ERROR; 5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 57445e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** @hide */ 575bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public boolean setPin(byte[] pin) { 5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 577bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.setPin(mAddress, pin); 5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (RemoteException e) {Log.e(TAG, "", e);} 5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return false; 5809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 581b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh 58245e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** @hide */ 583bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public boolean setPasskey(int passkey) { 584b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh try { 585bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.setPasskey(mAddress, passkey); 586b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh } catch (RemoteException e) {Log.e(TAG, "", e);} 587b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh return false; 588b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh } 589b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh 59045e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** @hide */ 591bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public boolean setPairingConfirmation(boolean confirm) { 592b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh try { 593bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.setPairingConfirmation(mAddress, confirm); 594b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh } catch (RemoteException e) {Log.e(TAG, "", e);} 595b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh return false; 596b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh } 597b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh 59845e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** @hide */ 599bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public boolean cancelPairingUserInput() { 6009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 601bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.cancelPairingUserInput(mAddress); 6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (RemoteException e) {Log.e(TAG, "", e);} 6039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return false; 6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 6059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 6069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 60745e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * Create an RFCOMM {@link BluetoothSocket} ready to start a secure 60816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * outgoing connection to this remote device on given channel. 60945e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * <p>The remote device will be authenticated and communication on this 61045e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * socket will be encrypted. 61145e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * <p>Use {@link BluetoothSocket#connect} to intiate the outgoing 61245e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * connection. 61345e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * <p>Valid RFCOMM channels are in range 1 to 30. 614cf44059813539bf7f36dabd278cef93ba3122c56Nick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 615de893f550301a60274e87aa8168225e7a7a42184Nick Pelly * 61645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @param channel RFCOMM channel to connect to 61745e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection 618bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * @throws IOException on error, for example Bluetooth not available, or 61945e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * insufficient permissions 62016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * @hide 621bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly */ 62245e2704ff512d41e22af2801d76e96955469ce8dNick Pelly public BluetoothSocket createRfcommSocket(int channel) throws IOException { 62316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, channel, 62416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly null); 62516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly } 62616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly 62716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly /** 62816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * Create an RFCOMM {@link BluetoothSocket} ready to start a secure 62916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * outgoing connection to this remote device using SDP lookup of uuid. 63016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * <p>This is designed to be used with {@link 63116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * BluetoothAdapter#listenUsingRfcommWithServiceRecord} for peer-peer 63216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * Bluetooth applications. 63316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * <p>Use {@link BluetoothSocket#connect} to intiate the outgoing 63416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * connection. This will also perform an SDP lookup of the given uuid to 63516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * determine which channel to connect to. 63616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * <p>The remote device will be authenticated and communication on this 63716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * socket will be encrypted. 63816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 63916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * 64016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * @param uuid service record uuid to lookup RFCOMM channel 64116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection 64216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * @throws IOException on error, for example Bluetooth not available, or 64316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * insufficient permissions 64416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly */ 64516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly public BluetoothSocket createRfcommSocketToServiceRecord(UUID uuid) throws IOException { 64616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, -1, 64716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly new ParcelUuid(uuid)); 648bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 649bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly 650bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly /** 651bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * Construct an insecure RFCOMM socket ready to start an outgoing 652bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * connection. 653bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * Call #connect on the returned #BluetoothSocket to begin the connection. 654bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * The remote device will not be authenticated and communication on this 655bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * socket will not be encrypted. 656bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * @param port remote port 657bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * @return An RFCOMM BluetoothSocket 658bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * @throws IOException On error, for example Bluetooth not available, or 659bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * insufficient permissions. 66045e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @hide 661bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly */ 662bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public BluetoothSocket createInsecureRfcommSocket(int port) throws IOException { 66316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, false, false, this, port, 66416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly null); 665bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 666bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly 667bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly /** 668bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * Construct a SCO socket ready to start an outgoing connection. 669bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * Call #connect on the returned #BluetoothSocket to begin the connection. 670bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * @return a SCO BluetoothSocket 671bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * @throws IOException on error, for example Bluetooth not available, or 672bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * insufficient permissions. 67345e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @hide 674bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly */ 675bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public BluetoothSocket createScoSocket() throws IOException { 67616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly return new BluetoothSocket(BluetoothSocket.TYPE_SCO, -1, true, true, this, -1, null); 677bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 678bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly 679bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly /** 6809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Check that a pin is valid and convert to byte array. 6819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 6829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Bluetooth pin's are 1 to 16 bytes of UTF8 characters. 6839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param pin pin as java String 6849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return the pin code as a UTF8 byte array, or null if it is an invalid 6859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Bluetooth pin. 68645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @hide 6879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 6889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static byte[] convertPinToBytes(String pin) { 6899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (pin == null) { 6909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return null; 6919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 6929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project byte[] pinBytes; 6939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 6949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project pinBytes = pin.getBytes("UTF8"); 6959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (UnsupportedEncodingException uee) { 6969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Log.e(TAG, "UTF8 not supported?!?"); // this should not happen 6979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return null; 6989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 6999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (pinBytes.length <= 0 || pinBytes.length > 16) { 7009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return null; 7019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 7029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return pinBytes; 7039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 7049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 7059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 706