Android蓝牙使用(一)

来源:互联网 发布:淘宝米兰密码是正品吗 编辑:程序博客网 时间:2024/06/06 20:03

蓝牙使用(二)http://blog.csdn.net/duo_shine/article/details/70259928
蓝牙固件升级:http://blog.csdn.net/duo_shine/article/details/73250034
封装的一个BleDemo:非常简单几行代码即可调用,如何使用在github写的也很详细:https://github.com/duoshine/BlueToothConnect-master
能get到的:


  1. 认识蓝牙
  2. 了解蓝牙4.0
  3. 如何开启蓝牙
  4. 如何扫描周边蓝牙设备
  5. .如何连接蓝牙设备
    6 手机端如何与ble终端通信
    最后会放上自己简单封装的一个demo,将上述功能都实现了的,后期有时间会写一篇关于OTA的,目前只支持ble,双模蓝牙设备亲测连接不上

一,认识蓝牙
lan ya…


二,了解蓝牙4.0(Android4.3,蓝牙4.0)
1.优点
安卓的 BLE 标准在 2013 年 7 月 24 日发布,一般搭配Android 4.3 及以上系统的手机都是支持牙 4.0(BLE)的,为了实现极低的功耗,BLE 协议设计为:在不必要射频的时候,彻底将空中射频关断(可以在需要的时候快速建立连接进行控制操作)。与传统蓝牙 BR\EDR 相比,BLE 有三大特性,从而实现低功耗:缩短无线开启间、快速建立连接、降低收发峰值功耗(具体由芯片决定)。
2.设备角色
在 BLE 协议中,有两个角色,周边/外围( Periphery )和中央( Central) ;周边是数据提供者,中央是数据使用/处理者;在 Android SDK 里面,一个中央可以同时连接多个周边,但是一个周边某一时刻只能连接一个中央 (Central)BluetoothGattServer 作为周边来提供数据;BluetoothGattServerCallback 返回周边的状态。BluetoothGatt 作为中央来使用和处理数据;BluetoothGattCallback 返回中央的状态和周边提供的数据。一个 Ble 设备某一时刻只能扮演一种角色。 每一个周边 BluetoothGattServer, 包含多个服务 Service, 每一个 Service 包含多个特征 Characteristic ,每一个 Characteristic 又包含多个Descriptor 。
3.了解api
BluetoothManager:
通过BluetoothManager获取BluetoothAdapter
BluetoothAdapter:
Android中只有一个BluetoothAdapter,通过BluetoothAdapter可以打开蓝牙,扫描,停止扫描等
BluetoothGattServer:
作为周边来提供数据,Characteristic的集合。
BluetoothGattServerCallback:
返回周边的状态
BluetoothGattCharacteristic:
相当于一个数据类型,它包括一个value和0~n个value的描述(BluetoothGattDescriptor)
BluetoothGattDescriptor:
描述符,对Characteristic的描述,包括范围、计量单位等
BluetoothGatt:
作为中央来使用和处理数据,通过BluetoothGatt可以连接设备(connect),发现服务(discoverServices),并把相应地属性返回到BluetoothGattCallback
BluetoothGattCallback:
已经连接上设备,对设备的某些操作后返回的结果。返回中央的状态和周边提供的数据


三,如何开启蓝牙

//权限:  <!-- 管理蓝牙设备的权限 -->    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>    <!-- 使用蓝牙设备的权限 -->    <uses-permission android:name="android.permission.BLUETOOTH"/>//先判断设备是否支持bleif (!getPackageManager().hasSystemFeature(            PackageManager.FEATURE_BLUETOOTH_LE)) {            ToastUtil.show(context, "该设备不支持BLE!");            finish();        }        //拿到BluetoothAdapter    if (mBluetoothManager == null) {            mBluetoothManager = (BluetoothManager) mContext                    .getSystemService(Context.BLUETOOTH_SERVICE);            if (mBluetoothManager == null) {                Log.e(TAG, "Unable to initialize BluetoothManager.");            }        }    mBluetoothAdapter = mBluetoothManager.getAdapter();        if (mBluetoothAdapter == null) {            Log.e(TAG, "Unable to obtain a BluetoothAdapter.");        }        //直接开启蓝牙        mBluetoothAdapter.enable();

开启的源码

  public boolean enable() {        if (isEnabled() == true){            if (DBG) Log.d(TAG, "enable(): BT is already enabled..!");            return true;        }        try {            return mManagerService.enable();        } catch (RemoteException e) {Log.e(TAG, "", e);}        return false;    }

如果为false你需要检查一下可能是权限没有添加
四,如何扫描周边蓝牙设备
挺麻烦的步骤,一步一步来吧
1.开启蓝牙之后需要开始扫描附近的设备,扫描最好是定时的,比如扫描五秒后自动停止扫描,这一过程是非常消耗性能的,

//扫描之前要先取消当前的设备发现过程

mBluetoothAdapter.cancelDiscovery();

//停止扫描mBluetoothAdapter.stopLeScan(mLeScanCallback);//开始扫描 mBluetoothAdapter.startLeScan(mLeScanCallback);

mLeScanCallback是接收扫描结果的接口,需要实现这个方法接收扫描结果

//设备搜索完毕的回调 @Override    public void onLeScan(final BluetoothDevice device, int rssi,byte[] scanRecord) {      //该方法回调多次  将搜索到的蓝牙设备展示即可 有mac地址和蓝牙Name        device.getAddress();        device.getName();    }

注意mLeScanCallback必须是同一个
到这里应该就是展示附近的蓝牙设备了,如果你没有展示出附近的蓝牙设备,可能是Android版本的问题,6.0需要加入定位权限

http://blog.csdn.net/duo_shine/article/details/70045896

0 0