【问题标题】:Pinging a bluetooth device in Android在 Android 中 Ping 蓝牙设备
【发布时间】:2013-12-09 21:39:54
【问题描述】:

有没有办法在 Android 中 ping 蓝牙设备?蓝牙设备未在 Android 中连接或配对,但我事先知道设备的 MAC 地址和 PIN。我想要实现的是 ping MAC 地址列表以查看是否有任何设备在范围内。

【问题讨论】:

  • 我认为MAC地址不适用于蓝牙。通用硬件地址似乎是。我现在只是在大声思考。
  • 您所描述的内容听起来很像蓝牙发现。当您收到有关可发现设备的信息时,您会获得名称和 MAC 地址。如果您提前知道 MAC 地址,那么您可以尝试连接它,但对每个设备执行此操作会有延迟。
  • 我在想一种比连接它更快的方法,比如简单的 ping。

标签: android bluetooth


【解决方案1】:

已解决:我所做的是查询设备上可用的服务(可用的 UUID)。如果收到 UUID,则设备在范围内。

所以步骤是:

  • 注册为 UUID 操作接收的广播

    String action = "android.bluetooth.device.action.UUID";
    IntentFilter filter = new IntentFilter(action);
    registerReceiver(mReceiver, filter);
    
  • 根据远程地址创建蓝牙设备并获取其 UUID

    BluetoothDevice bd = bluetoothAdapter.getRemoteDevice(address);
    bd.fetchUuidsWithSdp(); 
    
  • 创建一个广播接收器,它携带设备地址,能够告诉我那个

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
           //deviceExtra is our in range device
           deviceExtra  = 
           intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
           Parcelable[] uuidExtra = 
           intent.getParcelableArrayExtra("android.bluetooth.device.extra.UUID");
    }}};
    

【讨论】: