【android 蓝牙开发——BLE(低功耗)蓝牙 2021-10-09更新】

本篇博客,接着上一篇博客 【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<BluetoothGattService> gattServices) {  
    if (gattServices == null)  
        return;  
    for (BluetoothGattService gattService : gattServices) { // 遍历出gattServices里面的所有服务  
         //通过遍历可以在这里拿到自己想要的那个服务的uuid
        List<BluetoothGattCharacteristic> 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智能硬件开发详解

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值