600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > android BLE蓝牙详细讲解(一)

android BLE蓝牙详细讲解(一)

时间:2020-06-03 07:27:33

相关推荐

android  BLE蓝牙详细讲解(一)

本文主要讲解Android低功耗蓝牙的api使用以及蓝牙扫描、连接、发送数据、接收数据等一系列操作,本篇结尾有本人封装的BleLib蓝牙库,非常适合蓝牙初学者使用,只需要一行代码注入就OK了,而且用法也极其简单,我会在第二篇中专门讲解一下BleLib库的使用。

在BLE协议中,有两个角色,周边(Periphery)和中央(Central);周边是数据提供者,中央是数据使用/处理者,一个中央可以同时连接多个周边,但是一个周边某一时刻只能连接一个中央。

首先使用蓝牙就不得不说BluetoothGatt和BluetoothGattCallback这两个类,该类继承自BluetoothProfile,BluetoothGatt作为中央来使用和处理数据,通过BluetoothGatt可以连接设备(connect),发现服务(discoverServices),并把相应地属性返回到BluetoothGattCallback,BluetoothGattCallback返回中央的状态和周边提供的数据。

我们蓝牙操作的主要目的就是为了拿到中央BluetoothGatt这个对象,进而进行接下来的所有一系列操作,如下:

1.先拿到BluetoothManager:bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

2.再拿到BluetoothAdapt:btAdapter = bluetoothManager.getAdapter();

3.开始扫描:btAdapter.startLeScan( BluetoothAdapter.LeScanCallback);

4.从LeScanCallback中得到BluetoothDevice:public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {…..}

5.用BluetoothDevice得到BluetoothGatt:gatt = device.connectGatt(this, true, gattCallback);

这时总算拿到中央BluetoothGatt了,它有很多的方法,调用这些方法,你就可以通过BluetoothGattCallback和周边BluetoothGattServer交互了。

下面讲解一下主要类的大致理解:

BluetoothProfile: 一个通用的规范,按照这个规范来收发数据。

BluetoothManager:通过BluetoothManager来获取BluetoothAdapter

如:BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

BluetoothAdapter:一个Android系统只有一个BluetoothAdapter ,通过BluetoothManager 获取

BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();

BluetoothGattDescriptor:可以看成是描述符,对Characteristic的描述,包括范围、计量单位等。

BluetoothGattService:服务,Characteristic的集合。

BluetoothGattCallback:已经连接上设备,对设备的某些操作后返回的结果。这里必须提醒下,已经连接上设备后的才可以返回,没有返回的认真看看有没有连接上设备。

private BluetoothGattCallback GattCallback = new BluetoothGattCallback() {

// 这里有9个要实现的方法,看情况要实现那些,用到那些就实现那些

public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState){};

public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status){};

};

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

BluetoothGatt gatt = device.connectGatt(this, false, mGattCallback);

蓝牙交互的主要对应关系:

1、notification对应onCharacteristicChanged;

gatt.setCharacteristicNotification(characteristic, true);该方法一般是在发现服务后,进行设置的,设置该方法的目的是让硬件在数据改变的时候,发送数据给app,app则通过onCharacteristicChanged方法回调给用户,从参数中可获取到回调回来的数据。

2、readCharacteristic对应onCharacteristicRead;

gatt.readCharacteristic(characteristic);

3、writeCharacteristic对应onCharacteristicWrite;

gatt.wirteCharacteristic(mCurrentcharacteristic);

4、连接蓝牙或者断开蓝牙 对应 onConnectionStateChange;

5、 readDescriptor对应onDescriptorRead;

6、writeDescriptor对应onDescriptorWrite;

gatt.writeDescriptor(descriptor);

7、readRemoteRssi对应onReadRemoteRssi;

gatt.readRemoteRssi()

8、executeReliableWrite对应onReliableWriteCompleted;

9、discoverServices对应onServicesDiscovered

gatt.discoverServices()

开启蓝牙所具备的权限:

如果 android.hardware.bluetooth_le设置为false,可以安装在不支持的设备上使用,判断是否支持蓝牙4.0用以下代码就可以了

如:if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {

Toast.makeText(this, “设备不支持蓝牙4.0”, Toast.LENGTH_SHORT).show();

finish();

}

对蓝牙的启动关闭操作:

1、利用系统默认开启蓝牙对话框

if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

}

2、后台打开蓝牙,不做任何提示,这个也可以用来自定义打开蓝牙对话框啦

mBluetoothAdapter.enable();

3、后台关闭蓝牙

mBluetoothAdapter.disable();

本文的api介绍:(blelibrary库)

1、iQppCallback和QppApi这个两个类封装了完整的读写数据,设置通知等操作 此demo中并未用到这两个接口,此列出方便以后调用

2、BleDevice类为蓝牙对象,其中可以设置蓝牙的基本属性,以及连接状态等(可以继承该类进行扩展)

3、BleConfig类中主要是放置一些静态值,如连接超时时长、扫描时长、服务及特征的uuid,以及验证硬件发送的广播包以便进行过滤扫描到的设备

4、BleLisenter包含了ble蓝牙操作的所有接口 如开始扫描、停止扫描、扫描到设备、获取到服务、读取硬件返回的数据、向硬件写入数据、设置通知、蓝牙连接改变、蓝牙连接出错(在四此处设置同时最多可连接多少设备)等回调

5、BluetoothLeService实现所有的上述回调方法

本文先粗略的讲解一下android中api的大致使用及含义。

下篇将重点讲解BleLib库的使用以及各方法的具体使用。

使用:见DEMO

demo下载地址:/liulei-0911/BleDemo

androidstudio依赖地址: compile ‘.superLei:blelibrary:1.0.0’

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。