BluetoothDevice.java revision e6ee3be1c254404dad842298f6f56c11cc6c7ac8
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; 290e5d93b7ed983f98855555d560faf060836f1a52fJaikumar Ganesh /** A bond attempt failed because we received an Authentication Cancel 291e5d93b7ed983f98855555d560faf060836f1a52fJaikumar Ganesh * by remote end 292e5d93b7ed983f98855555d560faf060836f1a52fJaikumar Ganesh * @hide */ 293e5d93b7ed983f98855555d560faf060836f1a52fJaikumar Ganesh public static final int UNBOND_REASON_REMOTE_AUTH_CANCELED = 8; 29445e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** An existing bond was explicitly revoked 29545e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @hide */ 296e5d93b7ed983f98855555d560faf060836f1a52fJaikumar Ganesh public static final int UNBOND_REASON_REMOVED = 9; 2979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 29845e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** The user will be prompted to enter a pin 29945e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @hide */ 300b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh public static final int PAIRING_VARIANT_PIN = 0; 30145e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** The user will be prompted to enter a passkey 30245e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @hide */ 303b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh public static final int PAIRING_VARIANT_PASSKEY = 1; 30445e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** The user will be prompted to confirm the passkey displayed on the screen 30545e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @hide */ 30632d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh public static final int PAIRING_VARIANT_PASSKEY_CONFIRMATION = 2; 30732d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh /** The user will be prompted to accept or deny the incoming pairing request 30832d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh * @hide */ 30932d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh public static final int PAIRING_VARIANT_CONSENT = 3; 31032d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh /** The user will be prompted to enter the passkey displayed on remote device 31132d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh * @hide */ 31232d8571f509c392dca732c243e9b2138c15daecfJaikumar Ganesh public static final int PAIRING_VARIANT_DISPLAY_PASSKEY = 4; 313b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh 3141caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh /** 3151caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * Used as an extra field in {@link #ACTION_UUID} intents, 316aef439e6f825c0cb99a2ac08c8207f48b7a9fe10Nick Pelly * Contains the {@link android.os.ParcelUuid}s of the remote device which 317aef439e6f825c0cb99a2ac08c8207f48b7a9fe10Nick Pelly * is a parcelable version of {@link UUID}. 3181caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * @hide 3191caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh */ 3201caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh public static final String EXTRA_UUID = "android.bluetooth.device.extra.UUID"; 3211caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh 32216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly /** 32316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * Lazy initialization. Guaranteed final after first object constructed, or 32416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * getService() called. 32516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * TODO: Unify implementation of sService amongst BluetoothFoo API's 32616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly */ 32716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly private static IBluetooth sService; 328bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly 329bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly private final String mAddress; 330d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh 33116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly /*package*/ static IBluetooth getService() { 332bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly synchronized (BluetoothDevice.class) { 333bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly if (sService == null) { 334f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly IBinder b = ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE); 335bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly if (b == null) { 336bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly throw new RuntimeException("Bluetooth service not available"); 337bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 338bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly sService = IBluetooth.Stub.asInterface(b); 339bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 340bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 34116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly return sService; 34216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly } 343bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly 34416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly /** 34516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * Create a new BluetoothDevice 34616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * Bluetooth MAC address must be upper case, such as "00:11:22:33:AA:BB", 34716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * and is validated in this constructor. 34816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * @param address valid Bluetooth MAC address 34916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * @throws RuntimeException Bluetooth is not available on this platform 35016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * @throws IllegalArgumentException address is invalid 35116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * @hide 35216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly */ 35316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly /*package*/ BluetoothDevice(String address) { 35416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly getService(); // ensures sService is initialized 355005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly if (!BluetoothAdapter.checkBluetoothAddress(address)) { 356bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly throw new IllegalArgumentException(address + " is not a valid Bluetooth address"); 357bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 358bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly 359bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly mAddress = address; 3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 362bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly @Override 363bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public boolean equals(Object o) { 364bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly if (o instanceof BluetoothDevice) { 365bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return mAddress.equals(((BluetoothDevice)o).getAddress()); 366bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return false; 3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 370bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly @Override 371bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public int hashCode() { 372bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return mAddress.hashCode(); 373105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project } 374105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project 37545e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** 37645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * Returns a string representation of this BluetoothDevice. 37745e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * <p>Currently this is the Bluetooth hardware address, for example 37845e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * "00:11:22:AA:BB:CC". However, you should always use {@link #getAddress} 37945e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * if you explicitly require the Bluetooth hardware address in case the 38045e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * {@link #toString} representation changes in the future. 38145e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @return string representation of this BluetoothDevice 38245e2704ff512d41e22af2801d76e96955469ce8dNick Pelly */ 383bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly @Override 384bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public String toString() { 385bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return mAddress; 3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 388bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public int describeContents() { 389bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return 0; 390bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 391bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly 392bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public static final Parcelable.Creator<BluetoothDevice> CREATOR = 393bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly new Parcelable.Creator<BluetoothDevice>() { 394bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public BluetoothDevice createFromParcel(Parcel in) { 395bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return new BluetoothDevice(in.readString()); 396bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 397bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public BluetoothDevice[] newArray(int size) { 398bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return new BluetoothDevice[size]; 399bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 400bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly }; 401bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly 402bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public void writeToParcel(Parcel out, int flags) { 403bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly out.writeString(mAddress); 4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 40645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** 40745e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * Returns the hardware address of this BluetoothDevice. 40845e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * <p> For example, "00:11:22:AA:BB:CC". 40945e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @return Bluetooth hardware address as string 41045e2704ff512d41e22af2801d76e96955469ce8dNick Pelly */ 4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public String getAddress() { 412bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return mAddress; 4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 41645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * Get the friendly Bluetooth name of the remote device. 4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 41845e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * <p>The local adapter will automatically retrieve remote names when 41945e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * performing a device scan, and will cache them. This method just returns 42045e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * the name for this device from the cache. 421de893f550301a60274e87aa8168225e7a7a42184Nick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return the Bluetooth name, or null if there was a problem. 4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 4259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public String getName() { 4269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 427bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.getRemoteName(mAddress); 4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (RemoteException e) {Log.e(TAG, "", e);} 4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return null; 4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 433005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Start the bonding (pairing) process with the remote device. 434005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>This is an asynchronous call, it will return immediately. Register 435005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when 436005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * the bonding process completes, and its result. 437005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Android system services will handle the necessary user interactions 438005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * to confirm and complete the bonding process. 439005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 441005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * @return false on immediate error, true if bonding will begin 44218b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly * @hide 4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 444bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public boolean createBond() { 4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 446bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.createBond(mAddress); 4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (RemoteException e) {Log.e(TAG, "", e);} 4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return false; 4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 452005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Cancel an in-progress bonding request started with {@link #createBond}. 453005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 454005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * 455005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * @return true on sucess, false on error 45618b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly * @hide 4579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 458bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public boolean cancelBondProcess() { 4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 460bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.cancelBondProcess(mAddress); 4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (RemoteException e) {Log.e(TAG, "", e);} 4629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return false; 4639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 466005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Remove bond (pairing) with the remote device. 467005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Delete the link key associated with the remote device, and 468005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * immediately terminate connections to that device that require 469005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * authentication and encryption. 470005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 4719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 472005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * @return true on sucess, false on error 47318b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly * @hide 4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 475bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public boolean removeBond() { 4769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 477bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.removeBond(mAddress); 4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (RemoteException e) {Log.e(TAG, "", e);} 479bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return false; 4809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 483005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Get the bond state of the remote device. 484005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Possible values for the bond state are: 485005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * {@link #BOND_NONE}, 486005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * {@link #BOND_BONDING}, 487005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * {@link #BOND_BONDED}. 488005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. 4899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 490005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * @return the bond state 4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 492bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public int getBondState() { 4939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 494bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.getBondState(mAddress); 4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (RemoteException e) {Log.e(TAG, "", e);} 496005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly return BOND_NONE; 497005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly } 498005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly 499005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly /** 500005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * Get the Bluetooth class of the remote device. 501005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. 502005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * 503005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly * @return Bluetooth class object, or null on error 504005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly */ 505005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly public BluetoothClass getBluetoothClass() { 506005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly try { 507005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly int classInt = sService.getRemoteClass(mAddress); 508005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly if (classInt == BluetoothClass.ERROR) return null; 509005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly return new BluetoothClass(classInt); 510005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly } catch (RemoteException e) {Log.e(TAG, "", e);} 511005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly return null; 5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 514efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue /** 515efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue * Get trust state of a remote device. 516e6ee3be1c254404dad842298f6f56c11cc6c7ac8Nick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. 517efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue * @hide 518efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue */ 519efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue public boolean getTrustState() { 520efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue try { 521efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue return sService.getTrustState(mAddress); 522efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue } catch (RemoteException e) { 523efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue Log.e(TAG, "", e); 524efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue } 525efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue return false; 526efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue } 527efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue 528efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue /** 529efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue * Set trust state for a remote device. 530e6ee3be1c254404dad842298f6f56c11cc6c7ac8Nick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 531efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue * @param value the trust state value (true or false) 532efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue * @hide 533efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue */ 534efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue public boolean setTrust(boolean value) { 535efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue try { 536efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue return sService.setTrust(mAddress, value); 537efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue } catch (RemoteException e) { 538efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue Log.e(TAG, "", e); 539efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue } 540efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue return false; 541efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue } 542efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue 54345e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** @hide */ 544dd0463aef18d251c741bfc9dc7a2787443ef36f1Jaikumar Ganesh public ParcelUuid[] getUuids() { 5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 546bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.getRemoteUuids(mAddress); 5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (RemoteException e) {Log.e(TAG, "", e);} 5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return null; 5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 550d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh 5511caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh /** 5521caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * Perform a SDP query on the remote device to get the UUIDs 5531caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * supported. This API is asynchronous and an Intent is sent, 5541caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * with the UUIDs supported by the remote end. If there is an error 5551caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * in getting the SDP records or if the process takes a long time, 5561caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * an Intent is sent with the UUIDs that is currently present in the 5571caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * cache. Clients should use the {@link getUuids} to get UUIDs 5581caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * is SDP is not to be performed. 5591caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * 5601caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * @return False if the sanity check fails, True if the process 5611caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * of initiating an ACL connection to the remote device 5621caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * was started. 5631caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh * @hide 5641caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh */ 5651caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh public boolean fetchUuidsWithSdp() { 5661caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh try { 56716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly return sService.fetchRemoteUuids(mAddress, null, null); 5681caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh } catch (RemoteException e) {Log.e(TAG, "", e);} 5691caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh return false; 5701caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh } 5711caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh 57245e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** @hide */ 573dd0463aef18d251c741bfc9dc7a2787443ef36f1Jaikumar Ganesh public int getServiceChannel(ParcelUuid uuid) { 574d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh try { 575bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.getRemoteServiceChannel(mAddress, uuid); 576d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh } catch (RemoteException e) {Log.e(TAG, "", e);} 577b24e11baac589fe16426f2d243b460ab84991c7bNick Pelly return BluetoothDevice.ERROR; 5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 58045e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** @hide */ 581bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public boolean setPin(byte[] pin) { 5829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 583bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.setPin(mAddress, pin); 5849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (RemoteException e) {Log.e(TAG, "", e);} 5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return false; 5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 587b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh 58845e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** @hide */ 589bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public boolean setPasskey(int passkey) { 590b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh try { 591bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.setPasskey(mAddress, passkey); 592b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh } catch (RemoteException e) {Log.e(TAG, "", e);} 593b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh return false; 594b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh } 595b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh 59645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** @hide */ 597bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public boolean setPairingConfirmation(boolean confirm) { 598b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh try { 599bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.setPairingConfirmation(mAddress, confirm); 600b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh } catch (RemoteException e) {Log.e(TAG, "", e);} 601b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh return false; 602b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh } 603b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh 60445e2704ff512d41e22af2801d76e96955469ce8dNick Pelly /** @hide */ 605bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public boolean cancelPairingUserInput() { 6069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 607bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly return sService.cancelPairingUserInput(mAddress); 6089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (RemoteException e) {Log.e(TAG, "", e);} 6099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return false; 6109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 6119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 6129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 61345e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * Create an RFCOMM {@link BluetoothSocket} ready to start a secure 61416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * outgoing connection to this remote device on given channel. 61545e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * <p>The remote device will be authenticated and communication on this 61645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * socket will be encrypted. 61745e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * <p>Use {@link BluetoothSocket#connect} to intiate the outgoing 61845e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * connection. 61945e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * <p>Valid RFCOMM channels are in range 1 to 30. 620cf44059813539bf7f36dabd278cef93ba3122c56Nick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 621de893f550301a60274e87aa8168225e7a7a42184Nick Pelly * 62245e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @param channel RFCOMM channel to connect to 62345e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection 624bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * @throws IOException on error, for example Bluetooth not available, or 62545e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * insufficient permissions 62616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * @hide 627bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly */ 62845e2704ff512d41e22af2801d76e96955469ce8dNick Pelly public BluetoothSocket createRfcommSocket(int channel) throws IOException { 62916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, channel, 63016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly null); 63116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly } 63216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly 63316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly /** 63416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * Create an RFCOMM {@link BluetoothSocket} ready to start a secure 63516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * outgoing connection to this remote device using SDP lookup of uuid. 63616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * <p>This is designed to be used with {@link 63716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * BluetoothAdapter#listenUsingRfcommWithServiceRecord} for peer-peer 63816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * Bluetooth applications. 63916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * <p>Use {@link BluetoothSocket#connect} to intiate the outgoing 64016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * connection. This will also perform an SDP lookup of the given uuid to 64116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * determine which channel to connect to. 64216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * <p>The remote device will be authenticated and communication on this 64316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * socket will be encrypted. 64416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 64516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * 64616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * @param uuid service record uuid to lookup RFCOMM channel 64716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection 64816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * @throws IOException on error, for example Bluetooth not available, or 64916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly * insufficient permissions 65016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly */ 65116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly public BluetoothSocket createRfcommSocketToServiceRecord(UUID uuid) throws IOException { 65216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, -1, 65316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly new ParcelUuid(uuid)); 654bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 655bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly 656bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly /** 657bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * Construct an insecure RFCOMM socket ready to start an outgoing 658bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * connection. 659bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * Call #connect on the returned #BluetoothSocket to begin the connection. 660bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * The remote device will not be authenticated and communication on this 661bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * socket will not be encrypted. 662e6ee3be1c254404dad842298f6f56c11cc6c7ac8Nick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} 663e6ee3be1c254404dad842298f6f56c11cc6c7ac8Nick Pelly * 664bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * @param port remote port 665bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * @return An RFCOMM BluetoothSocket 666bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * @throws IOException On error, for example Bluetooth not available, or 667bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * insufficient permissions. 66845e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @hide 669bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly */ 670bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public BluetoothSocket createInsecureRfcommSocket(int port) throws IOException { 67116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, false, false, this, port, 67216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly null); 673bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 674bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly 675bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly /** 676bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * Construct a SCO socket ready to start an outgoing connection. 677bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * Call #connect on the returned #BluetoothSocket to begin the connection. 678e6ee3be1c254404dad842298f6f56c11cc6c7ac8Nick Pelly * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} 679e6ee3be1c254404dad842298f6f56c11cc6c7ac8Nick Pelly * 680bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * @return a SCO BluetoothSocket 681bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * @throws IOException on error, for example Bluetooth not available, or 682bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * insufficient permissions. 68345e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @hide 684bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly */ 685bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly public BluetoothSocket createScoSocket() throws IOException { 68616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly return new BluetoothSocket(BluetoothSocket.TYPE_SCO, -1, true, true, this, -1, null); 687bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly } 688bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly 689bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly /** 6909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Check that a pin is valid and convert to byte array. 6919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 6929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Bluetooth pin's are 1 to 16 bytes of UTF8 characters. 6939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param pin pin as java String 6949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return the pin code as a UTF8 byte array, or null if it is an invalid 6959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Bluetooth pin. 69645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * @hide 6979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 6989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static byte[] convertPinToBytes(String pin) { 6999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (pin == null) { 7009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return null; 7019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 7029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project byte[] pinBytes; 7039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 7049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project pinBytes = pin.getBytes("UTF8"); 7059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (UnsupportedEncodingException uee) { 7069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Log.e(TAG, "UTF8 not supported?!?"); // this should not happen 7079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return null; 7089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 7099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (pinBytes.length <= 0 || pinBytes.length > 16) { 7109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return null; 7119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 7129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return pinBytes; 7139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 7149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 7159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 716