




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
BluetoothLowEnergy——蓝牙低功耗Android4.3(API级别18)引入内置平台支持BLE的central角色,同时提供API和app应用程序用来发现设备,查询服务,和读/写characteristics。与传统蓝牙(ClassicBluetooth)不同,蓝牙低功耗(BLE)的目的是提供更显著的低功耗。这使得Android应用程序可以和具有低功耗的要求BLE设备,如接近传感器,心脏速率监视器,健身设备等进行通信。
关键术语和概念下面是关键BLE术语和概念的总结:通用属性规范(GATT)—GATTprofile是一个通用规范用于在BLE链路发送和接收被称为“属性(attributes)”的数据片。目前所有的低功耗应用profile都是基于GATT。蓝牙SIG定义了许多profile用于低功耗设备。Profile(配置文件)是一个规范,规范了设备如何工作在一个特定的应用场景。注意:一个设备可以实现多个profile。例如,一个设备可以包含一个心脏监测仪和电池电平检测器。
属性协议(ATT)—GATT是建立在属性协议(ATT)的顶层,通常也被称为GATT/ATT。ATT进行了优化用于在BLE设备上运行。为此,它采用尽可能少的字节越好。每个attribute属性被UUID(通用唯一标识符)唯一标识,UUID是标准128-bit格式的ID用来唯一标识信息。attributes被ATT格式化characteristics和services形式进行传送。特征(Characteristics)—一个characteristics包含一个单独的value值和0–n个用来描述characteristic值(value)的descriptors。一个characteristics可以被认为是一种类型的,类似于一个类。描述符(descriptor)—descriptor是被定义的attributes,用来描述一个characteristic的值。例如,一个descriptor可以指定一个人类可读的描述中,在可接受的范围里characteristic值,或者是测量单位,用来明确characteristic的值。服务(service)—service是characteristic的集合。例如,你可以有一个所谓的“HeartRateMonitor”service,其中包括characteristic,如“heartratemeasurement”。你可以在
找到关于一系列基于GATT的profile和service。
角色和职责以下是适用于当一个Android设备与BLE设备交互的角色和责任:中心设备(central)与外围设备(peripheral)。这也适用于BLE连接本身。Central设备进行扫描,寻找advertisenment,peripheral设备发出advertisement。GATTserver(服务器)与GATTclient(客户端)。这决定了两个设备建立连接后如何互相交互。
要了解它们的区别,假设你有一个Android手机和活动跟踪器,活动跟踪器是一个BLE装置。这款手机扮演central角色;活动跟踪器扮演peripheral角色(建立一个BLE连接,必须具备两者。如果两个设备只支持central角色或peripheral角色,不能跟对方建立一个BLE连接)。
一旦手机与活动跟踪器已经建立连接,他们开始相互传送GATT数据。根据它们传送数据的种类,其中一个可能作为GATTserver。例如,如果该活动跟踪器将传感器数据汇报到手机上,活动跟踪器作为server。如果活动跟踪器想要从手机接收更新,那么手机作为server。
在本文档中使用的示例中,Android应用程序(在Android设备上运行)是GATTclient。该应用从GATTserver获取数据,server是一款支持
HeartRateProfile的BLE心脏速率监测仪。但你可以设计你的Android应用程序,作为GATTserver角色。见BluetoothGattServer
获取更多信息。
BLE权限为了使用应用程序中的蓝牙功能,你必须声明蓝牙权限BLUETOOTH。你需要这个权限执行任意蓝牙通讯,如请求连接,接受连接,传输数据。如果你希望你的应用程序启动设备发现或操纵蓝牙设置,还必须声明BLUETOOTH_ADMIN权限。注意:如果您使用BLUETOOTH_ADMIN权限,那么你还必须有BLUETOOTH权限。
声明蓝牙权限在你的应用程序清单(manifest)文件。例如:<uses-permissionandroid:name="android.permission.BLUETOOTH"/><uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN"/>如果你想声明,你的应用程序是只提供给BLE功能的设备,在您的应用程序的清单包括如下语句:<uses-featureandroid:name="android.hardware.bluetooth_le"android:required="true"/>不过,如果你想使你的应用程序提供给那些不支持BLE设备,你仍然应该在您的应用程序的清单包含这个上述语句,但设置required="false"。然后在运行时可以通过使用
PackageManager.hasSystemFeature()确定BLE可用性://UsethischecktodeterminewhetherBLEissupportedonthedevice.Then//youcanselectivelydisableBLE-relatedfeatures.if(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)){Toast.makeText(this,R.string.ble_not_supported,Toast.LENGTH_SHORT).show();finish();}设置BLE在你的应用程序可以进行BLE通信之前,你需要验证这个设备上BLE是否被支持,如果支持,请确保它已启用。请注意,如果<uses-feature.../>设置为false,这个检查才是必需的。如果不支持BLE,那么你应该适当地禁用任何BLE功能。如果BLE支持,但被禁用,那么你可以要求用户启动蓝牙时不要离开应用程序。这种设置两个步骤完成,使用
BluetoothAdapter.
1.获取BluetoothAdapter该BluetoothAdapter是所有的蓝牙活动所必需的。该BluetoothAdapter代表设备自身的蓝牙适配器(蓝牙无线电)。只有一个蓝牙适配器用于整个系统,并且你的应用程序可以使用该对象进行交互。下面的代码片段显示了如何获取适配器。注意,该方法使用getSystemService()
返回BluetoothManager的一个实例,用于获取适配器。Android4.3(API级别18)引入BluetoothManager://InitializesBluetoothadapter.finalBluetoothManagerbluetoothManager=(BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);mBluetoothAdapter=bluetoothManager.getAdapter();2.启用蓝牙接下来,你需要确保蓝牙已启用。用isEnabled()
来检查蓝牙当前是否启用。如果此方法返回false,那么蓝牙被禁用。下面的代码片段检查蓝牙是否开启。如果不是,该片段将显示错误提示用户去设置以启用蓝牙:privateBluetoothAdaptermBluetoothAdapter;...//EnsuresBluetoothisavailableonthedeviceanditisenabled.Ifnot,//displaysadialogrequestinguserpermissiontoenableBluetooth.if(mBluetoothAdapter==null||!mBluetoothAdapter.isEnabled()){IntentenableBtIntent=newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);}查找BLE设备为了找到BLE设备,您可以使用startLeScan()
方法。此方法需要一个BluetoothAdapter.LeScanCallback作为参数。你必须实现这个回调,因为它决定扫描结果如何返回。因为扫描耗电量大,你应当遵守以下准则:1)只要你找到所需的设备,停止扫描。2)不要扫描一个循环,并设置您的扫描时间限制。以前可用的设备可能已经移出范围,继续扫描消耗电池电量。下面的代码片段显示了如何启动和停止扫描:/***ActivityforscanninganddisplayingavailableBLEdevices.*/publicclassDeviceScanActivityextendsListActivity{privateBluetoothAdaptermBluetoothAdapter;privatebooleanmScanning;privateHandlermHandler;//Stopsscanningafter10seconds.privatestaticfinallongSCAN_PERIOD=10000;...privatevoidscanLeDevice(finalbooleanenable){if(enable){//Stopsscanningafterapre-definedscanperiod.mHandler.postDelayed(newRunnable(){@Overridepublicvoidrun(){mScanning=false;mBluetoothAdapter.stopLeScan(mLeScanCallback);}},SCAN_PERIOD);mScanning=true;mBluetoothAdapter.startLeScan(mLeScanCallback);}else{mScanning=false;mBluetoothAdapter.stopLeScan(mLeScanCallback);}...}...}如何你想要扫描指定类型的peripheral设备,你可以用
startLeScan(UUID[],BluetoothAdapter.LeScanCallback)取代,它提供了一组UUID对象用于说明你的应用程序支持的GATTservices.下面是用于传送BLE扫描结果的接口函数BluetoothAdapter.LeScanCallback的具体实现:privateLeDeviceListAdaptermLeDeviceListAdapter;...//Devicescancallback.privateBluetoothAdapter.LeScanCallbackmLeScanCallback=newBluetoothAdapter.LeScanCallback(){@OverridepublicvoidonLeScan(finalBluetoothDevicedevice,intrssi,byte[]scanRecord){runOnUiThread(newRunnable(){@Overridepublicvoidrun(){mLeDeviceListAdapter.addDevice(device);mLeDeviceListAdapter.notifyDataSetChanged();}});}};注意:你可以只扫描BLE设备或传统蓝牙设备,就像Bluetooth描述那样。你不可以同时扫描BLE设备和传统蓝牙设备。
连接到GATTserver在和一个BLE设备交互的第一步是连接到它,更具体地,连接到所述装置上的GATTserver。要连接到一个BLE设备上的GATTserver,您可以使用connectGatt()方法。这个方法有三个参数:一个Context
对象,一个autoConnect(布尔值,表示是否自动连接到BLE装置)和BluetoothGattCallback:mBluetoothGatt=device.connectGatt(this,false,mGattCallback);
这个函数连接到由BLE设备上的GATTserver,并返回一个BluetoothGatt实例,您可以使用它来进行GATTclient操作。调用者(Android应用程序)是GATTclient。该BluetoothGattCallback是用来提供结果给client,如连接状态,以及任何进一步的GATTclient操作。
在这个例子中,BLE应用程序提供了一个活动(DeviceControlActivity)连接,显示数据,和显示该设备支持的GATTservices和characteristics。根据用户的输入,这一活动与一个叫BluetoothLeService的Service交互,BluetoothService通过AndroidBLE的API与BLE设备进行交互://AservicethatinteractswiththeBLEdeviceviatheAndroidBLEAPI.publicclassBluetoothLeServiceextendsService{privatefinalstaticStringTAG=BluetoothLeService.class.getSimpleName();privateBluetoothManagermBluetoothManager;privateBluetoothAdaptermBluetoothAdapter;privateStringmBluetoothDeviceAddress;privateBluetoothGattmBluetoothGatt;privateintmConnectionState=STATE_DISCONNECTED;privatestaticfinalintSTATE_DISCONNECTED=0;privatestaticfinalintSTATE_CONNECTING=1;privatestaticfinalintSTATE_CONNECTED=2;publicfinalstaticStringACTION_GATT_CONNECTED="com.example.bluetooth.le.ACTION_GATT_CONNECTED";publicfinalstaticStringACTION_GATT_DISCONNECTED="com.example.bluetooth.le.ACTION_GATT_DISCONNECTED";publicfinalstaticStringACTION_GATT_SERVICES_DISCOVERED="com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED";publicfinalstaticStringACTION_DATA_AVAILABLE="com.example.bluetooth.le.ACTION_DATA_AVAILABLE";publicfinalstaticStringEXTRA_DATA="com.example.bluetooth.le.EXTRA_DATA";publicfinalstaticUUIDUUID_HEART_RATE_MEASUREMENT=UUID.fromString(SampleGattAttributes.HEART_RATE_MEASUREMENT);//VariouscallbackmethodsdefinedbytheBLEAPI.privatefinalBluetoothGattCallbackmGattCallback=newBluetoothGattCallback(){@OverridepublicvoidonConnectionStateChange(BluetoothGattgatt,intstatus,intnewState){StringintentAction;if(newState==BluetoothProfile.STATE_CONNECTED){intentAction=ACTION_GATT_CONNECTED;mConnectionState=STATE_CONNECTED;broadcastUpdate(intentAction);Log.i(TAG,"ConnectedtoGATTserver.");Log.i(TAG,"Attemptingtostartservicediscovery:"+mBluetoothGatt.discoverServices());}elseif(newState==BluetoothProfile.STATE_DISCONNECTED){intentAction=ACTION_GATT_DISCONNECTED;mConnectionState=STATE_DISCONNECTED;Log.i(TAG,"DisconnectedfromGATTserver.");broadcastUpdate(intentAction);}}@Override//NewservicesdiscoveredpublicvoidonServicesDiscovered(BluetoothGattgatt,intstatus){if(status==BluetoothGatt.GATT_SUCCESS){broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);}else{Log.w(TAG,"onServicesDiscoveredreceived:"+status);}}@Override//ResultofacharacteristicreadoperationpublicvoidonCharacteristicRead(BluetoothGattgatt,BluetoothGattCharacteristiccharacteristic,intstatus){if(status==BluetoothGatt.GATT_SUCCESS){broadcastUpdate(ACTION_DATA_AVAILABLE,characteristic);}}...};...}当一个特定的回调被触发时,它会调用相应的broadcastUpdate()辅助方法并传递给它一个动作。注意,在该部分中的数据解析参照BluetoothHeartRateMeasurement
profilespecifications进行。privatevoidbroadcastUpdate(finalStringaction){finalIntentintent=newIntent(action);sendBroadcast(intent);}privatevoidbroadcastUpdate(finalStringaction,finalBluetoothGattCharacteristiccharacteristic){finalIntentintent=newIntent(action);//ThisisspecialhandlingfortheHeartRateMeasurementprofile.Data//parsingiscarriedoutasperprofilespecifications.if(UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())){intflag=characteristic.getProperties();intformat=-1;if((flag&0x01)!=0){format=BluetoothGattCharacteristic.FORMAT_UINT16;Log.d(TAG,"HeartrateformatUINT16.");}else{format=BluetoothGattCharacteristic.FORMAT_UINT8;Log.d(TAG,"HeartrateformatUINT8.");}finalintheartRate=characteristic.getIntValue(format,1);Log.d(TAG,String.format("Receivedheartrate:%d",heartRate));intent.putExtra(EXTRA_DATA,String.valueOf(heartRate));}else{//Forallotherprofiles,writesthedataformattedinHEX.finalbyte[]data=characteristic.getValue();if(data!=null&&data.length>0){finalStringBuilderstringBuilder=newStringBuilder(data.length);for(bytebyteChar:data)stringBuilder.append(String.format("%02X",byteChar));intent.putExtra(EXTRA_DATA,newString(data)+"\n"+stringBuilder.toString());}}sendBroadcast(intent);}早在DeviceControlActivity,这些事件由一个BroadcastReceiver处理://HandlesvariouseventsfiredbytheService.//ACTION_GATT_CONNECTED:connectedtoaGATTserver.//ACTION_GATT_DISCONNECTED:disconnectedfromaGATTserver.//ACTION_GATT_SERVICES_DISCOVERED:discoveredGATTservices.//ACTION_DATA_AVAILABLE:receiveddatafromthedevice.Thiscanbea//resultofreadornotificationoperations.privatefinalBroadcastReceivermGattUpdateReceiver=newBroadcastReceiver(){@OverridepublicvoidonReceive(Contextcontext,Intentintent){finalStringaction=intent.getAction();if(BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)){mConnected=true;updateConnectionState(R.string.connected);invalidateOptionsMenu();}elseif(BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)){mConnected=false;updateConnectionState(R.string.disconnected);invalidateOptionsMenu();clearUI();}elseif(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)){//Showallthesupportedservicesandcharacteristicsonthe//userinterface.displayGattServices(mBluetoothLeService.getSupportedGattServices());}elseif(BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)){displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));}}};读取BLE属性一旦你的Android应用程序已连接到GATTserver和discoveriableservice,它可以读取和写入支持的attributes。例如,通过server的service和characteristic这个片段进行迭代,在UI上显示它们:publicclassDeviceControlActivityextendsActivity{...//DemonstrateshowtoiteratethroughthesupportedGATT//Services/Characteristics.//Inthissample,wepopulatethedatastructurethatisboundtothe//ExpandableListViewontheUI.privatevoiddisplayGattServices(List<BluetoothGattService>gattServices){if(gattServices==null)return;Stringuuid=null;StringunknownServiceString=getResources().getString(R.string.unknown_service);StringunknownCharaString=getResources().getString(R.string.unknown_characteristic);ArrayList<HashMap<String,String>>gattServiceData=newArrayList<HashMap<String,String>>();ArrayList<ArrayList<HashMap<String,String>>>gattCharacteristicData=newArrayList<ArrayList<HashMap<String,String>>>();mGattCharacteristics=newArrayList<ArrayList<BluetoothGattCharacteristic>>();//LoopsthroughavailableGATTServices.for(BluetoothGattServicegattService:gattServices){HashMap<String,String>currentServiceData=newHashMap<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=newArrayList<HashMap<String,String>>();List<BluetoothGattCharacteristic>gattCharacteristics=gattService.getCharacteristics();ArrayList<BluetoothGattCharacteristic>charas=newArrayList<BluetoothGattCharacteristic>();//LoopsthroughavailableCharacteristics.for(BluetoothGattCharacteristicgattCharacteristic:gattCharacteristics){charas.add(gattCharacteristic);HashMap<String,String>currentCharaData=newHashMap<String,String>();uuid=gattCharacteristic.getUuid().
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论