最近一直在研究一个蓝牙功能 由于本人是菜鸟  学起来比较忙 一直搞了好久才弄懂 , 网上对蓝牙4.0也就是几个个dome 抄来抄去,全是英文注解 , 对英语不好的朋友来说

真是硬伤 , 一些没必要的描述罗里吧嗦 , 关键的方法接口 一笔带过 .........算了不吐槽了.我就介绍一下我最近的学习心得吧 ,简单的什么开启  蓝牙 搜索蓝牙什么的我就不说了 你们百度一下 android 蓝牙 4.0 一大坨.看完再来看我的博客也行  ,我就介绍点 网上那些 一笔带过 比较重要的接口回调 之类的.

蓝牙 中具体有些什么东西:  蓝牙名字 (name) 蓝牙地址(adress)  蓝牙信号强度 (riss) 蓝牙服务(service) 蓝牙服务下面又有小服务  也就是具体要做的   这些应该就是我们常用的 其他的我也没用过 也不知道.嘿嘿

蓝牙通讯过程 :  打开 查找(可以指定uuid查找设备) 连接  就不说了;连接之后  首先 获取 bluetooth service  (通过UUID 查找的)  然后再找bluetooth service 里面的小服务 叫 bluetooth

Characteristic  (也是通过UUID获取的)  之后你就可以 对其进行读写了 ,读写有三种情况 说了好像也没什么用 根据自己的需求吧 ; 

下面就介绍一下
BluetoothGattCallback   网上都是一笔带过根本没有什么解释的  找了好久表示好心累然后自己用设备一个一个方法调:下面贴代码 加注释里面是有发送数据的:
BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {

    @Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
//连接状态触发
        if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.d("Bluetooth", "-------ConnectSucceed------");
bluetoothGatt.discoverServices();//发现服务
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.d(" Bluetooth", "-------Disconnect------");
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
bluetoothAdapter = bluetoothManager.getAdapter();
bluetoothAdapter.enable();
bluetoothAdapter.startLeScan(uuid, leScanCallback);
}
}
} /**
* 发现设备服务回调
*/
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
final byte[] buffer = new byte[5];
String open = "open=1";//发送的数据
try {
byte[] data = open.getBytes("UTF-8");
writeDataToDevice(data);//发送(单独一个方法)
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
} @Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.d("000", "--------发送数据成功 ----- status:");
}
} @Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
Log.d("蓝牙回复数据为", characteristic.getValue().toString());
} @Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { }
.....
};
/**
* 向设备发送数据
*/
public void writeDataToDevice(byte[] data) {
if (bluetoothGatt == null) {
Log.d("00 ", "----------------BluetoothGatt=null-----------------------");
return;
} bluetoothGattService = bluetoothGatt
.getService(BLURTOOTH_SERVICE);//蓝牙设备中需要使用的服务的UUID
if (bluetoothGattService == null) {
Log.d("pcg", "service not found!");
bluetoothGatt.disconnect();
return;
} BluetoothGattCharacteristic mCharac = bluetoothGattService
.getCharacteristic(BLUETOOTH_CHARACTERISTIC);//需要使用该服务下具体某个特性的UUID
if (mCharac == null) {
//没有找到的情况
bluetoothGatt.disconnect();
return;
}
bluetoothGatt.setCharacteristicNotification(mCharac, true);
Log.d("pcg", "data:" + Arrays.toString(data));
mCharac.setValue(data); //设置需要发送的数据
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
writeCharacteristic(mCharac);
}

就介绍 到这里 吧  看完这个下载个dome  看一下 会清晰一点 .  搞开发还是 看看好  不要去拷贝人家的  ,  看懂了就用的灵活点 ,做这个功能发现全天下的dome 都是一个人写的似的 全是英文  开始怀疑大家都只是拷贝官方的dome  连交互都是一大坨的广播发来发去  真是看都不愿看.就介绍到这里  ,  写的不好 勿喷!

android 蓝牙4.0 开发介绍的更多相关文章

  1. iOS蓝牙4.0开发

    文/starfox寒流(简书作者)原文链接:http://www.jianshu.com/p/974d165f78b5著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. iOS 蓝牙4.0 ...

  2. Android 蓝牙4.0 BLE

    Android ble (Bluetooth Low Energy) 蓝牙4.0,也就是说API level >= 18,且支持蓝牙4.0的手机才可以使用. BLE是蓝牙4.0的核心Profil ...

  3. Android 蓝牙4.0 BLE (onServicesDiscovered 返回 status 是 129,133时)

    Android ble (Bluetooth Low Energy) 蓝牙4.0,也就是说android 4.3+, API level >= 18,且支持蓝牙4.0的手机才可以使用. BLE是 ...

  4. ym——物联网入口之中的一个Android蓝牙4.0

    转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 假设还有同学不知道蓝牙4.0能够做什么请查看Android+蓝牙 4.0 将带来什么? ...

  5. Android蓝牙串口程序开发

    本文主要介绍了针对android的蓝牙串口上位机开发. 程序下载地址:点击打开链接 一.帧定义 androidclient依照一定的数据帧格式通过蓝牙串口发送数据到连接到MCU的蓝牙从机.MCU接收到 ...

  6. android 蓝牙4.0多通道

    很久没记录东西了,前段时间研究了一哈android4.0控制多个外设的情况,注意,需要使用android版本4.3以上,蓝牙4.0及以上. 我这里使用的控制蓝牙灯泡,使用android4.3的手机,手 ...

  7. iOS 蓝牙4.0开发

    背景: 1.iOS的蓝牙不能用来传输文件.2.iOS与iOS设备之间进行数据通信,使用gameKit.framework3.iOS与其他非iOS设备进行数据通信,使用coreBluetooth.fra ...

  8. Android 蓝牙4.0的连接和通讯

    1.加入权限 <uses-sdk android:minSdkVersion=" android:targetSdkVersion="/> <uses-featu ...

  9. Android蓝牙通信功能开发

    1. 概述 Bluetooth 是几乎现在每部手机标准配备的功能,多用于耳机 mic 等设备与手机的连接,除此之外,还可以多部手机之间建立 bluetooth 通信,本文就通过 SDK 中带的一个聊天 ...

随机推荐

  1. Solr4.4的安装与配置

    最近准备用Solr搭建项目,所以对其作了一些了解,我采用的是Solr4.4版本:这个版本的Solr相对于以前的版本改变很大,这里记一下自己安装与配置的过程. 网上很多关于Solr的教程都很老了,很多教 ...

  2. 改进的sqlhelper学习日志

    下面就是详细的sqlhelper的代码了 using System; using System.Collections.Generic; using System.Linq; using System ...

  3. cmd命令行设置环境变量

    http://blog.sciencenet.cn/blog-51026-566742.html 1.查看当前所有可用的环境变量:输入 set 即可查看. 2.查看某个环境变量:输入 “set 变量名 ...

  4. Linux学习笔记10——文件I/O之一

    UNIX系统中的大多数文件I/O只需要用到5个函数:open,read,write,lseek以及close 文件描述符 文件描述符是一个非负整数,所有打开的文件都通过文件描述符引用 文件描述符的变化 ...

  5. 【转】分析器窗口 Profiler window

    转自unity圣典: http://game.ceeger.com/Manual/ProfilerWindow.html http://game.ceeger.com/Manual/Profiler. ...

  6. 对C++进行优化了的android-ndk-r6-crystax-2

    https://www.crystax.net/en/android/ndk/6?id=6 Here is customized distribution of Android NDK r6 whic ...

  7. Call-time pass-by-reference has been deprecated

    Warning: Call-time pass-by-reference has been deprecated解决方法 第一种方法: 修改php.ini就可以了. 1. 在PHP.ini中搜索关键字 ...

  8. win7不能在同一窗口打开文件夹,解决办法

    1.由于IE浏览器的主页被劫持,总是忽然弹出搜狗的主页,有的时候,忽然弹出IE浏览器(主页是搜狗),然后又自行关闭,我X,我的电脑竟然不受我控制,这可得了,这里我又要骂宁美国度了,made,组装机装了 ...

  9. c#基础语言编程-正则表达式基础

    引言 正则表达式是一种用高度抽象的字符串来描述字符串特征,进而实现对字符串的匹配.提取.替换等等.正则表达式(以下简称正则,Regex)通常不独立存在,各种编程语言和工具作为宿主语言提供对正则的支持, ...

  10. IOS图标尺寸一览

    iPhone专用程序: 图标名称 大小 圆角 用途 必需 Icon.png 57 X 57 10px 用于程序商店和在iPhone/iPod Touch中显示 必需 Icon@2x.png 114 X ...