para permitir que el Bluetooth se puede utilizar cualquiera de las siguientes funciones:
public void enableBT(View view){
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()){
Intent intentBtEnabled = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
// The REQUEST_ENABLE_BT constant passed to startActivityForResult() is a locally defined integer (which must be greater than 0), that the system passes back to you in your onActivityResult()
// implementation as the requestCode parameter.
int REQUEST_ENABLE_BT = 1;
startActivityForResult(intentBtEnabled, REQUEST_ENABLE_BT);
}
}
La segunda función es:
public void enableBT(View view){
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()){
mBluetoothAdapter.enable();
}
}
La diferencia es que el La primera función hace que la aplicación solicite permiso al usuario para encender Bluetooth o para denegar. La segunda función hace que la aplicación encienda el Bluetooth directamente.
para desactivar el Bluetooth usar la siguiente función:
public void disableBT(View view){
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter.isEnabled()){
mBluetoothAdapter.disable();
}
}
NOTA/La primera función sólo necesita el siguiente permiso para ser definido en el archivo AndroidManifest.xml:
<uses-permission android:name="android.permission.BLUETOOTH"/>
Mientras, el segunda y tercera funciones necesitan los siguientes permisos:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
¿Cómo es posible que no trabaja? ¿Estás obteniendo un error? Si es así, ¿cuál es el error? –
BluetoothAdapter muestra un error en SDK 1.5 – user458295