Communicating Android application with Bluetooth

1

I'm trying to make an implementation to display paired Bluetooth devices and those connected around a mobile phone. I made several searches , I found a example and now I'm trying to build a my implementation. What I want is to build a class called Bluetooth which has the following implementations:

A method to check if your phone's Bluetooth is enabled, if not, enable it. A list of paired and connected devices. A method for connecting to the Bluetooth device.

So I started the development, arriving at the following code:

package br.ufscar.dc.controledepatrimonio.Util.RFID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import br.ufscar.dc.controledepatrimonio.R;

public class Bluetooth extends BroadcastReceiver {
    private BluetoothAdapter dispositivo;
    private Activity activity;

    public Bluetooth (Activity activity) {
        dispositivo = BluetoothAdapter.getDefaultAdapter();
        this.activity = activity;
    }

    private void habilitarConexao(Bluetooth bluetooth) {
        if (bluetooth.dispositivo == null) {
            Toast.makeText(activity, R.string.msg_bluetooth_nao_suportado, Toast.LENGTH_LONG);
        }
        if (!bluetooth.dispositivo.isEnabled()) {
            Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            activity.startActivity(i, REQUEST_ENABLE_BT);
        }

    }

    @Override
    public void onReceive(Context context, Intent intent) {

    }
}

The problem is that the REQUEST_ENABLE_BT constant has the following error: "Can not resolve symbol 'REQUEST_ENABLE_BT'" I even found a post on that, and I did what is commented there, but another error occurs: "Wrong 2nd argument type. Found: 'int'. Required: 'android.os.Bundle'"

I wonder why. Since if I do the same encoding in an Activity it works normally. Thanks in advance for the help !!

    
asked by anonymous 06.08.2015 / 13:53

1 answer

0

People have come up with a solution. I did this:

package br.ufscar.dc.controledepatrimonio.Util.RFID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import br.ufscar.dc.controledepatrimonio.R;

public class Bluetooth extends BroadcastReceiver {
    private BluetoothAdapter dispositivo;

    public BluetoothAdapter getDispositivo() {
        return dispositivo;
    }

    public enum Estado {
        LIGADO, DESLIGADO, NAO_COMPATIVEL;
    }

    public Bluetooth () {
        dispositivo = BluetoothAdapter.getDefaultAdapter();
    }

    public Estado verificarEstado() {
        if (dispositivo == null) {
            return Estado.NAO_COMPATIVEL;
        }
        if (!dispositivo.isEnabled()) {
            return Estado.DESLIGADO;
        }

        return Estado.LIGADO;
    }

    @Override
    public void onReceive(Context context, Intent intent) {

    }

}

With this I return the state of Bluetooth to my Activity and it is responsible for activating or not the service. I will continue to implement the other methods!

    
06.08.2015 / 14:50