Android ble 蓝牙4.0 总结

本文介绍Android ble 蓝牙4.0,也就是说API level >= 18,且支持蓝牙4.0的手机才可以使用,如果手机系统版本API level < 18,也是用不了蓝牙4.0的哦。

首先发一下官方的demo,有兴趣的可以过去看看:http://developer.android.com/guide/topics/connectivity/bluetooth-le.html。android系统4.3以上,手机支持蓝牙4.0,具有搜索,配对,连接,发现服务及特征值,断开连接等功能,下载地址:http://download.csdn.net/detail/lqw770737185/8116019。

一、了解api及概念

1.1 BluetoothGatt

继承BluetoothProfile,通过BluetoothGatt可以连接设备(connect),发现服务(discoverServices),并把相应地属性返回到BluetoothGattCallback

1.2 BluetoothGattCharacteristic

相当于一个数据类型,它包括一个value和0~n个value的描述(BluetoothGattDescriptor)

1.3 BluetoothGattDescriptor

描述符,对Characteristic的描述,包括范围、计量单位等

1.4 BluetoothGattService

服务,Characteristic的集合。

1.5 BluetoothProfile

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

1.6 BluetoothManager

通过BluetoothManager来获取BluetoothAdapter

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

1.7 BluetoothAdapter

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

BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();

1.8 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.8.1:notification对应onCharacteristicChanged;

gatt.setCharacteristicNotification(characteristic, true);

1.8.2:readCharacteristic对应onCharacteristicRead;

gatt.readCharacteristic(characteristic);

1.8.3: writeCharacteristic对应onCharacteristicWrite;

gatt.wirteCharacteristic(mCurrentcharacteristic);

1.8.4:连接蓝牙或者断开蓝牙 对应 onConnectionStateChange;

1.8.5: readDescriptor对应onDescriptorRead;

1.8.6:writeDescriptor对应onDescriptorWrite;

gatt.writeDescriptor(descriptor);

1.8.7:readRemoteRssi对应onReadRemoteRssi;

gatt.readRemoteRssi()

1.8.8:executeReliableWrite对应onReliableWriteCompleted;

1.8.9:discoverServices对应onServicesDiscovered。

gatt.discoverServices()

1.9 BluetoothDevice

扫描后发现可连接的设备,获取已经连接的设备

二、开启蓝牙权限

<uses-permission android:name="android.permission.BLUETOOTH"/>

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

如果 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();

四、扫描设备,连接设备,获取设备信息 ,断开连接设备,自行查看官方demo,还是看demo比较清晰啊

时间: 2024-08-06 22:07:52

Android ble 蓝牙4.0 总结的相关文章

Android ble 蓝牙4.0 总结一

本文介绍Android ble 蓝牙4.0,也就是说API level >= 18,且支持蓝牙4.0的手机才可以使用,如果手机系统版本API level < 18,也是用不了蓝牙4.0的哦. 首先发一下官方的demo,有兴趣的可以过去看看:http://developer.android.com/guide/topics/connectivity/bluetooth-le.html.android系统4.3以上,手机支持蓝牙4.0,具有搜索,配对,连接,发现服务及特征值,断开连接等功能,下载地

Android BLE蓝牙详细解读

代码地址如下:http://www.demodashi.com/demo/15062.html 随着物联网时代的到来,越来越多的智能硬件设备开始流行起来,比如智能手环.心率检测仪.以及各式各样的智能家具和玩具类产品.安卓4.3(API 18)为BLE的核心功能提供平台支持和API,App可以利用它来发现设备.查询服务和读写特性.相比传统的蓝牙,BLE更显著的特点是低功耗.本文主要讲解Android低功耗蓝牙的api使用以及蓝牙扫描.连接.发送数据.接收数据等一系列操作,并主要介绍本人封装的Ble

android ble蓝牙开发略解

Android 蓝牙4.0开发 1.  权限和相关属性 “android:required="true"表示apk只有在具有bluetooth_le属性的系统里运行,这个4.3之前android系统没有 <uses-featureandroid:name="android.hardware.bluetooth_le"android:required="true"/> <uses-permissionandroid:name=&q

Android ble蓝牙使用注意

以下均为自己在Android ble开发项目中遇到的问题 1.尽量不要在BluetoothGattCallback里面的回调函数中执行读写通知操作,最多一个,因为例如在onServicesDiscovered回调函数中只会传一个写操作,不管里面有多少个,而通知如setCharacteristicNotification(characterist,true)也有写操作,所以如果需要同时执行多步征特操作时,不能在回调函数中执行,不然只会执行第一步特征操作. 2.读写通知都是异步操作,但是一般必须一步

Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现

Android4.3 规范了BLE的API,但是直到目前的4.4,还有些功能不完善. 在BLE协议中,有两个角色,周边(Periphery)和中央(Central):周边是数据提供者,中央是数据使用/处理者:在iOS SDK里面,可以把一个iOS设备作为一个周边,也可以作为一个中央:但是在Android SDK里面,直到目前最新的Android4.4.2,Android手机只能作为中央来使用和处理数据:那数据从哪儿来?从BLE设备来,现在的很多可穿戴设备都是用BLE来提供数据的. 一个中央可以同

Android BLE 蓝牙编程(二)

大家中秋快乐啊--哈哈,今天继续工程项目吧! 上篇我们已经实现了蓝牙设备的扫描,本篇我们来通过list展示扫描到的设备并 实现点击连接. 先贴出上篇的完整的MainActivity的方法: package com.wbnq.shouhuan; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothManager;

Android BLE 蓝牙编程(三)

上节我们已经可以连接上蓝牙设备了. 本节我们就要获取手环的电池电量和计步啦. 在介绍这个之前我们需要先了解下什么是 服务 什么是 UUID 我们记得上节中我们item监听事件的回调的返回值是BluetoothGatt 类型的,还记得么?嘿嘿. 返回的bluetoothgatt中包含一个或多个BluetoothGattService(服务) 每个service包含一个或多个characteristic(特征值) 每个特征值包含一个value 和多个 descriptor(注意看啊!是一个value

Android BLE蓝牙开发-读写数据 获取UUID

https://www.jianshu.com/p/3711cfbf7128 一个ble蓝牙设备有多个包括多个Profile 一个Profile中有多个服务Service(通过服务的uuid找到对应的Service) 一个Service中有多个特征Characteristic(通过特征的uuid找到对应的Characteristic) 一个Characteristic中包括一个value和多个Descriptor(通过Descriptor的uuid找到对应的Descriptor) 另外注意,连接

BLE蓝牙4.0串口调试助手

支持HEX和文本发送接收,仅175K  无广告 无高级权限 APK下载地址:http://pan.baidu.com/s/1gdk20dP IOS版敬请期待... 版权声明:本文为博主原创文章,未经博主允许不得转载.