Android Bluetooth 蓝牙4.0 详解
2016-08-09 15:06
357 查看
本文介绍Android ble 蓝牙4.0,也就是说API level >= 18,且支持蓝牙4.0的手机才可以使用,如果手机系统版本API level < 18,也是用不了蓝牙4.0的哦。
首先发一下官方的demo,有兴趣的可以过去看看: http://developer.android.com/guide/topics/connectivity/bluetooth-le.html 。android系统4.3以上,手机支持蓝牙4.0,具有搜索,配对,连接,发现服务及特征值,断开连接等功能,下载地址: http://download.csdn.net/detail/lqw770737185/8116019 。
一、了解api及概念
1.1 BluetoothGatt
继承BluetoothProfile,通过BluetoothGatt可以连接设备(connect),发现服务(discoverServices),并把相应地属性返回到 BluetoothGattCallback
1.2 BluetoothGattCharacteristic
相当于一个数据类型,它包括一个value和0~n个value的描述(BluetoothGattDescriptor)
1.3 BluetoothGattDescriptor
描述符,对Characteristic的描述,包括范围、计量单位等
1.4 BluetoothGattService
服务,Characteristic的集合。
1.5 BluetoothProfile
一个通用的规范,按照这个规范来收发数据。
1.6 BluetoothManager
通过BluetoothManager来获取BluetoothAdapter
1.7 BluetoothAdapter
一个Android系统只有一个BluetoothAdapter ,通过BluetoothManager 获取
1.8 BluetoothGattCallback
已经连接上设备,对设备的某些操作后返回的结果。这里必须提醒下,已经连接上设备后的才可以返回,没有返回的认真看看有没有连接上设备。
1.8.3:
1.8.4:连接蓝牙或者断开蓝牙 对应 onConnectionStateChange;
1.8.5:
1.8.6: writeDescriptor 对应 onDescriptorWrite;
1.8.7: readRemoteRssi 对应 onReadRemoteRssi;
1.8.8: executeReliableWrite 对应 onReliableWriteCompleted;
1.8.9: discoverServices 对应 onServicesDiscovered。
1.9 BluetoothDevice
扫描后发现可连接的设备,获取已经连接的设备
二、开启蓝牙权限
如果 android.hardware.bluetooth_le设置为false,可以安装在不支持的设备上使用,判断是否支持蓝牙4.0用以下代码就可以了
三、对蓝牙的启动关闭操作
1、利用系统默认开启蓝牙对话框
2、后台打开蓝牙,不做任何提示,这个也可以用来自定义打开蓝牙对话框啦
3、后台关闭蓝牙
四、扫描设备,连接设备,获取设备信息 ,断开连接设备,自行查看官方demo,还是看demo比较清晰啊
原文 : http://www.cnblogs.com/liqw/p/4031437.html
首先发一下官方的demo,有兴趣的可以过去看看: http://developer.android.com/guide/topics/connectivity/bluetooth-le.html 。android系统4.3以上,手机支持蓝牙4.0,具有搜索,配对,连接,发现服务及特征值,断开连接等功能,下载地址: http://download.csdn.net/detail/lqw770737185/8116019 。
一、了解api及概念
1.1 BluetoothGatt
继承BluetoothProfile,通过BluetoothGatt可以连接设备(connect),发现服务(discoverServices),并把相应地属性返回到 BluetoothGattCallback
1.2 BluetoothGattCharacteristic
相当于一个数据类型,它包括一个value和0~n个value的描述(BluetoothGattDescriptor)
1.3 BluetoothGattDescriptor
描述符,对Characteristic的描述,包括范围、计量单位等
1.4 BluetoothGattService
服务,Characteristic的集合。
1.5 BluetoothProfile
一个通用的规范,按照这个规范来收发数据。
1.6 BluetoothManager
通过BluetoothManager来获取BluetoothAdapter
1 | BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); |
一个Android系统只有一个BluetoothAdapter ,通过BluetoothManager 获取
1 | BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter(); |
已经连接上设备,对设备的某些操作后返回的结果。这里必须提醒下,已经连接上设备后的才可以返回,没有返回的认真看看有没有连接上设备。
1 2 3 4 5 6 7 | private BluetoothGattCallback GattCallback = new BluetoothGattCallback() { public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState){}; public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status){}; }; BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); BluetoothGatt gatt = device.connectGatt( this , false , mGattCallback); |
1.8.1:notification对应onCharacteristicChanged
;
1 | gatt.setCharacteristicNotification(characteristic, true ); |
1.8.2:
readCharacteristic对应onCharacteristicRead;
1 | gatt.readCharacteristic(characteristic); |
writeCharacteristic对应onCharacteristicWrite;
1 | gatt.wirteCharacteristic(mCurrentcharacteristic); |
1.8.5:
readDescriptor对应onDescriptorRead;
1.8.6: writeDescriptor 对应 onDescriptorWrite;
1 | gatt.writeDescriptor(descriptor); |
1 | gatt.readRemoteRssi() |
1.8.9: discoverServices 对应 onServicesDiscovered。
1 | gatt.discoverServices() |
扫描后发现可连接的设备,获取已经连接的设备
二、开启蓝牙权限
1 2 3 | <uses-permission android:name= "android.permission.BLUETOOTH" /> <uses-permission android:name= "android.permission.BLUETOOTH_ADMIN" /> <uses-feature android:name= "android.hardware.bluetooth_le" android:required= "true" /> |
1 2 3 4 | if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText( this , "设备不支持蓝牙4.0" , Toast.LENGTH_SHORT).show(); finish(); } |
1、利用系统默认开启蓝牙对话框
1 2 3 4 | if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } |
1 | mBluetoothAdapter.enable(); |
1 | mBluetoothAdapter.disable(); |
原文 : http://www.cnblogs.com/liqw/p/4031437.html
相关文章推荐
- Android Bluetooth HID实现详解
- Android Bluetooth使用详解
- Android Bluetooth详解(Android英文文档相关译文)
- Bluetooth---Android蓝牙4.0的数据通讯
- Android Bluetooth HID实现详解
- Android Bluetooth HID实现详解
- android之Bluetooth详解
- Android Bluetooth蓝牙开发\蓝牙协议\蓝牙通信例子_Android支持蓝牙4.0版本_BLE开发
- Android Bluetooth蓝牙开发\蓝牙协议\蓝牙通信例子_Android支持蓝牙4.0版本_BLE开发
- Android Bluetooth蓝牙开发\蓝牙协议\蓝牙通信例子_Android支持蓝牙4.0版本_BLE开发
- Android Bluetooth HID实现详解
- 【Android 应用开发】BluetoothSocket详解
- Android之Bluetooth详解
- Android Bluetooth详解(Android英文文档相关译文)
- Android Bluetooth蓝牙开发\蓝牙协议\蓝牙通信例子_Android支持蓝牙4.0版本_BLE开发
- Android中的Bluetooth详解
- Android中的Bluetooth详解
- 【Android 应用开发】BluetoothClass详解
- Android Bluetooth蓝牙开发\蓝牙协议\蓝牙通信例子_Android支持蓝牙4.0版本_BLE开发
- Android_Bluetooth详解