Android蓝牙篇:(一)基础篇

一、获取本地蓝牙设备

首先来看一下android API给出的获取方法:

To get a BluetoothAdapter representing the local Bluetooth adapter, 
when running on JELLY_BEAN_MR1 and below, call the static getDefaultAdapter method; 
when running on JELLY_BEAN_MR2 and higher, retrieve it through "android.content.Context.getSystemService" with "android.content.Context.BLUETOOTH_SERVICE".
Fundamentally, this is your starting point for all Bluetooth actions.

即为了使用蓝牙功能,JELLY_BEAN_MR1以下的版本使用如下方式获得BluetoothAdapter单例对象。

 BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();


JELLY_BEAN_MR2以后的版本,使用如下方式来调用蓝牙API。

     BluetoothManager bluetoothManager =
                (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
     BluetoothAdapter mAdpter=bluetoothManager.getAdapter(); 


这两种方式都是可以使用的,具体的区别后续会进行添加。(区别?


二、获取已配对的蓝牙适配器对象

首先来看一下android API给出的获取方法:

Once you have the local adapter, you can get a set of BluetoothDevice objects representing all paired devices with getBondedDevices(); 
即通过getBondedDevices() 获得已配对设备,返回(绑定(配对)到本地蓝牙的)BluetoothDevice对象集合;
使用示例:

Set<BluetoothDevice> devices = adapter.getBondedDevices();
     if(devices.size()>0)    
     {
         //用迭代
         for(Iterator iterator = devices.iterator();iterator.hasNext();)
         {
           //得到BluetoothDevice对象,也就是说得到配对的蓝牙适配器
           BluetoothDevice device = (BluetoothDevice)iterator.next();
           //得到远程蓝牙设备的地址
           Log.d("mytag",device.getAddress());
         }     
     }



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张之海

若有帮助,客官打赏一分吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值