Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

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;

    /**
     * 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();

        // Ensures Bluetooth is available on the device and it is enabled. If not,
        // displays a dialog requesting user permission to mEnableCharacteristic Bluetooth.
        // TODO / FIXME currently this bugs if the user doesn't have BT enabled,
        // TODO / FIXME since it just returns null and doesn't check the result later.
        if (bluetoothLeAdapter == null || !bluetoothLeAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            _this.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            return null;
        } else {
            return bluetoothLeAdapter;
        }
    }
}