2012-06-01 354 views

回答

0

是的,这是非常可能的

第一步你需要创建一个服务

第二步你需要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 

希望这有助于。

1

是的,你的服务可以侦听新的蓝牙设备,如VIPUL沙阿描述,但真正的问题是你如何使你的设备找到摆在首位其它蓝牙设备。当远程设备发现期间找到

ACTION_FOUND被发送。您可以拨打BluetoothAdapter.startDiscovery()来启动发现过程,但问题是通常可发现的设备非常少。几年前,通常设备始终保持可发现状态,但现在,用户需要根据需要暂时发现设备以配对设备。

所以,它没有任何意义有周期性确实发现一个服务(和监听ACTION_FOUND),不仅因为它消耗了大量的电池,并因为你不会找到任何东西。

如果你知道你正在寻找的,那么你可以尝试连接到这些设备的蓝牙地址,但我认为不是这样的。