参考链接:http://blog.csdn.net/xubin341719/article/details/38584469

1.android 手机的低功耗蓝牙,又称BLE ;BLE在andriod 4.3 以上才支持,又称蓝牙4.0,区别于经典蓝牙,BLE 低功耗,手机是否支持低功耗蓝牙,主要取决于手机硬件,所以使用前,需要先进行判断,是否支持低功耗蓝牙

2.蓝牙的使用,

  1.判断mobile 是否有低功耗蓝牙,返回值boolean

mainActivity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)

  2.获取bluetooth的adapter,adapter为null,证明设备无蓝牙

 mDefaultAdapter = BluetoothAdapter.getDefaultAdapter();

  3.设备是否已经开启蓝牙

mDefaultAdapter.isEnabled()

  4.打开蓝牙,开启蓝牙有两种方式,第一种,通过intent开启,会给用户以提示。

Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
mainActivity.startActivityForResult(intent, OPEN_BLUETOOTH_REQUEST_CODE); //在activity中获取开启蓝牙的反馈
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
switch (requestCode){
case OpenBluetoothPresenter.OPEN_BLUETOOTH_REQUEST_CODE:
if(requestCode==RESULT_OK){ //result_ok 是activity中的常量,为-1,说明操作成功了
mOpenBluetoothPresenter.scanDevice();
}else{
ToastUtils.showMessage(this,getString(R.string.connect_device_need_open_bluetooth));
} break;
} }

  

  第二种方式,这种方式会直接开启蓝牙,但是不能监听到用户是否真的开启了蓝牙;但是可以通过广播监听蓝牙状态的变化,建议非系统应用,采用第一种开启方式

 mDefaultAdapter.enable();

  5.扫描设备,因为我所使用的低功耗设备,而且只需要扫描低功耗设备,我采用的是startLeScan(),改方法过时了,建议采用bluetoothAdapterScanner.startScan,但是这个函数,要求sdk>23,而我需要支持的18,所以仍采用startLeScan

 mDefaultAdapter.startLeScan(this);

  6.扫描的结果,回调OnLEScan里面,改方法是实现接口:BluetoothAdapter.LeScanCallback

@Override
public void onLeScan (BluetoothDevice device, int rssi, byte[] scanRecord) {
boolean isDeviceFinded = false;
String deviceName = device.getName();
if (deviceName != null && deviceName.length() > 0 /*&& deviceName.startsWith("M")*/) {
if(devices==null){
devices = new ArrayList<>();
devices.add(device);
}else{
for (BluetoothDevice bluetoothDevice : devices) {
if(bluetoothDevice.getName().equals(deviceName)){
isDeviceFinded = true;
break;
}
} if(!isDeviceFinded){
devices.add(device);
} openBluetoothView.notifyDeviceList(devices);
}
}
}

  7,停止搜索

 mDefaultAdapter.stopLeScan(OpenBluetoothPresenter.this);

  8.连接设备,连接设备是通过bluetoothdevice的address进行连接,但是连接过程中,还是要进行一系列的判断,进行优化

if (mDefaultAdapter == null || address == null) {
ToastUtils.showMessage(mainActivity,mainActivity.getString(R.string.no_connect_device));
return;
} BluetoothDevice remoteDevice = mDefaultAdapter.getRemoteDevice(address);
if (remoteDevice == null) {
ToastUtils.showMessage(mainActivity,mainActivity.getString(R.string.connect_fail_no_device));
return;
} remoteDevice.connectGatt(mainActivity,false,this);

  9.连接的结果,会返回到BluetoothGattCallback里面,BluetoothGAttCallback是一个抽象类,里面的函数意义,:

/**
* 回调指示,
*
* @param gatt gatt客户端
* @param status 连接或者断开操作的状态,返回是BlutoothGatt 操作是否成功
* @param newState 新连接的状态,断开,或者连接bluetprofile state_connect,state_disconnect
*/
@Override
public void onConnectionStateChange (BluetoothGatt gatt, int status, int newState) {
Log.e(TAG, "onConnectionStateChange: status" + status + " status" + BluetoothGatt.GATT_SUCCESS + newState + ">>" + BluetoothProfile.STATE_CONNECTED);
} /**
* 连接设备的services,characteristrics,desriptors 更新的时候调用
*
* @param gatt gatt客户端
* @param status 连接设备已经被探索成功,是BluetoothGatt.Gatt_successfuly
*/
@Override
public void onServicesDiscovered (BluetoothGatt gatt, int status) {
Log.e(TAG, "onServicesDiscovered: " + status + " sss" + BluetoothGatt.GATT_SUCCESS);
} /**
* 读到特征值得结果
*
* @param gatt 客户端
* @param characteristic 特征值
* @param status 读的操作完成后,status 是BluetoothGatt.Gatt_Success
*/
@Override
public void onCharacteristicRead (BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
Log.e(TAG, "onCharacteristicRead: " + status);
} /**
* 设备的特征值变化的时候,该函数被触发
*
* @param gatt 客户端
* @param characteristic 变换后的特征值
*/
@Override
public void onCharacteristicChanged (BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Log.e(TAG, "onCharacteristicChanged: 调用了");
}
/**
* 在调用mBluetoothGatt.writeCharacteristic()函数之后,接收该函数操作的结果
* @param gatt 客户端
* @param characteristic 特征值,这个特征值是从远程设备返回的之前写入的值,应用程序应该进行对比,如果不正确,进行操作
* @param status 如果写入成功,这个返回的BluetoothGatt 的Gatt_success
*/
@Override
public void onCharacteristicWrite (BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
}

  10.remoteDevice connectGatt()会返回一个GAtt对象,gatt connect方法会重新连接一个已经连接过的连接

 BluetoothGatt mBluetoothGatt = remoteDevice.connectGatt(mainActivity, false, this);
//如果与远程设备的连接断开,调用该函数会重新进行连接,如果设备不在连接范围内,
//会等设备出现在连接范围内后,调用一次,进行连接
mBluetoothGatt.connect();

  11.向蓝牙发送指令,通过的是串口服务

 //当write操作完成后,会回调BluetoothGattCallback的onCharacteristicWrite函数,回报操作的结果,
//返回true,写入成功
boolean b = mBluetoothGatt.writeCharacteristic(rxServiceChar);

  12.开启Characteritics特征

 //开启或者关闭指定的特征值,Boolean 参数即为开启或关闭操作,该函数返回值,是操作是否成功
//返回的结果会在BluetoothGattCallback里面的onCharacteriticsChang调用
mBluetoothGatt.setCharacteristicNotification(characteristic,true);

  13.获取远程设备的所有服务

mBluetoothGatt.getServices();

  14.断开连接

public void disconnect(){
if(mDefaultAdapter==null||mBluetoothGatt==null){
Log.d(TAG, "disconnect: mDefaultAdapter或者 mBluetoothGatt为null,无法关闭");
return;
} mBluetoothGatt.disconnect();
}

  15.清除对象

 public void close(){
if(mBluetoothGatt == null){
Log.d(TAG, "close: mBluetoothGatt 位null,已经close");
return;
} mOldBluetoothAddress = null;
mBluetoothGatt.close();
mBluetoothGatt = null;
}

  

android 低功耗蓝牙使用的更多相关文章

  1. 【转】Android低功耗蓝牙应用开发获取的服务UUID

    原文网址:http://blog.csdn.net/zhangjs0322/article/details/39048939 Android低功耗蓝牙应用程序开始时获取到的蓝牙血压计所有服务的UUID ...

  2. Android低功耗蓝牙(BLE)使用详解

    代码地址如下:http://www.demodashi.com/demo/13390.html 与普通蓝牙相比,低功耗蓝牙显著降低了能量消耗,允许Android应用程序与具有更严格电源要求的BLE设备 ...

  3. 使用BleLib的轻松搞定Android低功耗蓝牙Ble 4.0开发具体解释

    转载请注明来源: http://blog.csdn.net/kjunchen/article/details/50909410 使用BleLib的轻松搞定Android低功耗蓝牙Ble 4.0开发具体 ...

  4. Android低功耗蓝牙(BLE)开发的一点感受

    最近一段时间,因为产品的需要我做了一个基于低功耗蓝牙设备的Android应用,其中碰到了一些困难,使我深深体会到Android开发的难处:不同品牌,不同型号和不同版本之间的差异使得Android应用适 ...

  5. Android 低功耗蓝牙BLE 开发注意事项

    基本概念和问题 1.蓝牙设计范式? 当手机通过扫描低功耗蓝牙设备并连接上后,手机与蓝牙设备构成了客户端-服务端架构.手机通过连接蓝牙设备,可以读取蓝牙设备上的信息.手机就是客户端,蓝牙设备是服务端. ...

  6. Android低功耗蓝牙(蓝牙4.0)——BLE开发(上)

    段时间,公司项目用到了手机APP和蓝牙设备的通讯开发,这里也正好对低功耗蓝牙(蓝牙4.0及以后标准)的开发,做一个总结. 蓝牙技术联盟在2010年6月30号公布了蓝牙4.0标准,4.0标准在蓝牙3.0 ...

  7. Android低功耗蓝牙总结

    这里只列出重点原理内容,更加细节的内容请阅读前面文章 首先要搞清楚一点,我们在 Android 中通过 SDK 获得的蓝牙广播包是经过底层的 SDK 给我们处理过的,是一个长度为 62 的字节数组.这 ...

  8. Android 低功耗蓝牙的多设备连接与数据接收,简单实现

    在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...

  9. Android BLE蓝牙详细解读

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

随机推荐

  1. 用JAVA制作微型操作系统4月23日情况

    弄好了一个自认为十分精美的界面,但本想着昨天就在开始按钮上先套入控制jp222面板上的jb2标签上的时间更新,这按钮起到开始线程的作用(我认为按钮应该可以通过t.start()来触发线程,结果不知为什 ...

  2. 深入理解JAVA I/O系列二:字节流详解

    流的概念 JAVA程序通过流来完成输入/输出.流是生产或消费信息的抽象,流通过JAVA的输入输出与物理设备链接,尽管与它们链接的物理设备不尽相同,所有流的行为具有相同的方式.这样就意味一个输入流能够抽 ...

  3. 蜗牛慢慢爬 LeetCode 5.Longest Palindromic Substring [Difficulty: Medium]

    题目 Given a string s, find the longest palindromic substring in s. You may assume that the maximum le ...

  4. cxDBTreelist一些使用方法

    一.导出EXCEL   TXT   HTML: uses cxTLExportLink; cxExportTLToEXCEL(dm.SaveDialog.FileName,cxDBTreeList1, ...

  5. WDS迁移注意事项

    先说背景:公司使用WDS来部署操作系统,目前DHCP和WDS都安装在同一台服务器上,但是此服务器已过保,所以筹划迁移,将WDS和DHCP分别迁移到两台服务器上.迁移计划是保持WDS暂时不动,DHCP先 ...

  6. [微软官网] SQLSERVER 执行页面还原

    执行页面还原 https://docs.microsoft.com/zh-cn/previous-versions/sql/sql-server-2008-r2/ms175168(v=sql.105) ...

  7. [转帖]七牛云对HTTPS 的解释

     感觉对RTT 还有 建立连接的说明挺好的 转帖一下 学习   https://www.cnblogs.com/qiniu/p/6856012.html   序•魔戒再现   几天前,OpenSSL  ...

  8. 斑马条码打印机GK888T打印标签是间隔的 ,不是连续的

    有间断的标签纸和连续的标签纸是两种不同的纸张类型, 打印机的标签感应器需要工作在不同的模式来跟踪感应它们. 打印机正确感应纸张才不会红灯闪烁,打印的内容才按文件设计打印到标签的对应位置上. 所以要在驱 ...

  9. MySQL的间隙锁

    什么是间隙锁当我们用范围条件而不是相等条件检索数据,并请求共享或排他锁时,InnoDB会给符合条件的已有数据记录的索引项加锁:对于键值在条件范围内但不存在的记录,叫做“间隙(GAP)”,InnoDB也 ...

  10. oracle表空间到32G后扩容

    ), ) total_space FROM dba_data_files ORDER BY tablespace_name; /*查看表空间的使用情况*/ select a.a1 表空间名称, tru ...