ble4.2 支持一主都从,手机是主,然后锁是从,一个手机可以连接多个蓝牙,但是一个蓝牙只能被一个手机连接
如果是ble5.0,那么支持多对多
1. 权限
2.获取蓝牙适配器并打开蓝牙,开启蓝牙
3. 扫描蓝牙
4. 蓝牙设备的配对
5. 蓝牙通信
问题:
android6.0以后蓝牙权限问题:https://blog.csdn.net/tangxl2008008/article/details/51334604
蓝牙配对的时候两边都会同时产生一个密钥,确认密钥
蓝牙扫描Api区别:https://blog.csdn.net/panade/article/details/82620941
BluetoothAdapter.startDiscovery()//可以扫描经典蓝牙和ble蓝牙两种
BluetoothAdapter.startLeScan()//扫描低功耗蓝牙,在api21已经弃用,不过还是可以使用
BluetoothLeScanner.startScan()//新的ble扫描方法
==================================================================================================
BluetoothAdapter 标识基本本机蓝牙设备
扫描以后获取BluetoothDevice,标识扫描到的蓝牙设备
然后BluetoothDevice调用连接方法以后获取BluetoothGatt对象,连接蓝牙
连接成功以后调用 mGatt.discoverServices(); 发现服务
public void onServicesDiscovered(BluetoothGatt gatt, int status) {} 回调发现服务和特征值
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status){}
读取特征值,这里才是真的读取,必须要在onServicesDiscovered方法中调用readCharacteristic()
特征值: 可读、可写、可订阅
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {} 蓝牙发送psw2到app
ble->app
psw1: 12个字节 psw2:4个字节 psw1+psw2 = key 16个字节
发送鉴权帧: 用key加密 systemId(旧的用mac地址替代)
蓝牙锁收到以后发送psw3给App
使用Mac地址连接:final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceMac);
==========================================
入网: App 连接锁以后, 锁进入入网模式-》生成psw2发送给App,
App从服务获取psw1, 通过mac地址或者sn
psw1作用,用于解密psw2
App获取psw2以后请求服务器, psw2+mac+userId绑定蓝牙设备该用户
App回复Ack到锁, 锁提示入网成功
App和蓝牙鉴权:
App通过Mac地址连蓝牙设备
从服务器获取该蓝牙设备的psw2,psw1
App连接蓝牙设备以后,锁等待鉴权30S内,30S过了,断开超时
APP发送鉴权信息, psw1+psw2加密 systemId(旧版本加密mac地址发送给锁)
锁收到以后鉴权成功生成psw3, 发送psw3给app,
App 会ack
APP和蓝牙通信:
使用 psw1+psw3加密数据发送,
心跳包不用加密
App和蓝牙解绑:
锁进入用户管理模式,退出网络,
发送退网针,使用psw1+psw3加密数据,
锁回ACK
删除 psw2
psw1是死的存在服务器
每次绑定都需要生成psw2
每次连接都需要鉴权生成psw3
==========================================
OTA升级: 补丁升级
DFU升级: 刷机