学低功率蓝牙功率等级划分开发技术难吗?及琪教育androidble课程怎么样?

如果你无法观看此视频,请先确认是否安装了Flash Player安装
Android BLE 开发入门
难度:高级
所属分类:
你的学习进度:已完成% 
Android BLE 开发系列课程
组合价:69K币
原价:69K币
适合人群:
适合有一定Android开发基础的人员,想学习Android BLE开发但又无任何蓝牙技术基础。
课程描述:
近年来,智能穿戴设备和移动互联网、物联网火爆兴起,正在改变人们的生活,蓝牙低功耗技术(BLE)作为其中非常重要的传输手段,扮演着重要角色。作为Android开发中的垂直领域,并且Android蓝牙开发有着很多不为人知的坑和机型兼容性问题,常常让人头疼不已,因此真正精通Android BLE开发的人员屈指可数。本系列课程目的就是在总结自身多年开发经验基础上,给大家分享Android BLE开发的全貌。本节课是第一讲,目标是带领大家入门,了解什么是BLE,有什么特点,在BLE操作前有什么前提,通信流程是什么,如何进行BLE扫描和连接。学习目标:1.理解BLE概念 ;2.掌握BLE通信流程 ;3.掌握Android BLE扫描设备 ;4.掌握Android BLE蓝牙连接 。学习建议:1.先熟悉基于 Android Studio 的 Android 开发 ;2.熟悉回调函数的概念 。下载资料:有
试题加载中...
重庆大学本硕,先于世界500强ALU任职,高级工程师和全球技术支持,2次创业经历,目前于一家移动互联网医疗公司任CTO,爱好互联网技术,尤其热衷于新技术开发。
2947人参加该课程
(C)2017 北京课工场教育科技有限公司 版权所有
京公网安备90号
加群找伙伴问大牛> 博客详情
摘要: Android4.3以上加入了低功耗蓝牙,可以大大节省设备功耗。
& &低功耗蓝牙包括的术语及概念:
如上图所示,使用低功耗蓝牙可以包括多个Profile,一个Profile中有多个Service,一个Service中有多个Characteristic,一个Characteristic中包括一个value和多个Descriptor。
Android中进行蓝牙开发需要使用到的类的执行过程是:
1、使用BluetoothAdapter.startLeScan来扫描低功耗蓝牙设备
2、在扫描到设备的回调函数中会得到BluetoothDevice对象,并使用BluetoothAdapter.stopLeScan停止扫描
3、使用BluetoothDevice.connectGatt来获取到BluetoothGatt对象
4、执行BluetoothGatt.discoverServices,这个方法是异步操作,在回调函数onServicesDiscovered中得到status,通过判断status是否等于BluetoothGatt.GATT_SUCCESS来判断查找Service是否成功
5、如果成功了,则通过BluetoothGatt.getService来获取BluetoothGattService
6、接着通过BluetoothGattService.getCharacteristic获取BluetoothGattCharacteristic
7、然后通过BluetoothGattCharacteristic.getDescriptor获取BluetoothGattDescriptor
Android Bluetooth源码静态类图如下:
使用低功耗蓝牙需要用到的权限:
&uses-permission&android:name="android.permission.BLUETOOTH"/&
&uses-permission&android:name="android.permission.BLUETOOTH_ADMIN"/&
下面介绍怎样使用BLE:
1、准备BLE
& & 1)获取BluetoothAdapter
& & &BluetoothAdapter是从系统服务获取到的,全系统就一个。
//&Initializes&Bluetooth&adapter.
final&BluetoothManager&bluetoothManager&=
&&&&&&&&(BluetoothManager)&getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter&=&bluetoothManager.getAdapter();
& & 2)检测蓝牙是否打开
& & 如果蓝牙未打开,系统会自动打开,会弹出系统框展示打开蓝牙。
private&BluetoothAdapter&mBluetoothA
//&Ensures&Bluetooth&is&available&on&the&device&and&it&is&enabled.&If&not,
//&displays&a&dialog&requesting&user&permission&to&enable&Bluetooth.
if&(mBluetoothAdapter&==&null&||&!mBluetoothAdapter.isEnabled())&{
&&&&Intent&enableBtIntent&=&new&Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
&&&&startActivityForResult(enableBtIntent,&REQUEST_ENABLE_BT);
2、查找BLE设备
& &因为扫描BLE设备是电源密集型操作,浪费电量,因此要保证以下原则:
& & 1)扫描到需要的设备后,马上停止扫描;
& & 2)给扫描一个时间限制
& &扫描示例代码如下:
&*&Activity&for&scanning&and&displaying&available&BLE&devices.
public&class&DeviceScanActivity&extends&ListActivity&{
&&&&private&BluetoothAdapter&mBluetoothA
&&&&private&boolean&mS
&&&&private&Handler&mH
&&&&//&Stops&scanning&after&10&seconds.
&&&&private&static&final&long&SCAN_PERIOD&=&10000;
&&&&private&void&scanLeDevice(final&boolean&enable)&{
&&&&&&&&if&(enable)&{
&&&&&&&&&&&&//&Stops&scanning&after&a&pre-defined&scan&period.
&&&&&&&&&&&&mHandler.postDelayed(new&Runnable()&{
&&&&&&&&&&&&&&&&@Override
&&&&&&&&&&&&&&&&public&void&run()&{
&&&&&&&&&&&&&&&&&&&&mScanning&=&
&&&&&&&&&&&&&&&&&&&&mBluetoothAdapter.stopLeScan(mLeScanCallback);
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&},&SCAN_PERIOD);
&&&&&&&&&&&&mScanning&=&
&&&&&&&&&&&&mBluetoothAdapter.startLeScan(mLeScanCallback);
&&&&&&&&}&else&{
&&&&&&&&&&&&mScanning&=&
&&&&&&&&&&&&mBluetoothAdapter.stopLeScan(mLeScanCallback);
&&&&&&&&...
& & &如果只是要扫描到特定类型的设备,则使用接口&,通过UUID来查找设备。
& & 扫描回调的代码如下所示:
private&LeDeviceListAdapter&mLeDeviceListA
//&Device&scan&callback.
private&BluetoothAdapter.LeScanCallback&mLeScanCallback&=
&&&&&&&&new&BluetoothAdapter.LeScanCallback()&{
&&&&@Override
&&&&public&void&onLeScan(final&BluetoothDevice&device,&int&rssi,
&&&&&&&&&&&&byte[]&scanRecord)&{
&&&&&&&&runOnUiThread(new&Runnable()&{
&&&&&&&&&&&@Override
&&&&&&&&&&&public&void&run()&{
&&&&&&&&&&&&&&&mLeDeviceListAdapter.addDevice(device);
&&&&&&&&&&&&&&&mLeDeviceListAdapter.notifyDataSetChanged();
&&&&&&&&&&&}
&&&&&&&});
注意:我们既可以扫描BLE设备,也可以扫描普通蓝牙设备,也可以同时将BLE设备和普通蓝牙设备一起扫描到。
3、连接到GATT Server
& &获取到BluetoothGatt实例,
mBluetoothGatt&=&device.connectGatt(this,&false,&mGattCallback);
&&&&具体实例如下:
//&A&service&that&interacts&with&the&BLE&device&via&the&Android&BLE&API.
public&class&BluetoothLeService&extends&Service&{
&&&&private&final&static&String&TAG&=&BluetoothLeService.class.getSimpleName();
&&&&private&BluetoothManager&mBluetoothM
&&&&private&BluetoothAdapter&mBluetoothA
&&&&private&String&mBluetoothDeviceA
&&&&private&BluetoothGatt&mBluetoothG
&&&&private&int&mConnectionState&=&STATE_DISCONNECTED;
&&&&private&static&final&int&STATE_DISCONNECTED&=&0;
&&&&private&static&final&int&STATE_CONNECTING&=&1;
&&&&private&static&final&int&STATE_CONNECTED&=&2;
&&&&public&final&static&String&ACTION_GATT_CONNECTED&=
&&&&&&&&&&&&"com.example.bluetooth.le.ACTION_GATT_CONNECTED";
&&&&public&final&static&String&ACTION_GATT_DISCONNECTED&=
&&&&&&&&&&&&"com.example.bluetooth.le.ACTION_GATT_DISCONNECTED";
&&&&public&final&static&String&ACTION_GATT_SERVICES_DISCOVERED&=
&&&&&&&&&&&&"com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED";
&&&&public&final&static&String&ACTION_DATA_AVAILABLE&=
&&&&&&&&&&&&"com.example.bluetooth.le.ACTION_DATA_AVAILABLE";
&&&&public&final&static&String&EXTRA_DATA&=
&&&&&&&&&&&&"com.example.bluetooth.le.EXTRA_DATA";
&&&&public&final&static&UUID&UUID_HEART_RATE_MEASUREMENT&=
&&&&&&&&&&&&UUID.fromString(SampleGattAttributes.HEART_RATE_MEASUREMENT);
&&&&//&Various&callback&methods&defined&by&the&BLE&API.
&&&&private&final&BluetoothGattCallback&mGattCallback&=
&&&&&&&&&&&&new&BluetoothGattCallback()&{
&&&&&&&&@Override
&&&&&&&&public&void&onConnectionStateChange(BluetoothGatt&gatt,&int&status,
&&&&&&&&&&&&&&&&int&newState)&{
&&&&&&&&&&&&String&intentA
&&&&&&&&&&&&if&(newState&==&BluetoothProfile.STATE_CONNECTED)&{
&&&&&&&&&&&&&&&&intentAction&=&ACTION_GATT_CONNECTED;
&&&&&&&&&&&&&&&&mConnectionState&=&STATE_CONNECTED;
&&&&&&&&&&&&&&&&broadcastUpdate(intentAction);
&&&&&&&&&&&&&&&&Log.i(TAG,&"Connected&to&GATT&server.");
&&&&&&&&&&&&&&&&Log.i(TAG,&"Attempting&to&start&service&discovery:"&+
&&&&&&&&&&&&&&&&&&&&&&&&mBluetoothGatt.discoverServices());
&&&&&&&&&&&&}&else&if&(newState&==&BluetoothProfile.STATE_DISCONNECTED)&{
&&&&&&&&&&&&&&&&intentAction&=&ACTION_GATT_DISCONNECTED;
&&&&&&&&&&&&&&&&mConnectionState&=&STATE_DISCONNECTED;
&&&&&&&&&&&&&&&&Log.i(TAG,&"Disconnected&from&GATT&server.");
&&&&&&&&&&&&&&&&broadcastUpdate(intentAction);
&&&&&&&&&&&&}
&&&&&&&&@Override
&&&&&&&&//&New&services&discovered
&&&&&&&&public&void&onServicesDiscovered(BluetoothGatt&gatt,&int&status)&{
&&&&&&&&&&&&if&(status&==&BluetoothGatt.GATT_SUCCESS)&{
&&&&&&&&&&&&&&&&broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
&&&&&&&&&&&&}&else&{
&&&&&&&&&&&&&&&&Log.w(TAG,&"onServicesDiscovered&received:&"&+&status);
&&&&&&&&&&&&}
&&&&&&&&@Override
&&&&&&&&//&Result&of&a&characteristic&read&operation
&&&&&&&&public&void&onCharacteristicRead(BluetoothGatt&gatt,
&&&&&&&&&&&&&&&&BluetoothGattCharacteristic&characteristic,
&&&&&&&&&&&&&&&&int&status)&{
&&&&&&&&&&&&if&(status&==&BluetoothGatt.GATT_SUCCESS)&{
&&&&&&&&&&&&&&&&broadcastUpdate(ACTION_DATA_AVAILABLE,&characteristic);
&&&&&&&&&&&&}
& & 其中,discoverService方式是异步的,它的回调方法是上面代码中的onServiceDiscovered。
private&void&broadcastUpdate(final&String&action)&{
&&&&final&Intent&intent&=&new&Intent(action);
&&&&sendBroadcast(intent);
private&void&broadcastUpdate(final&String&action,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&final&BluetoothGattCharacteristic&characteristic)&{
&&&&final&Intent&intent&=&new&Intent(action);
&&&&//&This&is&special&handling&for&the&Heart&Rate&Measurement&profile.&Data
&&&&//&parsing&is&carried&out&as&per&profile&specifications.
&&&&if&(UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid()))&{
&&&&&&&&int&flag&=&characteristic.getProperties();
&&&&&&&&int&format&=&-1;
&&&&&&&&if&((flag&&&0x01)&!=&0)&{
&&&&&&&&&&&&format&=&BluetoothGattCharacteristic.FORMAT_UINT16;
&&&&&&&&&&&&Log.d(TAG,&"Heart&rate&format&UINT16.");
&&&&&&&&}&else&{
&&&&&&&&&&&&format&=&BluetoothGattCharacteristic.FORMAT_UINT8;
&&&&&&&&&&&&Log.d(TAG,&"Heart&rate&format&UINT8.");
&&&&&&&&final&int&heartRate&=&characteristic.getIntValue(format,&1);
&&&&&&&&Log.d(TAG,&String.format("Received&heart&rate:&%d",&heartRate));
&&&&&&&&intent.putExtra(EXTRA_DATA,&String.valueOf(heartRate));
&&&&}&else&{
&&&&&&&&//&For&all&other&profiles,&writes&the&data&formatted&in&HEX.
&&&&&&&&final&byte[]&data&=&characteristic.getValue();
&&&&&&&&if&(data&!=&null&&&&data.length&&&0)&{
&&&&&&&&&&&&final&StringBuilder&stringBuilder&=&new&StringBuilder(data.length);
&&&&&&&&&&&&for(byte&byteChar&:&data)
&&&&&&&&&&&&&&&&stringBuilder.append(String.format("%02X&",&byteChar));
&&&&&&&&&&&&intent.putExtra(EXTRA_DATA,&new&String(data)&+&"\n"&+
&&&&&&&&&&&&&&&&&&&&stringBuilder.toString());
&&&&sendBroadcast(intent);
//&Handles&various&events&fired&by&the&Service.
//&ACTION_GATT_CONNECTED:&connected&to&a&GATT&server.
//&ACTION_GATT_DISCONNECTED:&disconnected&from&a&GATT&server.
//&ACTION_GATT_SERVICES_DISCOVERED:&discovered&GATT&services.
//&ACTION_DATA_AVAILABLE:&received&data&from&the&device.&This&can&be&a
//&result&of&read&or&notification&operations.
private&final&BroadcastReceiver&mGattUpdateReceiver&=&new&BroadcastReceiver()&{
&&&&@Override
&&&&public&void&onReceive(Context&context,&Intent&intent)&{
&&&&&&&&final&String&action&=&intent.getAction();
&&&&&&&&if&(BluetoothLeService.ACTION_GATT_CONNECTED.equals(action))&{
&&&&&&&&&&&&mConnected&=&
&&&&&&&&&&&&updateConnectionState(R.string.connected);
&&&&&&&&&&&&invalidateOptionsMenu();
&&&&&&&&}&else&if&(BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action))&{
&&&&&&&&&&&&mConnected&=&
&&&&&&&&&&&&updateConnectionState(R.string.disconnected);
&&&&&&&&&&&&invalidateOptionsMenu();
&&&&&&&&&&&&clearUI();
&&&&&&&&}&else&if&(BluetoothLeService.
&&&&&&&&&&&&&&&&ACTION_GATT_SERVICES_DISCOVERED.equals(action))&{
&&&&&&&&&&&&//&Show&all&the&supported&services&and&characteristics&on&the
&&&&&&&&&&&&//&user&interface.
&&&&&&&&&&&&displayGattServices(mBluetoothLeService.getSupportedGattServices());
&&&&&&&&}&else&if&(BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action))&{
&&&&&&&&&&&&displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
4、读BLE属性
& & 一旦获取到GATT的Services,就可以读写他们的属性了,实例如下:
public&class&DeviceControlActivity&extends&Activity&{
&&&&//&Demonstrates&how&to&iterate&through&the&supported&GATT
&&&&//&Services/Characteristics.
&&&&//&In&this&sample,&we&populate&the&data&structure&that&is&bound&to&the
&&&&//&ExpandableListView&on&the&UI.
&&&&private&void&displayGattServices(List&BluetoothGattService&&gattServices)&{
&&&&&&&&if&(gattServices&==&null)&
&&&&&&&&String&uuid&=&
&&&&&&&&String&unknownServiceString&=&getResources().
&&&&&&&&&&&&&&&&getString(R.string.unknown_service);
&&&&&&&&String&unknownCharaString&=&getResources().
&&&&&&&&&&&&&&&&getString(R.string.unknown_characteristic);
&&&&&&&&ArrayList&HashMap&String,&String&&&gattServiceData&=
&&&&&&&&&&&&&&&&new&ArrayList&HashMap&String,&String&&();
&&&&&&&&ArrayList&ArrayList&HashMap&String,&String&&&&gattCharacteristicData
&&&&&&&&&&&&&&&&=&new&ArrayList&ArrayList&HashMap&String,&String&&&();
&&&&&&&&mGattCharacteristics&=
&&&&&&&&&&&&&&&&new&ArrayList&ArrayList&BluetoothGattCharacteristic&&();
&&&&&&&&//&Loops&through&available&GATT&Services.
&&&&&&&&for&(BluetoothGattService&gattService&:&gattServices)&{
&&&&&&&&&&&&HashMap&String,&String&&currentServiceData&=
&&&&&&&&&&&&&&&&&&&&new&HashMap&String,&String&();
&&&&&&&&&&&&uuid&=&gattService.getUuid().toString();
&&&&&&&&&&&&currentServiceData.put(
&&&&&&&&&&&&&&&&&&&&LIST_NAME,&SampleGattAttributes.
&&&&&&&&&&&&&&&&&&&&&&&&&&&&lookup(uuid,&unknownServiceString));
&&&&&&&&&&&&currentServiceData.put(LIST_UUID,&uuid);
&&&&&&&&&&&&gattServiceData.add(currentServiceData);
&&&&&&&&&&&&ArrayList&HashMap&String,&String&&&gattCharacteristicGroupData&=
&&&&&&&&&&&&&&&&&&&&new&ArrayList&HashMap&String,&String&&();
&&&&&&&&&&&&List&BluetoothGattCharacteristic&&gattCharacteristics&=
&&&&&&&&&&&&&&&&&&&&gattService.getCharacteristics();
&&&&&&&&&&&&ArrayList&BluetoothGattCharacteristic&&charas&=
&&&&&&&&&&&&&&&&&&&&new&ArrayList&BluetoothGattCharacteristic&();
&&&&&&&&&&&//&Loops&through&available&Characteristics.
&&&&&&&&&&&&for&(BluetoothGattCharacteristic&gattCharacteristic&:
&&&&&&&&&&&&&&&&&&&&gattCharacteristics)&{
&&&&&&&&&&&&&&&&charas.add(gattCharacteristic);
&&&&&&&&&&&&&&&&HashMap&String,&String&&currentCharaData&=
&&&&&&&&&&&&&&&&&&&&&&&&new&HashMap&String,&String&();
&&&&&&&&&&&&&&&&uuid&=&gattCharacteristic.getUuid().toString();
&&&&&&&&&&&&&&&&currentCharaData.put(
&&&&&&&&&&&&&&&&&&&&&&&&LIST_NAME,&SampleGattAttributes.lookup(uuid,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&unknownCharaString));
&&&&&&&&&&&&&&&&currentCharaData.put(LIST_UUID,&uuid);
&&&&&&&&&&&&&&&&gattCharacteristicGroupData.add(currentCharaData);
&&&&&&&&&&&&}
&&&&&&&&&&&&mGattCharacteristics.add(charas);
&&&&&&&&&&&&gattCharacteristicData.add(gattCharacteristicGroupData);
&&&&&&&&&}
& 在获取Service的时候,每个蓝牙设备都会有两个默认的Service,它们和对应的UUID分别如下:
Bluetooth Generic Access Profile & &{0-805f9b34fb}
Bluetooth Generic Attribute Profile {0-805F9B34FB}
5、收到GATT通知
& &如果设备主动给手机发信息,则可以通过notification的方式,这种方式不用手机去轮询地读设备上的数据。手机可以用如下方式给设备设置notification功能。
private&BluetoothGatt&mBluetoothG
BluetoothGattCharacteristic&
mBluetoothGatt.setCharacteristicNotification(characteristic,&enabled);
BluetoothGattDescriptor&descriptor&=&characteristic.getDescriptor(
&&&&&&&&UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
& & 如果notificaiton方式对于某个Characteristic是enable的,那么当设备上的这个Characteristic改变时,手机上的&回调就会被促发。如下所示:
//&Characteristic&notification
public&void&onCharacteristicChanged(BluetoothGatt&gatt,
&&&&&&&&BluetoothGattCharacteristic&characteristic)&{
&&&&broadcastUpdate(ACTION_DATA_AVAILABLE,&characteristic);
6、关闭客户端蓝牙
public&void&close()&{
&&&&if&(mBluetoothGatt&==&null)&{
&&&&mBluetoothGatt.close();
&&&&mBluetoothGatt&=&
人打赏支持
码字总数 41795
支付宝支付
微信扫码支付
打赏金额: ¥
已支付成功
打赏金额: ¥需求发布后1小时内收到服务商响应每个需求平均有10个服务商参与95%以上的需求得到了圆满解决所有需求不向雇主收取任何佣金好的包装增加视觉销售力
蓝牙BLE功能开发技术支持
有相似问题想解决?专业顾问来帮助您
匹配服务商
选择服务商,签单
服务商工作
验收并付款
已投标服务商
综合评分:0颗星
速度:0.00
服务:0.00
态度:0.00
好评率:0%
共有2个服务商参与报价,查看更多服务商报价
参与报价,开始赚钱
提交你的报价和方案
中标后交付作品
获得任务赏金
极速:10分钟急速响应
高品质:精选服务商提供服务
放心:不满意可退款
APP成品套餐
APP成品源码套餐
根据浏览的需求为您推荐
交易成功的需求
APP定制开发相关需求
关注猪八戒微信

我要回帖

更多关于 蓝牙功率等级 的文章

 

随机推荐