本篇博客,接着上一篇博客 【android 蓝牙开发——传统蓝牙】,讲一下低功耗蓝牙的开发。

着重说明一下:在大于等于4.3 和 小于5.0 之间的android手机系统版本,只支持作为中心设备角色,也就是说如果你的android手机是4.4的版本,你开发的软件,那就只能搜索到一些外围设备,例如:智能手环一类的东西,而不能做为外围设备被其他中心设备搜索到;在 5.0以及5.0 之后,既可以作为中心设备,也可以作为外围设备,这个在以后博客中继续讲解。

同样也都是一样的套路,在下面的 相关博客链接 中,人家都将的很清楚啦。也不再这里在重新说一边了。只把一些需要注意的地方说一下,希望大家和自己以后能避免。

大概步骤:

  1. 打开蓝牙
  2. 扫描设备
  3. 连接设备
  4. 写入数据,控制设备
  5. 读取设备返回的数据
  6. 断开设备
  7. 关闭蓝牙

注意事项:

一:

//搜索的操作最好放在Activity的onResume里面或者服务里面,我有发现放在onCreate有时响应不及时,而且人家google Daemon 也是放在onResume里面的。    private void scanLeDevice(final boolean enable) {        if (enable) {            mScanning = true;            Log.i(TAG, "scanLeDevice: 开始搜索");            mBluetoothAdapter.startLeScan(mLeScanCallback); //开始搜索            mHandler.postDelayed(new Runnable() {                @Override                public void run() {                    mScanning = false;                    mBluetoothAdapter.stopLeScan(mLeScanCallback);                    Log.i(TAG, "scanLeDevice: 搜索停止");                    deviceAdapter.notifyDataSetChanged();                }            }, SCAN_PERIOD); //10秒后停止搜索        } else {            mScanning = false;            mBluetoothAdapter.stopLeScan(mLeScanCallback);//停止搜索        }    }

二:这个方法很关键

private void displayGattServices(List gattServices) {      if (gattServices == null)          return;      for (BluetoothGattService gattService : gattServices) { // 遍历出gattServices里面的所有服务           //通过遍历可以在这里拿到自己想要的那个服务的uuid        List gattCharacteristics = gattServices.getCharacteristics();          for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { // 遍历每条服务里的所有Characteristic              if (gattCharacteristic.getUuid().toString().equalsIgnoreCase(需要通信的UUID)) {                   // 有哪些UUID,每个UUID有什么属性及作用,一般硬件工程师都会给相应的文档。我们程序也可以读取其属性判断其属性。                  // 此处可以可根据UUID的类型对设备进行读操作,写操作,设置notification等操作                  // BluetoothGattCharacteristic gattNoticCharacteristic 假设是可设置通知的Characteristic                  // BluetoothGattCharacteristic gattWriteCharacteristic 假设是可读的Characteristic                  // BluetoothGattCharacteristic gattReadCharacteristic  假设是可写的Characteristic              }          }      }  }  

三:服务的绑定处理,为什么是这样,你要查阅一下服务的绑定知识。

 private final ServiceConnection mServiceConnection = new ServiceConnection() {        @Override        public void onServiceConnected(ComponentName componentName, IBinder service) {            Log.i(TAG, "onServiceConnected: mDeviceAddress=" + mDeviceAddress);            isBond = true;            mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();            if (!mBluetoothLeService.initialize()) {                Log.e(TAG, "Unable to initialize Bluetooth");                finish();            }            mBluetoothLeService.connect(mDeviceAddress);        }        @Override        public void onServiceDisconnected(ComponentName componentName) {            Log.i(TAG, "onServiceDisconnected: ");            mBluetoothLeService = null;            isBond = false;        }    };

在onDestroy() 或者 onStop() 要做一些解绑处理:

@Override    protected void onDestroy() {        super.onDestroy();        if (mGattUpdateReceiver != null) {            unregisterReceiver(mGattUpdateReceiver);//解绑广播        }        if (mBluetoothLeService != null) {            mBluetoothLeService.disconnect();  //断开连接        }        if (isBond) {            isBond = false;            unbindService(mServiceConnection);//解绑服务        }    }

源码下载

源码说明:后续会补上。


相关博客:

google 官方文件 比较具有参考价值

google 官方 Daemon android-BluetoothLeGatt

Android BLE蓝牙4.0开发详解

http://www.jianshu.com/c/f1805c68192b

Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现

Android:BLE智能硬件开发详解

更多相关文章

  1. 关于蓝牙设备之间共享网络的问题(android4.2)
  2. Android设备检测
  3. Android蓝牙BLE的详细讲解
  4. Android判断设备是手机还是平板
  5. Android设备的显示信息
  6. Android中获取设备的IP
  7. Make ADB To Support Android Devices(如何使ADB在Linux下支持And
  8. android 如何遍历Cursor
  9. android蓝牙开发入门到精通1---蓝牙基础

随机推荐

  1. Android从Linux系统启动
  2. Android系统信息获取 之十:移动网络相关信
  3. 「Android(安卓)Tips」解决 Mac OSX 无法
  4. Android应用商店——Splash页面的实现,And
  5. Android(安卓)Geocoder(位置解析)
  6. Android(安卓)使用grade实现Android(安卓
  7. Android开机自启动
  8. Android(安卓)Studio 使用小结
  9. zbar android sdk源码编译
  10. android 线性布局几个属性