Android 蓝牙4.0 BLE (onServicesDiscovered 返回 status 是 129,133时)
Android ble (Bluetooth Low Energy) 蓝牙4.0,也就是说android 4.3+, API level >= 18,且支持蓝牙4.0的手机才可以使用。
BLE是蓝牙4.0的核心Profile,主打功能是快速搜索,快速连接,无需配对,超低功耗保持连接和传输数据,弱点是数据传输速率低,由于BLE的低功耗特点,因此普遍用于穿戴设备。
官方demo:http://developer.android.com/guide/topics/connectivity/bluetooth-le.html
官方demo(csdn下载,感谢分享的人吧):http://download.csdn.net/detail/lqw770737185/8116019
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
//不支持ble
finish();
}
BluetoothAdapter.LeScanCallback接口,BLE设备的搜索结果将通过这个callback返回 private ExecutorService mExecutor;
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
// 不能做太多事情,特别周围ble设备多的时候。容易出现错误或者ANR,需要把结果处理放到另外的线程去。
if(mExecutor == null) {
mExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);
}
new ScanProcessor().executeOnExecutor(mExecutor, new ScanData(device, rssi, scanRecord));
}
};
ScanData:
private class ScanData {
public ScanData(BluetoothDevice device, int rssi, byte[] scanRecord) {
this.device = device;
this.rssi = rssi;
this.scanRecord = scanRecord;
}
int rssi;
BluetoothDevice device;
byte[] scanRecord;
}
扫描结果处理:
private class ScanProcessor extends AsyncTask<ScanData, Void, MingbikeBeacon> {
public ScanProcessor() {
}
@Override
protected Object doInBackground(ScanData... params) {
if (params == null || params.length == 0)
return null;
ScanData scanData = params[0];
// todo scanData
return new Object();
}
@Override
protected void onPostExecute(Object obj) {
// todo obj
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
(2)关闭BLE设备的搜索
mBluetoothAdapter.stopLeScan(mLeScanCallback);
(3)开启BLE设备的搜索
mBluetoothAdapter.startLeScan(mLeScanCallback);
*************注意:搜索时,你只能搜索传统蓝牙设备或者BLE设备,两者完全独立,不可同时被搜索。
(4)使用BluetoothGatt 来连接设备
private BluetoothGatt mBluetoothGatt;
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); // 蓝牙mac地址
if (device == null) {
Log.w(TAG, "Device not found. Unable to connect.");
return false;
}
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
BluetoothDevice.connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback);
参数autoConnect,如果为 true 的话,系统就会发起一个后台连接,等到系统发现了一个设备,就会自动连上,通常这个过程是非常慢的。为 false 的话,就会直接连接,通常会比较快。同样,BluetoothGatt.connect()只能发起一个后台连接,不是直接连接。所以这个地方需要小心。
(5)使用BluetoothGattCallback 来跟设备进行通信
private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt,
int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
9 if (newState == BluetoothProfile.STATE_CONNECTED) {
setState(State.CONNECTED);
gatt.discoverServices();
} else {
setState(State.IDLE);
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt,
int status) {
if(status == BluetoothGatt.GATT_SUCCESS) {
Log.v(TAG, "onServicesDiscovered: " + status);
}
}
}
连接时会走这个方法onConnectionStateChange,传过来的新状态是连接状态,这时在这个方法中调用一下这句:mBluetoothGatt.discoverServices(),
mBluetoothGatt是连接完成时的对象,调用这句后,会走回调函数的onServicesDiscovered方法。在这个方法中去获取设备的一些服务,蓝牙通道,然后通过这些通道去发送数据给外设。
有些设备,在 onServicesDiscovered 回调中,返回的 status 是 129,133时,在关闭,重新打开蓝牙,无法解决问题时,建议更换设备,这些问题大多是设备底层gatt 服务异常,重新连接,进行discoverServices();
// 出现129,133时。关闭蓝牙
mBluetoothAdapter.disable();
// 关闭蓝牙后,延时1s,重新开启蓝牙
mBluetoothAdapter.enable();
在蓝牙设备中, 其包含有多个BluetoothGattService, 而每个BluetoothGattService中又包含有多个BluetoothGattCharacteristic。
当onServicesDiscovered()回调的 status == BluetoothGatt.GATT_SUCCESS, 可以进行获取service。
(1)获取到设备中的服务列表 mBluetoothGatt.getServices(); 或者通过uuid 来获取某一个服务:
public List<BluetoothGattService> getSupportedGattServices() {
if (mBluetoothGatt == null)
return null;
return mBluetoothGatt.getServices();
// 或者通过uuid来获取某一个服务 mBluetoothGatt.getServices(uuid);
}
(2)通过Gatt这个对像,就是蓝牙连接完成后获取到的对象,通过这个对象设置好指定的通道向设备中写入和读取数据。
在onServicesDiscovered(BluetoothGatt gatt, int status) 回调中, 获取服务对象,获取读取、写入、描述的特征。
// 获取指定uuid的服务
BluetoothGattService service = gatt.getService(BluetoothUUID.bleServerUUID);
// 获取读取特征
BluetoothGattCharacteristic readCharacteristic = service.getCharacteristic(BluetoothUUID.readDataUUID);
// 获取写入特征
writeCharacteristic = service.getCharacteristic(BluetoothUUID.writeDataUUID);
gatt.setCharacteristicNotification(readCharacteristic, true);
// 获取描述特征
BluetoothGattDescriptor descriptor = readCharacteristic.getDescriptor(BluetoothUUID.CLIENT_CHARACTERISTIC_CONFIG);
// 要看外围设备的设置
if (isNotify) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
} else {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
}
gatt.writeDescriptor(descriptor);
服务中, 也可以通过调用 gattService.getCharacteristics():获得Characteristic 集合.
在 Characteristic中, 可以通过 mBluetoothGatt.readCharacteristic(characteristic); 来读取其里面的数据, 其结果在mGattCallback 回调函数中获取.
写如数据:
mCurrentcharacteristic.setValue(data);
mBluetoothGatt.wirteCharacteristic(mCurrentcharacteristic);
public void wirteCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.writeCharacteristic(characteristic);
}
public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.readCharacteristic(characteristic);
}
当外围设备,调用:
BluetoothGattServer.class
notifyCharacteristicChanged(BluetoothDevice device,
BluetoothGattCharacteristic characteristic, boolean confirm);
就会触发中心设备的onCharractersticChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) 方法回调:
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
byte[] values = characteristic.getValue();
// todo values
}
注意:BluetoothGattCharacteristic这个是指定的通道,蓝牙服务:
BluetoothGattCharacteristic characteristic = null;
characteristic = mGattCharacteristics.get(4).get(4);
这两个数字就是从指定的服务中找到你要发送数据的那个服务。
如果要进行多个连接,每次连接完成后可以将BluetoothGatt的对象放到一个list里面,获取到的服务也放到一个List里面,然后发送数据的时候调用不同的Gatt发送不同的通道数据即可。
参考:
Android 蓝牙4.0 BLE (onServicesDiscovered 返回 status 是 129,133时)的更多相关文章
- Android 蓝牙4.0 BLE
Android ble (Bluetooth Low Energy) 蓝牙4.0,也就是说API level >= 18,且支持蓝牙4.0的手机才可以使用. BLE是蓝牙4.0的核心Profil ...
- android蓝牙4.0(BLE)开发之ibeacon初步
一个april beacon里携带的信息如下 ? 1 <code class=" hljs ">0201061AFF4C0002159069BDB88C11416BAC ...
- ym——物联网入口之中的一个Android蓝牙4.0
转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 假设还有同学不知道蓝牙4.0能够做什么请查看Android+蓝牙 4.0 将带来什么? ...
- android 蓝牙4.0 开发介绍
最近一直在研究一个蓝牙功能 由于本人是菜鸟 学起来比较忙 一直搞了好久才弄懂 , 网上对蓝牙4.0也就是几个个dome 抄来抄去,全是英文注解 , 对英语不好的朋友来说 真是硬伤 , 一些没必要的描 ...
- Android项目实战(三十四):蓝牙4.0 BLE 多设备连接
最近项目有个需求,手机设备连接多个蓝牙4.0 设备 并获取这些设备的数据. 查询了很多资料终于实现,现进行总结. ------------------------------------------- ...
- Android 蓝牙4.0的连接和通讯
1.加入权限 <uses-sdk android:minSdkVersion=" android:targetSdkVersion="/> <uses-featu ...
- android 蓝牙4.0多通道
很久没记录东西了,前段时间研究了一哈android4.0控制多个外设的情况,注意,需要使用android版本4.3以上,蓝牙4.0及以上. 我这里使用的控制蓝牙灯泡,使用android4.3的手机,手 ...
- IOS学习之蓝牙4.0 BLE
IOS学习也一段时间了,该上点干货了.前段时间研究了一下IOS蓝牙通讯相关的东西,把研究的一个成果给大家分享一下. 一 项目背景 简单介绍一下做的东西,设备是一个金融刷卡器,通过蓝牙与iphone手机 ...
- 蓝牙4.0 BLE入门
在BLE协议中有两个角色,一个是周边(Periphery),另外一个是中央(Central).一个中央可以同时连接多个周边,但一个周边某一时刻只能连接一个中央.但是不管periphery还是centr ...
随机推荐
- settings.py常见配置项
settings.py常见配置项 1. 配置Django_Admin依照中文界面显示 LANGUAGE_CODE = 'zh-hans' 2. 数据库配置(默认使用sqlite3) 1 .默认使用的s ...
- openresty用naxsi防xss、SQL注入
下载naxsi wget https://github.com/nbs-system/naxsi/archive/untagged-afabfc163946baa8036f.tar.gz tar zx ...
- sed 详解【转】
原文地址:http://www.cnblogs.com/sparkdev/archive/2017/07/10/7138073.html 基本命令格式 sed [常用选项] 命令文本 输入 常用选项 ...
- ExceptionLess的webAPI调用
引用 <package id="bootstrap" version="3.0.0" targetFramework="net461" ...
- CDOJ1927 爱吃瓜的伊卡洛斯(2) 【并查集】启发式合并+set
伊卡洛斯很爱吃西瓜.一次,他来到一个西瓜摊旁,发现水果摊有N个西瓜,西瓜有红色.黄色.绿色.蓝色……等等数不清的颜色. 伊卡洛斯很想知道知道一些信息,便于老板交谈了起来. 当老板的话的第一个字符为”A ...
- linux inotify 文件变化检测
用webstorm开发angular项目的时候,改写文件后发现热更新有时候会失效,从而不得不重新运行下项目,然而这浪费了好多时间,google一番后,解决办法如下 echo fs.inotify.ma ...
- [转]Tor Browser在国内Windows平台下的超详细教程
https://cloudfra.com/tor-browser-windows.html 下载与安装 首先,你必须身处科学式网络(实在怕网站再出问题),接着就可以点击打开Tor Browser官网, ...
- Java程序编译和运行过程之 一个对象的生命之旅(类加载和类加载器)
Java程序从创建到运行要经过两个大步骤 1:源文件(.java)由编译器编译成字节码ByteCode(.class) 2:字节码由Java虚拟机解释并运行 源文件编译成字节码,主要分成两个部分: 1 ...
- mysql-索引-日志
索引:基于元数据之上的在某个字段或多个字段取出来,索引是加速读操作的,但对写操作时有副作用的 BTree 索引:抽取出来重新排序,是左前缀索引每一个叶子结点到根结点的距离相同: 哈希索引:基于键查找值 ...
- Codeforces 639D Bear and Contribution
Bear and Contribution 对于对于5余数为, 0, 1, 2, 3, 4的分别处理一次, 用优先队列贪心. #include<bits/stdc++.h> #define ...