蓝牙从诞生之初就作为手机功能模块的中的重要一分子,同时也是framework的重要一环,最近看了一些关于蓝牙相关的。今天就先来总结下
权限
<user-permission android: name=“android.permission.BLUETOOTH”
manifast文件中注册,当然6.0后会有一个动态注册的机制,意思就是即使你在清单文件中静态注册了还是不能满足它,他还要你在使用它的时候再去弄下(权限)>-<
其实也就是一句话
requestPermissions(new String[]{Manifest.permission.BLUETOOTH}, 1);动态权限就搞定了!!!
嘿嘿。。。。。。
这个API是新加入的,使用它时需要判断当前API的Level,否则它会 crash
蓝牙在目前的发展史可分为两部分:
1.android SDKversion 16 之前 我们称之为 “普通蓝牙”
2.android SDKversion 16 之后新的API诞生此后的蓝牙称之为“BLE” <BuleTooth Low Energy>
接下来介绍下介绍下普通的和低能耗的不同点
普通蓝牙 是使用设备的mac地址,然后通过Socket建立一个通道BuleToothServerScoket,然后去Scan周围的设备
BLE: 基于**GATT**协议栈工作的 在建立通道后会创建一个回调如下<GATT是什么鬼,看官莫急,且见下文分晓>
使用方法
/**
* 初始化蓝牙适配器
*/
public void initBluetoothBleLowUtils(Activity mActivity, Handler mHandler,boolean canReconntect) {
this.mActivity = mActivity;
this.mHandler=mHandler;
// 开始扫描后允许重连,防止意外断开连接
this.canReconntect = canReconntect;
BluetoothManager btManager = (BluetoothManager) mActivity.getSystemService(Context.BLUETOOTH_SERVICE);
bAdapter = btManager.getAdapter();
if(listBluetoothGatts == null) {
listBluetoothGatts = new HashMap<String,BluetoothGatt>();
}
}
public List<BluetoothBean> getListBluetoothBean()
{
return listBluetoothBean;
}
/**
* 开始扫描ble
*/
public void startLeScan(final BluetoothConnectActivity.BtScanResult btScanResult) {
MyAlert.openDialogLoading(mActivity, true);
// 初始化蓝牙扫描结果存储List
if(listBluetoothBean == null) {
listBluetoothBean = new ArrayList<>();
} else {
for(int i=0;i<listBluetoothBean.size();i++) {
if(!listBluetoothBean.get(i).isConnect()) {
listBluetoothBean.remove(i);
}
}
}
if(bAdapter != null) {
boolean result = bAdapter.startLeScan(leScanCallback);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// 设置扫描时间为3S
stopLenScan();
// 完成后将扫描结果返回
MyAlert.openDialogLoading(mActivity, false);
btScanResult.getBtScanResult(listBluetoothBean);
}
}, Constant.BLUETOOTH_SCAN_TIME);
}
}
/**
* 停止扫描
*/
private void stopLenScan()
{
if(bAdapter != null)
{
bAdapter.stopLeScan(leScanCallback);
LogUtils.instance().logI("LE扫描停止...");
}
}
/**
*发起连接(供外部调用)
* @param device:目标设备
*/
public void requestConnect(BluetoothDevice device, BtConnectCallback callbackBtConnect, Boolean isFirstCon)
{
this.callbackBtConnect = callbackBtConnect;
if(isFirstCon){
disconnectAll();
}
BluetoothGatt gatt = device.connectGatt(mActivity, true, mGattCallback);
// 为了防止重复的gatt,连接成功先检查是否有重复的,有则断开
BluetoothGatt last = listBluetoothGatts.remove(device.getAddress());
if(last != null) {
last.disconnect();
last.close();
}
// 添加当前gatt
listBluetoothGatts.put(device.getAddress(), gatt);
}
/**
* 断开连接
* @param address
*/
public void disconnect(String address,boolean isReapConnect) {
if (null != listBluetoothGatts && listBluetoothGatts.containsKey(address)) {
BluetoothGatt gatt = listBluetoothGatts.remove(address);
if (gatt != null) {
gatt.disconnect();
gatt.close();
}
for(int i=0;i<listBluetoothBean.size();i++) {
if(listBluetoothBean.get(i).getBtAddress().equals(address)) {
listBluetoothBean.remove(i);
}
}
}
if(isReapConnect){
Constant.BLUETOOTH_CONNECT_STATE = false;
EventBus.getDefault().post(Constant.BLUETOOTH_STATE_CHANGE);
}else{
Message message=mHandler.obtainMessage(1,listBluetoothBean);
mHandler.sendMessage(message);
}
}
看完使用下面看看官方说明
在这里大概说下GATT是什么吧,如果要了解更过底下有gatt,笔者了解gatt原文
GATT generic Attributes的缩写,中文是通用属性,是低功耗蓝牙设备之间进行通信的协议。
GATT定义了一种多层的数据结构,已连接的低功耗蓝牙设备用它来进行通信。
GATT基于ATT。see Bluetooth Core System Architecture for block diagram and explanations)
GATT定义的多层数据结构简要概括起来就是服务(service)可以包含多个特征(characteristic),每个特征包含属性 (properties)和值(value),还可以包含多个描述(descriptor)。它形象的结构如下图:
GATT原文:https://blog.csdn.net/ohyeahhhh/article/details/52175596
由于本人也是在学习探索阶段,能力有险不足之处请各位看官指正!!