Communicating Android app with Bluetooth

Asked

Viewed 176 times

1

I’m trying to perform an implementation to display paired Bluetooth devices and those connected around a mobile phone. I did several research, I found a example and now I’m trying to build an implementation of mine. What I want is to build a class called Bluetooth which has the following implementations:

A method to check if the mobile’s Bluetooth is enabled, if not, enable. A list of paired and connected devices. A method to connect with 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 presents the following error: "Cannot resolve Symbol 'REQUEST_ENABLE_BT'" I even found a post about it, and I did what is commented there, but another error occurs: "Wrong 2nd argument type. Found: 'int'. required: 'android.os.Bundle'"

I would like to know why. Since if I do the same coding in an Activity it works normally. Thanks in advance for the help!!

1 answer

0

Guys, I’ve come up with a solution. I did it this way:

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 status of Bluetooth to my Activity and she is responsible for activating or not the service. I will continue the implementation of the other methods!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.