1
To Enable the Bluetooth you could use either of the following functions: 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); } } The second function is: public void enableBT( View view){ BluetoothAdapter mBluetoothAdapter = BluetoothAdapter .getDefaultAdapter(); if (!mBluetoothAdapter.isEnabled()){ mBluetoothAdapter.enable(); } } The difference is that the first function makes the app ask the user a permission to turn on the Bluetooth or to deny. The second function makes the app turn on the Bluetooth directly. To Disable the Bluetooth use the following function: public void disableBT( View view){ BluetoothAdapter mBluetoothAdapter = BluetoothAdapter .getDefaultAdapter(); if (mBluetoothAdapter.isEnabled()){ mBluetoothAdapter.disable(); } } NOTE/ The first function needs only the following permission to be defined in the AndroidManifest.xml file: <uses-permission android:name = "android.permission.BLUETOOTH" /> While, the second and third functions need the following permissions: <uses-permission android:name = "android.permission.BLUETOOTH" /> <uses-permission android:name = "android.permission.BLUETOOTH_ADMIN" />

How To Enable the Bluetooth For Your Android App

Embed Size (px)

DESCRIPTION

this document provides the detailed information about how to activate bluetooth functions on your android device for your application.

Citation preview

To Enable the Bluetooth you could use either of the following functions: 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); } }The second function is:public void enableBT(View view){ BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (!mBluetoothAdapter.isEnabled()){ mBluetoothAdapter.enable(); }}The difference is that the first function makes the app ask the user a permission to turn on the Bluetooth or to deny. The second function makes the app turn on the Bluetooth directly.To Disable the Bluetooth use the following function:public void disableBT(View view){ BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter.isEnabled()){ mBluetoothAdapter.disable(); }}NOTE/ The first function needs only the following permission to be defined in the AndroidManifest.xml file:

While, the second and third functions need the following permissions: