小编Szy*_*ymX的帖子

在Android中无需连接即可检测蓝牙设备

我的目标是建立一个通过蓝牙和GPS发现免费停车位的应用程序.我假设司机有: - 我的应用程序 - 蓝牙总是打开他的智能手机 - 他的车里的蓝牙模块(BT耳机等)

我想我的应用程序发现附近有已知MAC地址的汽车蓝牙设备(CarDevice)并检测CarDevice是否不再可用(引擎已关闭).

至于现在,我有一个扫描所有附近设备的服务,如果找到正确的设备,它会显示Toast.

但是如何检测CarDevice是否不在范围内?

应尽快收到此信息.

  1. 我应该在0.5秒或1秒内每隔短时间重复bluetootDevice.doDiscovery吗? - 这可能会耗费大量功耗

  2. 或者可能有一些列表,其中Android保持检测和可访问的设备?

  3. 或者我应该假设如果某人有车载蓝牙模块,他总是使用它,所以我应该专注于断线事件?如果是的话,我该怎么做?

这是服务代码:

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 …
Run Code Online (Sandbox Code Playgroud)

connection android asynchronous android-service android-bluetooth

5
推荐指数
0
解决办法
664
查看次数