Bluetooth Permissions
In order to use Bluetooth features in your application, you need to declare at least one of two Bluetooth permissions: BLUETOOTH
and BLUETOOTH_ADMIN
.
You must request the BLUETOOTH
permission in order to perform any Bluetooth communication, such as requesting a connection, accepting a connection, and transferring data.
You must request the BLUETOOTH_ADMIN
permission in order to initiate device discovery or manipulate Bluetooth settings. Most applications need this permission solely for the ability to discover local Bluetooth devices. The other abilities granted by this permission should not be used, unless the application is a "power manager" that will modify Bluetooth settings upon user request. Note: If you use BLUETOOTH_ADMIN
permission, then must also have the BLUETOOTH
permission.
Declare the Bluetooth permission(s) in your application manifest file. For example:
<manifest ... >
<uses-permission android:name="android.permission.BLUETOOTH" />
...
</manifest>
See the <uses-permission> reference for more information about declaring application permissions.
Setting Up Bluetooth
Figure 1: The enabling Bluetooth dialog.
Before your application can communicate over Bluetooth, you need to verify that Bluetooth is supported on the device, and if so, ensure that it is enabled.
If Bluetooth is not supported, then you should gracefully disable any Bluetooth features. If Bluetooth is supported, but disabled, then you can request that the user enable Bluetooth without leaving your application. This setup is accomplished in two steps, using the BluetoothAdapter
.
- Get the
BluetoothAdapter
The
BluetoothAdapter
is required for any and all Bluetooth activity. To get theBluetoothAdapter
, call the staticgetDefaultAdapter()
method. This returns aBluetoothAdapter
that represents the device's own Bluetooth adapter (the Bluetooth radio). There's one Bluetooth adapter for the entire system, and your application can interact with it using this object. IfgetDefaultAdapter()
returns null, then the device does not support Bluetooth and your story ends here. For example:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
} - Enable Bluetooth
Next, you need to ensure that Bluetooth is enabled. Call
isEnabled()
to check whether Bluetooth is currently enable. If this method returns false, then Bluetooth is disabled. To request that Bluetooth be enabled, callstartActivityForResult()
with theACTION_REQUEST_ENABLE
action Intent. This will issue a request to enable Bluetooth through the system settings (without stopping your application). For example:
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}A dialog will appear requesting user permission to enable Bluetooth, as shown in Figure 1. If the user responds "Yes," the system will begin to enable Bluetooth and focus will return to your application once the process completes (or fails).
The
REQUEST_ENABLE_BT
constant passed tostartActivityForResult()
is a locally defined integer (which must be greater than 0), that the system passes back to you in youronActivityResult()
implementation as therequestCode
parameter.If enabling Bluetooth succeeds, your activity receives the
RESULT_OK
result code in theonActivityResult()
callback. If Bluetooth was not enabled due to an error (or the user responded "No") then the result code isRESULT_CANCELED
.
Optionally, your application can also listen for the ACTION_STATE_CHANGED
broadcast Intent, which the system will broadcast whenever the Bluetooth state has changed. This broadcast contains the extra fields EXTRA_STATE
and EXTRA_PREVIOUS_STATE
, containing the new and old Bluetooth states, respectively. Possible values for these extra fields are STATE_TURNING_ON
, STATE_ON
, STATE_TURNING_OFF
, and STATE_OFF
. Listening for this broadcast can be useful to detect changes made to the Bluetooth state while your app is running.
Tip: Enabling discoverability will automatically enable Bluetooth. If you plan to consistently enable device discoverability before performing Bluetooth activity, you can skip step 2 above. Read about enabling discoverability, below.
'공부 > Android' 카테고리의 다른 글
안드로이드 환경설정 만들기(Preference Activity) (0) | 2013.07.15 |
---|---|
안드로이드 블루투스 정리(android bluetooth) (0) | 2013.07.15 |
안드로이드 PreferenceActivity (0) | 2013.07.12 |
어플리케이션 zipalign (apk zipalign) (0) | 2013.02.24 |
안드로이드 php, mysql 연동하기 _ 2 (3) | 2013.01.20 |