本文介绍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
 
 
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
1.7 BluetoothAdapter
 
一个Android系统只有一个BluetoothAdapter ,通过BluetoothManager 获取
 
BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
1.8 BluetoothGattCallback
 
已经连接上设备,对设备的某些操作后返回的结果。这里必须提醒下,已经连接上设备后的才可以返回,没有返回的认真看看有没有连接上设备。
 
 
private BluetoothGattCallback GattCallback = new BluetoothGattCallback() {
    // 这里有9个要实现的方法,看情况要实现那些,用到那些就实现那些
    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;
 
 
gatt.setCharacteristicNotification(characteristic, true);
1.8.2:readCharacteristic对应onCharacteristicRead;
 
 
gatt.readCharacteristic(characteristic);
1.8.3: writeCharacteristic对应onCharacteristicWrite;
 
 
gatt.wirteCharacteristic(mCurrentcharacteristic);
1.8.4:连接蓝牙或者断开蓝牙 对应 onConnectionStateChange;
 
1.8.5: readDescriptor对应onDescriptorRead;
 
1.8.6:writeDescriptor对应onDescriptorWrite;
 
 
gatt.writeDescriptor(descriptor);
1.8.7:readRemoteRssi对应onReadRemoteRssi;
 
 
gatt.readRemoteRssi()
1.8.8:executeReliableWrite对应onReliableWriteCompleted;
 
1.8.9:discoverServices对应onServicesDiscovered。
 
gatt.discoverServices()
1.9 BluetoothDevice
 
扫描后发现可连接的设备,获取已经连接的设备
 
二、开启蓝牙权限
 
 
<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"/>
如果 android.hardware.bluetooth_le设置为false,可以安装在不支持的设备上使用,判断是否支持蓝牙4.0用以下代码就可以了
 
 
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
    Toast.makeText(this, "设备不支持蓝牙4.0", Toast.LENGTH_SHORT).show();
    finish();
}
三、对蓝牙的启动关闭操作
 
1、利用系统默认开启蓝牙对话框
 
 
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
2、后台打开蓝牙,不做任何提示,这个也可以用来自定义打开蓝牙对话框啦
 
 
mBluetoothAdapter.enable();
3、后台关闭蓝牙
 
mBluetoothAdapter.disable();
四、扫描设备,连接设备,获取设备信息 ,断开连接设备,自行查看官方demo,还是看demo比较清晰啊

Android ble 蓝牙4.0 总结的更多相关文章

  1. Android ble 蓝牙4.0 总结一

    本文介绍Android ble 蓝牙4.0,也就是说API level >= 18,且支持蓝牙4.0的手机才可以使用,如果手机系统版本API level < 18,也是用不了蓝牙4.0的哦 ...

  2. android5.0 BLE 蓝牙4.0+浅析demo搜索(一)

    作者:Bgwan链接:https://zhuanlan.zhihu.com/p/23341414来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者:Bgwan 莳萝花 ...

  3. Android BLE蓝牙详细解读

    代码地址如下:http://www.demodashi.com/demo/15062.html 随着物联网时代的到来,越来越多的智能硬件设备开始流行起来,比如智能手环.心率检测仪.以及各式各样的智能家 ...

  4. Android5.0(Lollipop) BLE蓝牙4.0+浅析demo连接(三)

    作者:Bgwan链接:https://zhuanlan.zhihu.com/p/23363591来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. Android5.0(L ...

  5. Android5.0(Lollipop) BLE蓝牙4.0+浅析code(二)

    作者:Bgwan链接:https://zhuanlan.zhihu.com/p/23347612来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. Android5.0(L ...

  6. Android BLE 蓝牙编程(一)

    最近在研究这个,等我有时间来写吧! 终于在端午节给自己放个假,现在就来说说关于android蓝牙ble的 最近的学习成果吧!! 需要材料(写个简单教程吧--关于小米手环的哦!嘿嘿) Android 手 ...

  7. android ble蓝牙开发略解

    Android 蓝牙4.0开发 1.  权限和相关属性 “android:required="true"表示apk只有在具有bluetooth_le属性的系统里运行,这个4.3之前 ...

  8. Android BLE 蓝牙开发——扫码枪基于BLESSED

    一.蓝牙模式HID与BLE 当扫码枪与手机连接时,通常采用的是蓝牙HID(Human Interface Device)模式.本质上是一个把扫码枪作为一个硬件键盘,按照键盘协议把扫码后的结果逐个输入到 ...

  9. Android BLE 蓝牙编程(四)

    接上篇,我们已经实现了短震,长震的功能了- 现在我们需要实现点击后一直震动的功能 开始我的想法是再循环中不断执行write方法,然而这个办法行不通. 系统会报错. 那要如何实现这个想法呢?其实很简单, ...

随机推荐

  1. linux内核源码阅读之facebook硬盘加速flashcache之三

    上一节讲到在刷缓存的时候会调用new_kcahed_job创建kcached_job,由此我们也可以看到cache数据块与磁盘数据的对应关系.上一篇:http://blog.csdn.net/lium ...

  2. python下的web服务器模块

    python下的web服务模块有三种: BaseHTTPServer: 提供基本的Web服务和处理器类,分别是HTTPServer和BaseHTTPRequestHandler SimpleHTTPS ...

  3. stm32之中断系统

    概述: 提供中断控制器,用于总体管理异常,称之为“嵌套向量中断控制器:Nested Vectored Interrupt Controller (NVIC) VIC:中断管理器: NVIC:内嵌中断管 ...

  4. SPI通信

    SPI是由Motorola公司提出的一种同步串行外围接口:它在速度要求不高,低功耗,需要保存少量参数的智能化传感系统中得到了广泛应用: SPI是一个全双工的同步串行接口,在数据传输过程中,总线上只能是 ...

  5. [Swust OJ 648]--简单字典(数位dp)

    题目链接:http://acm.swust.edu.cn/problem/0648/ Time limit(ms): 1000 Memory limit(kb): 65535   有这样一本字典,它每 ...

  6. Oracle AWR 报告详解

    转自:http://blog.csdn.net/laoshangxyc/article/details/8615187 持续更新中... Oracle awr报告详解 DB Name DB Id In ...

  7. 将时间显示为“刚刚”“n分钟/小时前”等

    在很多场合为了显示出信息的及时性,一般会将时间显示成“刚刚”,“5分钟前”,“3小时前”等,而不是直接将时间打印出来.比如微博,SNS类应用就最长用到这个功能.而一般存储在数据库中的时间格式为 Uni ...

  8. main函数的参数问题 (转载)

    void main(int arg ,char *arv[]){} arg -- 命令行参数总个数arv[0] -- 参数1,程序名 arv[1] -- 参数2,字符串 arv[2] -- 参数3,字 ...

  9. 已知的CPropertysheet bug: 切换焦点导致无响应

    当一个页面内容比较多时我们首先可能考虑用Tab Control,但如果有很多页面内容需要动态加载则用CPropertySheet比较好点~ CPropertySheet有两种不同的显示模式.一种就是向 ...

  10. 不能使用ASP.NET验证控件---WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping。请添加一个名为 jquery (区分大小写)的 ScriptRes

    方法一: 在webconfig中找到 <appSettings>        <add key=" aspnet:UseTaskFriendlySynchronizati ...