android bluetooth开发基础-5发现设备

来源:互联网 发布:大数据金融教学实验室 编辑:程序博客网 时间:2024/05/01 05:32

使用BluetoothAdapter,你能够通过设备发现(device discovery)或者通过查询配对设备的列表来发现远程蓝牙设备。

设备发现(Device discovery)是搜查本地启动蓝牙的设备,然后请求该设备一些信息的一个扫描过程(有时,这被称为“discovering”,“inquiring”或者“scannig”)。但是,本地蓝牙设备只有在启动蓝牙的时候才会对发现请求作出响应。如果一个设备被发现,它将通过共享一些信息,如设备名称、类别和唯一的MAC地址,来对发现请求作出响应。使用这些信息,执行设备发现请求动作的设备就能够初始化一个连接,对被发现的设备发出连接请求。

如果一个远程设备第一次请求连接,那么接收到连接请求的设备会自动发送一个配对请求。如果一个设备已经被配对,那么关于该设备的基本信息(设备名称、类别和MAC地址)将会被保存,并且能用Bluetooth APIs读取。知道了一个远程设备的MAC地址之后,就可以使用该MAC地址在任何时间初始化一个连接,无需再执行device discovery(假设该设备在距离范围之内)。

在被配对和被连接之间是有区别的。被配对意味着两个设备彼此知道对方的存在,有一个连接key被用于认证,能够建立一条加密的连接。被连接意味着设备当前共享一个RFCOMM渠道并且能够传输数据给对方。Android Bluetooth APIs要求设备在建立RFCOMM连接之前要先配对。配对是在你使用Bluetooth APIs建立一个加密连接的时候自动执行的。

下面的章节将描述发现已经配对的设备,或者,使用device discovery发现新的设备。

 

Note: Android-powered devices are not discoverable by default. A user can make the device discoverable for a limited time through the system settings, or an application can request that the user enable discoverability without leaving the application. How to enable discoverability is discussed below.

查询配对设备

在执行device discovery之前,最好在已配对的设备列表中查看所要发现的设备是否已经存在。通过调用getBondedDevices()函数可以获得代表已经配对的设备的BluetoothDevice集合。 例如,你可以查询所有已经配对的设备,然后通过一个ArrayAdapter添加和显示每个设备的名字给用户:

[java] view plaincopy
  1. Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();  
  2. // If there are paired devices  
  3. if (pairedDevices.size() > 0) {     
  4.  // Loop through paired devices      
  5. for (BluetoothDevice device : pairedDevices) {          
  6. // Add the name and address to an array adapter to show in a ListView   
  7. mArrayAdapter.add(device.getName() + "/n" + device.getAddress());    }}  

为了建立一个连接,需要才能够BluetoothDevice对象中获取的是MAC地址。在这个例子中,MAC地址作为显示给用户的ArrayAdapter的一部分存储。只要有需要,可以把MAC地址提取出来。

发现设备

调用startDiscovery()开始设备发现的过程,这个过程是异步的,startDiscovery()方法会立即返回一个boolean的值表示启动是否成功。这个发现过程通常包括大约12秒的查询扫描,之后是在发现的设备中查询其蓝牙名称。

你的应用程序中必须注册一个ACTION_FOUND Intent的BroadcastReceiver,用于接收发现一个蓝牙设备时发出的信息。对于每一个设备,系统将广播ACTION_FOUND的Intent。这个Intent包含了一些附加数据域——EXTRA_DEVICE和EXTRA_CLASS,分别包含BluetoothDevice类和BluetoothClass类的实例。

下面代码展示了如何注册设备发现时的广播处理函数:

 

[java] view plaincopy
  1. // Create a BroadcastReceiver for ACTION_FOUND  
  2. private final BroadcastReceiver mReceiver = new BroadcastReceiver() {    public void onReceive(Context context, Intent intent)   
  3. {          
  4.    String action = intent.getAction();          
  5. // When discovery finds a device          
  6.    if (BluetoothDevice.ACTION_FOUND.equals(action)) {              
  7. // Get the BluetoothDevice object from the Intent     
  8.    BluetoothDevice device = intent.getParcelableExtra(    BluetoothDevice.EXTRA_DEVICE);              
  9. // Add the name and address to an array adapter to show in a ListView            mArrayAdapter.add(device.getName() + "/n" + device.getAddress());  
  10.         }  
  11.     }};  
  12. // Register the BroadcastReceiverIntentFilter  
  13.  filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);  
  14. registerReceiver(mReceiver, filter);   
  15. // Don't forget to unregister during onDestroy  

 

为了初始化一个连接,我们需要从BluetoothDevice对象中获取MAC地址。

注意: 执行设备发现这个过程,需要花费蓝牙适配器大量资源,是一个重量级过程。如果你发现一个设备并要连接它,最好先调用cancelDiscovery()方法来停止设备发现过程。如果你已经有一个连接,那么执行设备发现过程或导致连接的带宽大幅度减少,所以当你已经有连接的时候最好就不要执行设备发现过程了。

 

启动发现功能

如果你想要你的设备能被其他设备发现,调用startActivityForResult(Intent,int),传递一个ACTION_REQUEST_DISCOVERABLE action Intent给它。这将发送一个请求给系统设置以启动可被发现模式。可被发现模式一般默认持续120秒,你可以通过给Intent添加一个EXTRA_DISCOVERABLE_DURATION Intent extra来更改可被发现模式的持续时间,这个时间最大是300秒。

请看代码示例:

[java] view plaincopy
  1. Intent discoverableIntent = newIntent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);  
  2. discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);  
  3. startActivity(discoverableIntent);  

 

 

启动可被发现模式 

Figure 2: The enabling discoverability dialog.

 

一个对话框将会出现,请求用户权限来启动设备的可被发现模式,如Figure 2所示。如果用户点击“Yes”,那么设备在设定的时间内将是可被发现的。你的Activity将调用onActivityResult()回调函数。如果用户点击“No”,那么将产生一个错误,结果码将是Activity.RESULT_CANCELLED。

 

 

Note: If Bluetooth has not been enabled on the device, then enabling device discoverability will automatically enable Bluetooth.

The device will silently remain in discoverable mode for the allotted time. If you would like to be notified when the discoverable mode has changed, you can register a BroadcastReceiver for theACTION_SCAN_MODE_CHANGED Intent. This will contain the extra fields EXTRA_SCAN_MODE andEXTRA_PREVIOUS_SCAN_MODE, which tell you the new and old scan mode, respectively. Possible values for each are SCAN_MODE_CONNECTABLE_DISCOVERABLESCAN_MODE_CONNECTABLE, orSCAN_MODE_NONE, which indicate that the device is either in discoverable mode, not in discoverable mode but still able to receive connections, or not in discoverable mode and unable to receive connections, respectively.

You do not need to enable device discoverability if you will be initiating the connection to a remote device. Enabling discoverability is only necessary when you want your application to host a server socket that will accept incoming connections, because the remote devices must be able to discover the device before it can initiate the connection.

原创粉丝点击