Android蓝牙 - 用于记录附近蓝牙设备的服务

时间:2012-06-01 03:46:58

标签: android bluetooth

是否可以创建可以侦听附近设备并将设备信息记录到文件的服务?

2 个答案:

答案 0 :(得分:1)

是的,您的服务可以收听新的蓝牙设备,如Vipul Shah所述,但真正的问题是如何让您的设备首先找到其他蓝牙设备。

在发现期间找到远程设备时发送ACTION_FOUND。您可以调用BluetoothAdapter.startDiscovery()来启动发现过程,但问题是很少有设备通常是可发现的。几年前,设备始终可以被发现是很常见的,但现在用户需要根据需要临时发现设备,以便配对。

因此,拥有一个定期发现(并监听ACTION_FOUND)的服务是没有意义的,因为它消耗了大量的电池并且因为你找不到任何东西。

如果你知道你正在寻找的设备的蓝牙地址,那么你可以尝试连接它们,但我认为情况并非如此。

答案 1 :(得分:0)

是的,这是非常可能的

第1步您需要创建一项服务

步骤2您需要 BluetoothDevice.ACTION_FOUND广播接收器来查找附近的设备。

步骤3然后,您可以逐个查询所有找到的设备

步骤4您将快速枚举找到的设备将其信息转储到文件中。

以下是广播接收器

 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            // 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);
                // Add the name and address to an array adapter to show in a ListView
                // You will log this information into file.
                mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
        }
    };

注册广播接收器以进行意图行动,如下所示

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy

希望这有帮助。