2014-09-蓝牙4.0初体验.docx_第1页
2014-09-蓝牙4.0初体验.docx_第2页
2014-09-蓝牙4.0初体验.docx_第3页
2014-09-蓝牙4.0初体验.docx_第4页
2014-09-蓝牙4.0初体验.docx_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

蓝牙4.0Ble 开发要点Android 4.3才开始支持BLE API,所以保证在蓝牙4.0在Android 4.3及其以上的系统使用首先要确定他所需要的权限,2个权限如下:BLE分为三部分Service、Characteristic、Descriptor,这三部分都由UUID作为唯一标示符。一个蓝牙4.0的终端可以包含多个Service,一个Service可以包含多个Characteristic,一个Characteristic包含一个Value和多个Descriptor,一个Descriptor包含一个Value。蓝牙使用第一步:判断手机是否支持蓝牙4.0 方法如下if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE) / 满足条件,表示手机支持初始化 Bluetooth adapter, 通过蓝牙管理器得到一个参考蓝牙适配器(API必须在以上android4.3或以上和版本)BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);mBluetoothAdapter = bluetoothManager.getAdapter();蓝牙使用第二步:搜索蓝牙设备/开始搜索mBluetoothAdapter.startLeScan(mLeScanCallback);/停止搜索mBluetoothAdapter.stopLeScan(mLeScanCallback);搜索蓝牙的回调方法:/ Device scan callback.private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() Overridepublic void onLeScan(final BluetoothDevice device, final int rssi, final byte scanRecord) runOnUiThread(new Runnable() Overridepublic void run() / 可以获取搜索到的蓝牙的一些信息 ,包括名称,mark地址等等name = device.getName();address = device.getAddress(););第三步:蓝牙连接mBluetoothGatt = device.connectGatt(this, false, mGattCallback);此方法会根据连接的结果给予回调方法;private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() Overridepublic void onConnectionStateChange(BluetoothGatt gatt, int status,int newState) String intentAction;System.out.println(=status: + status+,newState=+newState);/ 蓝牙连接成功if (newState = BluetoothProfile.STATE_CONNECTED) intentAction = ACTION_GATT_CONNECTED;mConnectionState = STATE_CONNECTED;broadcastUpdate(intentAction);Log.i(TAG, Connected to GATT server.);/ Attempts to discover services after successful connection.Log.i(TAG, Attempting to start service discovery:+ mBluetoothGatt.discoverServices();/ 蓝牙连接成功之后,必须调用此方法来发现蓝牙的服务mBluetoothGatt.discoverServices(); / 蓝牙连接失败else if (newState = BluetoothProfile.STATE_DISCONNECTED) intentAction = ACTION_GATT_DISCONNECTED;mConnectionState = STATE_DISCONNECTED;Log.i(TAG, Disconnected from GATT server.);broadcastUpdate(intentAction);/ 发现服务Overridepublic void onServicesDiscovered(BluetoothGatt gatt, int status) if (status = BluetoothGatt.GATT_SUCCESS) / 发现服务 else / 读取蓝牙数据Overridepublic void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) System.out.println(onCharacteristicRead);if (status = BluetoothGatt.GATT_SUCCESS) broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);Overridepublic void onDescriptorWrite(BluetoothGatt gatt,BluetoothGattDescriptor descriptor, int status) System.out.println(onDescriptorWriteonDescriptorWrite = + status+ , descriptor = + descriptor.getUuid().toString();/ 服务状态发生变化Overridepublic void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic) broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);if (characteristic.getValue() != null) System.out.println(characteristic.getStringValue(0);System.out.println(-onCharacteristicChanged-);/ 获取蓝牙信号强度Overridepublic void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) System.out.println(rssi = + rssi);/ 写入数据public void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) System.out.println(-write success- status: + status);System.out.println(-write success- WriteType: + characteristic.getWriteType();第四步:蓝牙服务读取 读取蓝牙服务,遍历服务,获取到自己需要的服务uuid遍历次服务下的特征值,判断读,写,通知属性,设置通知属性发送数据到蓝牙设备打开通知for (BluetoothGattService gattService : gattServices) uuid_1 = gattService.getUuid().toString();Log.e(info1,uuid_1=+uuid_1);List gattCharacteristics = gattService.getCharacteristics();Map charas = new HashMap();if(Config.uuid.equals(uuid_1)String uuid=null;/ Loops through available Characteristics.for (final BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) uuid = gattCharacteristic.getUuid().toString();Log.e(info1,uuid=+uuid);if(Config.uuid_write.equals(uuid)final int prop = gattCharacteristic.getProperties();/ 具有可写属性 if (prop & BluetoothGattCharacteristic.PROPERTY_WRITE) 0) Log.e(info,uuid_write 可读);/ 具有通知属性 if (prop & BluetoothGattCharacteristic.PROPERTY_NOTIFY) 0) Log.e(info,uuid_write 通知属性);if (prop & BluetoothGattCharacteristic.PROPERTY_WRITE) 0) Log.e(info,uuid_write 可写);charas.put(uuid, gattCharacteristic);if(Config.uuid_read.equals(uuid)final int prop = gattCharacteristic.getProperties();/ 具有可写属性 if (prop & BluetoothGattCharacteristic.PROPERTY_WRITE) 0) Log.e(info,uuid_read 可写);/ 具有可读属性 if (prop & BluetoothGattCharacteristic.PROPERTY_READ) 0) Log.e(info,uuid_read 可读);/ 具有通知属性 if (prop & BluetoothGattCharacteristic.PROPERTY_NOTIFY) 0) Log.e(info,uuid_read 通知属性);MyApplication.getInstance().bindService.setCharacteristicNotification(gattCharacteristic, true);charas.put(uuid, gattCharacteristic);i

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论