Enable bluetooth and get the adapter object - Android Bluetooth

Android examples for Bluetooth:Turn On bluetooth

Description

Enable bluetooth and get the adapter object

Demo Code


//package com.java2s;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;

import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;

public class Main {
    public final static int REQUEST_ENABLE_BT = 0xB1007007;

    /**/*from   w w  w . j av a2  s  . c  om*/
     * Enable bluetooth and get the adapter object
     * @return A BluetoothAdapter instance
     */
    public static BluetoothAdapter getLeAdapter(Activity _this) {
        // Initializes Bluetooth adapter.
        final BluetoothManager bluetoothLeManager = (BluetoothManager) _this
                .getSystemService(Context.BLUETOOTH_SERVICE);
        final BluetoothAdapter bluetoothLeAdapter = bluetoothLeManager
                .getAdapter();

        if (bluetoothLeAdapter == null || !bluetoothLeAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            _this.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            return null;
        } else {
            return bluetoothLeAdapter;
        }
    }
}

Related Tutorials