怎么介绍呢,一直考虑,还是从业务流程开始,1、Meshservice初始化--》2、扫描设备---》3、连接设备--》4、扫描设备--》5、设备组网

这样分析多初学者,或者刚接触CSRdemo的比较容易!

1、从MainActivity开始,第一步是在onCreate里面初始化蓝牙mesh的入口

AndroidManifest配置文件中有一下代码,不然启动MeshService失败。

<service
    android:name="com.csr.csrmesh2.MeshService"
    android:enabled="true"
    android:exported="false" >
</service>
MeshLibraryManager.initInstance(getApplicationContext(), MeshLibraryManager.MeshChannel.BLUETOOTH,LogLevel.DEBUG);

2、在MeshLibraryManager中进行创建service连接

private MeshLibraryManager(Context context, MeshChannel channel,LogLevel lgLevel) {Log.e(TAG,"2==init===MeshLibraryManager()");// Log message has a tag and a priority associated with it.
    logLevel= lgLevel;App.bus.register(this);mContext = new WeakReference<Context>(context);mCurrentChannel = channel;Intent bindIntent = new Intent(mContext.get(), MeshService.class);context.bindService(bindIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
}

3、启动蓝牙,扫描蓝牙设备进行连接,重点是setBluetoothBeareEnabled接口

//激活蓝牙
private void enableBluetooth() {Log.e(TAG,"4==enable===enableBluetooth()");mCurrentChannel = MeshChannel.BLUETOOTH;isChannelReady = false;//广播“通道没有准备好”
    App.bus.post(new MeshSystemEvent(MeshSystemEvent.SystemEvent.CHANNEL_NOT_READY));mBtBearer = new BluetoothBearer(this, mContext.get());//启动蓝牙承载*****
    mService.setBluetoothBearerEnabled(mBtBearer);//设置这个开始扫描蓝牙设备
}
在MeshLibraryManager中有一个MeshApiMessageHandler内部做消息处理,个人感觉有路由意思在里面meshService
都是通过这个handler消息反馈出来,还有一个BluetootHandler这个也是消息处理,后面重点结束这二个handler的关系

4、这个时候蓝牙设备已经连接上了,可以进行组网了

在demo中是这样MeshLibraryManager.getInstance().getMeshService().setDeviceDiscoveryFilterEnabled(true)
在meshService中的传入handler上报蓝牙设备数据
case DEVICE_UUID: {ParcelUuid uuid = event.data.getParcelable(MeshConstants.EXTRA_UUID);int uuidHash = event.data.getInt(MeshConstants.EXTRA_UUIDHASH_31);int rssi = event.data.getInt(MeshConstants.EXTRA_RSSI);int ttl = event.data.getInt(MeshConstants.EXTRA_TTL);boolean existing = false;for (ScanDevice info : mDiscoveredDevices) {if (uuid != null && info.uuid.equalsIgnoreCase(uuid.toString())) {info.rssi = rssi;info.ttl = ttl;// check if we already have appearance info according with the uuidHash
            if (mAppearances.containsKey(uuidHash)) {info.setAppearance(mAppearances.get(uuidHash));}info.updated();updateList();existing = true;break;}}if (!existing) {ScanDevice info = new ScanDevice(uuid.toString().toUpperCase(), rssi, uuidHash, ttl);// check if we already have appearance info according with the uuidHash
        if (mAppearances.containsKey(uuidHash)) {info.setAppearance(mAppearances.get(uuidHash));}mDiscoveredDevices.add(info);updateList();}break;
}
case DEVICE_APPEARANCE: {byte[] appearance = event.data.getByteArray(MeshConstants.EXTRA_APPEARANCE);String shortName = event.data.getString(MeshConstants.EXTRA_SHORTNAME);int uuidHash = event.data.getInt(MeshConstants.EXTRA_UUIDHASH_31);for (ScanDevice info : mDiscoveredDevices) {if (info.uuidHash == uuidHash) {info.setAppearance(new AppearanceDevice(appearance, shortName));info.updated();}}mAppearances.put(uuidHash, new AppearanceDevice(appearance, shortName));Log.d(TAG, "* Association Event DEVICE_APPEARANCE - mAppearances: " + mAppearances.toString());updateList();break;
}

5、有蓝牙设备进行授权组网

MeshLibraryManager.getInstance().getMeshService().associateDevice(deviceHash,authcode,isauthocode,deviceId)

A、蓝牙设备授权进度

case ASSOCIATION_PROGRESS: {int progress = event.data.getInt(MeshConstants.EXTRA_PROGRESS_INFORMATION);if (!event.data.getBoolean(MeshConstants.EXTRA_CAN_BE_CANCELLED)) {runOnUiThread(new Runnable() {@Override
            public void run() {if (mDialog != null) {mDialog.getButtonCancel().setEnabled(false);}}});}updateProgress(getPercentageOf(progress, ASSOCIATION_COMPLETE_PERCENT));break;
}B、授权组网成功
case DEVICE_ASSOCIATED: {/* Once association is completed we are approximately half way through the process.
       We still need to query lots of info from the device using MCP.
       The remaining 100 - ASSOCIATION_COMPLETE_PERCENT of the progress is used for that.*/
    updateProgress(ASSOCIATION_COMPLETE_PERCENT);int deviceId = event.data.getInt(MeshConstants.EXTRA_DEVICE_ID);int uuidHash = event.data.getInt(MeshConstants.EXTRA_UUIDHASH_31);byte[] dhmKey = event.data.getByteArray(MeshConstants.EXTRA_RESET_KEY);mTempDevice = new UnknownDevice();mTempDevice.setDeviceID(deviceId);mTempDevice.setDeviceHash(uuidHash);mTempDevice.setDmKey(dhmKey);mTempDevice.setPlaceID(Utils.getLatestPlaceIdUsed(this));mTempDevice.setAssociated(true);AppearanceDevice appearanceDevice = mAppearances.get(uuidHash);/* Set to 2 as we always have to query model low and high. We may or may not need appearance and
       the number of models to query for groups is variable.*/
    mNumPostAssociationSteps = 2;mPostAssociationStep = 0;if (appearanceDevice != null) {mTempDevice.setName(appearanceDevice.getShortName().trim() + " " + (deviceId - Constants.MIN_DEVICE_ID));mTempDevice.setAppearance(appearanceDevice.getAppearanceType());// Request model low from the device.
        mCurrentRequestState = DeviceInfo.MODEL_LOW;ConfigModel.getInfo(mTempDevice.getDeviceID(), DeviceInfo.MODEL_LOW);}else {// An extra step as we don't have the appearance yet.
        mNumPostAssociationSteps++;mTempDevice.setName(getString(R.string.unknown) + " " + (deviceId - Constants.MIN_DEVICE_ID));// Request appearance from the device.
        mCurrentRequestState = DeviceInfo.APPEARANCE;ConfigModel.getInfo(mTempDevice.getDeviceID(), DeviceInfo.APPEARANCE);}mTempDevice = mDeviceManager.createOrUpdateDevice(mTempDevice, true);break;
}

以上代码根据csrdemo2.1中copy,其实原理和1.3版本是一样的,那为什么看到代码发现比较复杂呢,

个人分析原型有二点,第一点:demo除了灯控模块还有集成了灯具、分组、情景、网关、云、sensor等模块,demo还增加了palce和area概念。

如有不足之处,希望各位大牛指正。对想了解这套系统的同行,希望对你们有帮助。

QQ:172620790  一起沟通交流

如果需要了解后期内容,请增加评论,我会继续加油!

												

【CSRMesh2.1蓝牙开发】-Android demo介绍【二】之代码分析相关推荐

  1. 【鸿蒙OS开发入门】06 - 启动流程代码分析之KernelOS:之启动Linux-4.19 Kernel内核 启动init进程

    [鸿蒙OS开发入门]06 - 启动流程代码分析之KernelOS:之启动Linux-4.19 Kernel内核 一.head.S 启动start_kernel() 1.1 start_kernel() ...

  2. 【鸿蒙OS开发入门】13 - 启动流程代码分析之第一个用户态进程:init 进程 之 init 任务详解

    [鸿蒙OS开发入门]13 - 启动流程代码分析之第一个用户态进程:init 进程 之 init 任务详解 一. /etc/init.cfg 系统默认cfg:启动lo回环网卡 1.1 init.Hi35 ...

  3. 团队项目开发“编码规范”之九:代码分析

    团队项目开发"编码规范"之九: 代码分析 发布日期:2011年3月17日星期三作者:EricHu                                           ...

  4. VS 2015 开发Android底部导航条----[实例代码,多图]

    1.废话背景介绍  在Build 2016开发者大会上,微软宣布,Xamarin将被整合进所有版本的Visual Studio之中. 这也就是说,Xamarin将免费提供给所有购买了Visual St ...

  5. 华为软件开发云测评报告二:代码检查

    相关文章:<华为软件开发云测评报告一:项目管理> 体验环境 体验方式:PC端 系统:Windows 64位 浏览器类型:Chrome浏览器 浏览器版本:58.0.3029.110 体验时间 ...

  6. GAT: 图注意力模型介绍及PyTorch代码分析

    文章目录 GAT: 图注意力模型介绍及代码分析 原理 图注意力层(Graph Attentional Layer) 情境一:节点和它的一个邻居 情境二:节点和它的多个邻节点 聚合(Aggregatio ...

  7. android中基于蓝牙开发的demo

    今儿闲着无聊,重新浏览android中sdk重的sample中的demo,觉的BluetoothChat写的不错,就把它搬到这里,以方便查看和学习. 主显示界面activity: /** Copyri ...

  8. android低耗能蓝牙开发,Android BLE低功耗蓝牙开发

    最近做了一个智能硬件开发(针灸仪)的项目,有一部分涉及到低功耗蓝牙的开发,就是通过蓝牙和设备进行数据的交互,比如控制改设备的LED的开关,设备的开关机,设置设备的时间和温度等,下面就项目中遇到的坑一一 ...

  9. Kotlin 开发Android app(十二):Android布局FrameLayout和ViewPager2控件实现滚动广告栏

    在上一节中我们简单的介绍了RecyclerView 的使用,他是整个开发的重点控件,这一节我们来看看FrameLayout 布局结合ViewPager2,开发一个广告控件. 新模块banner 先创建 ...

最新文章

  1. 搜索算法,一触即达:GitHub上有个规模最大的开源算法库
  2. python文件不存在时创建文件_python-创建一个文件(如果不存在)
  3. 编译通过PCL1.5.1的第一个例子图解
  4. C++学习-环境配置
  5. VMware虚拟机网络模式详解 NAT模式
  6. 手把手教你报表工具PentahoBI安装和简单使用
  7. visio调整形状位置_VISIO绘图技巧—三相桥式全控整流电路绘制
  8. 【转】如何用好SVN的Branch
  9. 论文浅尝 - TACL2020 | TYDI QA:Google 发表一个多语言的问答语料库
  10. java unit test怎么写_Java J Unit Test
  11. ERROR: ld.so: object '/usr/lib64/libtcmalloc.so.4' from LD_PRELOAD cannot be preloaded: ignored
  12. 【学堂在线数据挖掘:理论方法笔记】第四天(3.28)
  13. MySQL多字节字符集造成主从数据不一致问题
  14. android foobar wifi,foobar2000安卓
  15. 【Object C】从Java 一步步走向Object C
  16. 人工智能、机器学习、神经网络及深度学习关系
  17. react native之修改APP的名称和图标
  18. ubuntu 16.04下设置静态IP地址
  19. c++判断某一天是这一年的第几天
  20. android车载系统测试,一种车载Android多媒体主机的自动测试方法和系统与流程

热门文章

  1. SQL中CharIndex函数、InStr 函数、PatIndex函数、Stuff函数区别与作用
  2. 最全MBTI人格测试
  3. Android ComponentName的使用
  4. [zz]什么是网关?网关的作用
  5. ubuntu修改网卡名
  6. C语言创建删不掉的文件夹
  7. 前端模拟面试字数过23477万内容|刷题打卡
  8. Arduino制作天猫精灵朋友天狗精灵-LD3320语音模块
  9. 【黑马程序员】 MyEclipse 快捷键大全,麻麻再也不用担心我打字慢了 --By寻找资拥
  10. Illustrator 教程:如何在 Illustrator 中对齐内容?