`
897371388
  • 浏览: 531081 次
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

使用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 toenable discoverabilityis discussed below.

查询配对设备

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

  1. Set<BluetoothDevice>pairedDevices=mBluetoothAdapter.getBondedDevices();
  2. //Iftherearepaireddevices
  3. if(pairedDevices.size()>0){
  4. //Loopthroughpaireddevices
  5. for(BluetoothDevicedevice:pairedDevices){
  6. //AddthenameandaddresstoanarrayadaptertoshowinaListView
  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类的实例。

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

  1. //CreateaBroadcastReceiverforACTION_FOUND
  2. privatefinalBroadcastReceivermReceiver=newBroadcastReceiver(){publicvoidonReceive(Contextcontext,Intentintent)
  3. {
  4. Stringaction=intent.getAction();
  5. //Whendiscoveryfindsadevice
  6. if(BluetoothDevice.ACTION_FOUND.equals(action)){
  7. //GettheBluetoothDeviceobjectfromtheIntent
  8. BluetoothDevicedevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  9. //AddthenameandaddresstoanarrayadaptertoshowinaListViewmArrayAdapter.add(device.getName()+"/n"+device.getAddress());
  10. }
  11. }};
  12. //RegistertheBroadcastReceiverIntentFilter
  13. filter=newIntentFilter(BluetoothDevice.ACTION_FOUND);
  14. registerReceiver(mReceiver,filter);
  15. //Don'tforgettounregisterduringonDestroy

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

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

启动发现功能

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

请看代码示例:

  1. IntentdiscoverableIntent=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_CHANGEDIntent. This will contain the extra fieldsEXTRA_SCAN_MODEandEXTRA_PREVIOUS_SCAN_MODE, which tell you the new and old scan mode, respectively. Possible values for each areSCAN_MODE_CONNECTABLE_DISCOVERABLE,SCAN_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.

分享到:
评论

相关推荐

    新版Android开发教程.rar

    ----------------------------------- Android 编程基础 1 封面----------------------------------- Android 编程基础 2 开放手机联盟 --Open --Open --Open --Open Handset Handset Handset Handset Alliance ...

    Android 蓝牙基础功能的开发

    Android 蓝牙基础功能的开发,功能包括扫描、连接、发送等,分为中央设备和外设蓝牙2个apk源码

    Android应用程序开发教程PDF电子书完整版、Android开发学习教程

    最底层的是一个 Linux Kernel,加载了几个移动设备必要的系统驱动(这么说来 Android 基础系统是要以 GPL 发布了?不知道 34 家厂商的硬件开发商们是怎么样想的);上面是类库和 Runtime,绿色的类库部分可以看 到...

    Android 初学中阶高阶书籍_集合打包2

    段,Android 图像处理篇,Android_CTS测试研,GoogleMaps基础开发教程,Android_GPS架构分析,android_JNI编程_一些技 巧,android_jni操作指南,Android_NDK开发实例,Android_RIL层剖析(官方翻译),Android2.2+API+中文...

    Android 初学中阶高阶书籍_集合打包3

    段,Android 图像处理篇,Android_CTS测试研,GoogleMaps基础开发教程,Android_GPS架构分析,android_JNI编程_一些技 巧,android_jni操作指南,Android_NDK开发实例,Android_RIL层剖析(官方翻译),Android2.2+API+中文...

    C++实现蓝牙bluetooth通讯功能

    C++实现蓝牙bluetooth通讯功能,基础功能实现,源代码,

    Android蓝牙和Cors网络开发源码

    内含两个独立的模块:蓝牙通信和Cors...其中蓝牙通信是在android sdk中提供的关于蓝牙的sample的基础上修改完成的,cors网络通信则是本人自己根据项目需要结合socket网络编程技术完成的,经测试能正常拿到差分数据。

    BLE初级入门资料集合

    Bluetooth低能耗技术基础知识.pdf Bluetooth技术-基础知识和品牌.pdf Getting Started with Bluetooth Low Energy.pdf RTL8762 CC254X 小蜜蜂BLE开发板简介v.pdf TI官方BLE教程.pdf xRTL8762AK_Datasheet_1.0 ...

    android开发教程之获取power_profile.xml文件的方法(android运行时能耗值)

    系统的设置–&gt;电池–&gt;使用情况中,统计的能耗的使用情况也是以power_profile.xml的value作为基础参数的1、我的...”Android”&gt; ”none”&gt;0 ”screen.on”&gt;100 ”bluetooth.active”&gt;142 &lt;item name=”bluetoo

    安卓java读取网页源码-alidd-samples:alidd-样品

    一款针对Android平台下快速集成便捷开发框架livery,帮助开发者架构企业级应用. 基于an-base仓库()与版本演化而来,针对此做了很多优化,当前优化后最新体积仅有586KB. :warning:注意 Livery版本1.2.x与1.1.x不完全...

    可穿戴、移动式皮电反应(GSR)测量系统(原理图、pcb、bom、代码)-电路方案

    器件通过Bluetooth:registered:低功耗无线接口与Android设备进行通信。 移动式皮电反应(GSR)系统设计框图: 移动式皮电反应测量原理图部分截图: 移动式皮电反应测量源码截图: 说明: 附件内容提供测试结果、硬件文件和...

    跳坑《一百七十六》蓝牙API使用指南

    基础库版本 1.1.0 开始支持,低版本需做兼容处理 iOS 微信客户端 6.5.6 版本开始支持,Android 客户端目前已经支持 由于系统的问题,目前仅在 mac 版的开发工具上支持蓝牙调试 tip: Mac系统可能无法获取advertisData...

    安卓蓝牙串口助手源码

    此源码为AndroidStudio3.0版,可以在此基础上开发其他蓝牙串口助手功能。

    TestWearApp.rar

    可用于手机与手机连接收发消息,也可用于手机与腕表之间收发消息,从蓝牙4.0开始包含两个蓝牙芯片模块:传统/经典蓝牙模块(Classic Bluetooth,简称BT 经典蓝牙是在之前的蓝牙1.0,1.2,2.0+EDR,2.1+EDR,3.0+EDR等基础...

    internalblue:适用于Broadcom和Cypress芯片的蓝牙实验框架

    在此基础上,我们开发了一个蓝牙实验框架,该框架能够修补固件。 这实现了各种功能,否则这些功能只有在完整的软件定义的无线电实现中才可能实现,例如在链路层上注入和监视数据包。 InternalBlue不仅在安全移动...

Global site tag (gtag.js) - Google Analytics