Android How to - Is Bluetooth Open








Question

We would like to know how to is Bluetooth Open.

Answer

/*  w  ww  . j a  v a 2 s  .c  o  m*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.util.Log;

class BlueToothUtil {

    private Context context;
    private BluetoothAdapter bluetoothAdapter = null;
    private BluetoothSocket bluetoothSocket = null;
    private OutputStream outputStream = null;
    private InputStream inputStream = null;

    private static final String TAG = "BlueTooth_Util";

    public BlueToothUtil(Context context,BluetoothSocket bluetoothSocket) {
        this.context = context;
        this.bluetoothSocket = bluetoothSocket;
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    }

    public boolean IsBlueToothOpen() {
        if (bluetoothAdapter.isEnabled()) {
            return true;
        } else {
            return false;
        }
    }

    public void WriteData(int data) {
        try {
            outputStream = bluetoothSocket.getOutputStream();
            inputStream = bluetoothSocket.getInputStream();
            Log.d(TAG, data+"");
            outputStream.write(data);
        } catch (IOException e) {
            Log.d(TAG, e.getMessage());
        }
    }
}