【android 蓝牙开发——BLE(低功耗)蓝牙】
16lz
2021-01-23
本篇博客,接着上一篇博客 【android 蓝牙开发——传统蓝牙】,讲一下低功耗蓝牙的开发。
着重说明一下:在大于等于4.3 和 小于5.0 之间的android手机系统版本,只支持作为中心设备角色,也就是说如果你的android手机是4.4的版本,你开发的软件,那就只能搜索到一些外围设备,例如:智能手环一类的东西,而不能做为外围设备被其他中心设备搜索到;在 5.0以及5.0 之后,既可以作为中心设备,也可以作为外围设备,这个在以后博客中继续讲解。
同样也都是一样的套路,在下面的 相关博客链接 中,人家都将的很清楚啦。也不再这里在重新说一边了。只把一些需要注意的地方说一下,希望大家和自己以后能避免。
大概步骤:
- 打开蓝牙
- 扫描设备
- 连接设备
- 写入数据,控制设备
- 读取设备返回的数据
- 断开设备
- 关闭蓝牙
注意事项:
一:
//搜索的操作最好放在Activity的onResume里面或者服务里面,我有发现放在onCreate有时响应不及时,而且人家google Daemon 也是放在onResume里面的。 private void scanLeDevice(final boolean enable) { if (enable) { mScanning = true; Log.i(TAG, "scanLeDevice: 开始搜索"); mBluetoothAdapter.startLeScan(mLeScanCallback); //开始搜索 mHandler.postDelayed(new Runnable() { @Override public void run() { mScanning = false; mBluetoothAdapter.stopLeScan(mLeScanCallback); Log.i(TAG, "scanLeDevice: 搜索停止"); deviceAdapter.notifyDataSetChanged(); } }, SCAN_PERIOD); //10秒后停止搜索 } else { mScanning = false; mBluetoothAdapter.stopLeScan(mLeScanCallback);//停止搜索 } }
二:这个方法很关键
private void displayGattServices(List gattServices) { if (gattServices == null) return; for (BluetoothGattService gattService : gattServices) { // 遍历出gattServices里面的所有服务 //通过遍历可以在这里拿到自己想要的那个服务的uuid List gattCharacteristics = gattServices.getCharacteristics(); for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { // 遍历每条服务里的所有Characteristic if (gattCharacteristic.getUuid().toString().equalsIgnoreCase(需要通信的UUID)) { // 有哪些UUID,每个UUID有什么属性及作用,一般硬件工程师都会给相应的文档。我们程序也可以读取其属性判断其属性。 // 此处可以可根据UUID的类型对设备进行读操作,写操作,设置notification等操作 // BluetoothGattCharacteristic gattNoticCharacteristic 假设是可设置通知的Characteristic // BluetoothGattCharacteristic gattWriteCharacteristic 假设是可读的Characteristic // BluetoothGattCharacteristic gattReadCharacteristic 假设是可写的Characteristic } } } }
三:服务的绑定处理,为什么是这样,你要查阅一下服务的绑定知识。
private final ServiceConnection mServiceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder service) { Log.i(TAG, "onServiceConnected: mDeviceAddress=" + mDeviceAddress); isBond = true; mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService(); if (!mBluetoothLeService.initialize()) { Log.e(TAG, "Unable to initialize Bluetooth"); finish(); } mBluetoothLeService.connect(mDeviceAddress); } @Override public void onServiceDisconnected(ComponentName componentName) { Log.i(TAG, "onServiceDisconnected: "); mBluetoothLeService = null; isBond = false; } };
在onDestroy() 或者 onStop() 要做一些解绑处理:
@Override protected void onDestroy() { super.onDestroy(); if (mGattUpdateReceiver != null) { unregisterReceiver(mGattUpdateReceiver);//解绑广播 } if (mBluetoothLeService != null) { mBluetoothLeService.disconnect(); //断开连接 } if (isBond) { isBond = false; unbindService(mServiceConnection);//解绑服务 } }
源码下载
源码说明:后续会补上。
相关博客:
google 官方文件 比较具有参考价值
google 官方 Daemon android-BluetoothLeGatt
Android BLE蓝牙4.0开发详解
http://www.jianshu.com/c/f1805c68192b
Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现
Android:BLE智能硬件开发详解
更多相关文章
- 关于蓝牙设备之间共享网络的问题(android4.2)
- Android设备检测
- Android蓝牙BLE的详细讲解
- Android判断设备是手机还是平板
- Android设备的显示信息
- Android中获取设备的IP
- Make ADB To Support Android Devices(如何使ADB在Linux下支持And
- android 如何遍历Cursor
- android蓝牙开发入门到精通1---蓝牙基础