Question

My goal is to build an application that discovers free parking spots via bluetooth and gps. I assume that driver has: - my application - Bluetooth always turned on in his smartphone - Bluetooth Module in his car( BT Headphone etc.)

I would like to my app discovers if car's bluetooth device (CarDevice) with known MAC address is nearby and detects if CarDevice is no longer available (Engine is turned off).

As for now, I have a service that scans all nearby devices and if it finds correct one, it shows Toast.

But how I can detect if CarDevice is no longer in range?

This information should be received as fast as it can be.

  1. Should I repeat bluetootDevice.doDiscovery for every short amount of time like 0.5 sec or 1 sec?- this will be probably power-consuming

  2. Or maybe there is some List where Android keeps detected and reachable devices?

  3. Or I should presume that if someone has a car with Bluetooth module, he always use it so I should focused on disconnection event? If yes, how should I do that?

Here is service code:

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;    
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import java.util.Set;

public class DriveService extends Service {


public static final String TAG = "DriveService";

int mStartMode;       // indicates how to behave if the service is killed
private BluetoothAdapter bluetoothAdapter = null;
private ArrayAdapter<String> discoverdDevicesArrayAdapter;
private ArrayAdapter<String> pairedDevicesArrayAdapter;

//test
public String macAdresss = "7C:D1:C3:F0:13:62";

@Override
public void onCreate() {
    // The service is being created
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    Log.d(TAG, "onCreate");
    if(bluetoothAdapter.isDiscovering()){
        bluetoothAdapter.cancelDiscovery();
    }

}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // The service is starting, due to a call to startService()

    Log.d(TAG, "onStartCommand");

    bluetoothAdapter.startDiscovery();

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    this.registerReceiver(mReceiver, filter);


    return mStartMode;
}

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.d(TAG,"onReceive");


        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // If it's already paired, skip it, because it's been listed already
            String deviceAdres = device.getAddress();
            if (deviceAdres.equals(macAdresss)) {
                Toast.makeText(getApplicationContext(),"mac znaleziony",Toast.LENGTH_LONG).show();
            }

        }
    }
};


@Override
public void onDestroy() {
    // The service is no longer used and is being destroyed
}
}

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top