当前位置: 首页 > news >正文

无锡做食品网站的公司北京网站页设计制作

无锡做食品网站的公司,北京网站页设计制作,做视频网站需要什么服务器配置,宠物网站设计说明书Harmony Ble 蓝牙App #xff08;一#xff09;扫描 前言正文一、创建工程二、工程配置① 权限配置② Debug配置③ UI配置 三、扫描① 扫描接口② 扫描类 四、业务处理① Slice的生命周期② 蓝牙开关和动态权限请求 五、扫描设备六、显示设备① 自定义蓝牙类② 提供者③ 显示… Harmony Ble 蓝牙App 一扫描 前言正文一、创建工程二、工程配置① 权限配置② Debug配置③ UI配置 三、扫描① 扫描接口② 扫描类 四、业务处理① Slice的生命周期② 蓝牙开关和动态权限请求 五、扫描设备六、显示设备① 自定义蓝牙类② 提供者③ 显示设备 七、源码 前言 关于Android的低功耗蓝牙我做了很多介绍了那么对于Harmony来说这一块我没有做过介绍而实际中我确实做过一个Harmony的BLE项目所以这里分享一些内容出来。 正文 在Harmony中进行Ble的蓝牙开发实际上和Android中类似但是又有一些不同因为Harmony的SDK还在不断的完善。而这里我们使用的是API 6进行项目开发使用的语言是Java至于为什么使用API 6而不是最新的API 9因为我买不起遥遥领先所以只能用API 6的HUAWEI P30进行真机测试。蓝牙这种APP一定是要使用真机测试的你用虚拟机是不行的话不多说我们开始吧。 一、创建工程 下面开始创建工程。 选择Empty Ability点击Next。我们创建一个名为HarmonyBle的项目语言为Java。 点击Finish完成创建。 默认的工程就是这个样子的是不是很像Android创建的工程呢 二、工程配置 ① 权限配置 Harmony中同样有权限这个概念也需要配置静态权限和动态权限只不过配置静态权限的地方不一样。Harmony是在config.json中里面的代码如下 {app: {bundleName: com.llw.ble,vendor: example,version: {code: 1000000,name: 1.0.0}},deviceConfig: {},module: {package: com.llw.ble,name: .MyApplication,mainAbility: com.llw.ble.MainAbility,deviceType: [phone,tablet,tv,wearable,car],distro: {deliveryWithInstall: true,moduleName: entry,moduleType: entry,installationFree: false},abilities: [{skills: [{entities: [entity.system.home],actions: [action.system.home]}],name: com.llw.ble.MainAbility,description: $string:mainability_description,icon: $media:icon,label: $string:entry_MainAbility,launchType: standard,orientation: unspecified,visible: true,type: page}]} }你阅读一下你就会发现这和Android的AndroidManifest.xml配置文件好像差不多啊。只不过一个用的是json一个用的是xml。 所以我们配置权限也是在config.json中例如扫描蓝牙时我们需要定位权限。可以在里面增加如下代码 reqPermissions: [{name: ohos.permission.LOCATION},{name: ohos.permission.USE_BLUETOOTH},{name: ohos.permission.DISCOVER_BLUETOOTH},{name: ohos.permission.MANAGE_BLUETOOTH}]如下图所示注意json中标点符号。 ② Debug配置 然后我们就应该要来写代码了不过在此之前我们先了解一下Ability和Slice的区别Ability就像一个画框而Slice就像一个画布。我们可以在一个画框里面加载多个画布就好像多个页面之前的跳转我们可以用Slice来进行下面我们增加一个扫描的Slice我们复制一下MainAbilitySlice再粘贴一下出现的弹窗中改名字 为什么要通过这种方式来创建Java文件呢因为DevEco Studio我创建不了Java文件可能是这个版本的DS没有这个选项亦或是我没有找到。 下面我们需要创建对应layout文件再resources/base/layout下创建一个slice_scan.xml代码如下 ?xml version1.0 encodingutf-8? DirectionalLayoutxmlns:ohoshttp://schemas.huawei.com/res/ohosohos:heightmatch_parentohos:widthmatch_parentohos:alignmentcenterohos:orientationverticalListContainerohos:id$id:lc_deviceohos:heightmatch_parentohos:widthmatch_parent//DirectionalLayout然后我们再修改ScanSlice中的内容让它加载我们刚写好的slice_scan.xml。修改onStart()方法代码如下所示 Overridepublic void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_slice_scan);}现在App打开之后默认会运行MainAbility我们看一下这个里面。 public class MainAbility extends Ability {Overridepublic void onStart(Intent intent) {super.onStart(intent);super.setMainRoute(MainAbilitySlice.class.getName());} }可以看到在setMainRoute()方法中默认加载的是MainAbilitySlice将它改为我们刚写好的ScanSlice代码super.setMainRoute(ScanSlice.class.getName()); 然后我们先运行一下看看通过USB链接到鸿蒙手机上。 这里会提示报错浏览一下错误信息。 这里是说我们需要配置Signing。点击Run下面或者右侧弹窗的Open signing configs会打开一个配置窗口如下图所示 我们点击Signing Configs选项需要你进行登录如下图所示 这里就需要你登录华为的帐号了我们当前在本地运行所以是Debug模式旁边有一个Release表示发布版本它里面配置的东西和Debug模式一致区别在于Debug模式下的配置信息只要我们登录之后DevEco Studio会帮助我们自动生成而Release中的信息则需要开发者去华为开发者官网上去创建应用并申请配置文件和证书比较麻烦但是如果你要上架应用则必须做这一步在国内华为应用市场上架应用是最严格的。华为的你搞得定其他的都是小趴菜不值一提。 下面我们先登录会打开一个网页登录成功之后你会看到这样的页面。 然后我们回到DS就会自动配置Debug模式下的证书和配置文件如下图所示 点击OK会在DS中进行一个配置配置好之后你可以在工程目录下的build.gradle中看到debug的相关信息如下图所示。 然后我们再运行一下看看这一次毫无疑问是可以运行成功的。如下图所示 ③ UI配置 可以看到默认的标题栏就如同Android默认的ActionBar丑的很特别我们去掉它在config.json中添加如下代码 metaData: {customizeData: [{extra: ,name: hwc-theme,value: androidhwext:style/Theme.Emui.Light.NoTitleBar}]},添加位置如下所示 下面我们再运行一下看看。 是不是Unbelievable! 同样为了标题好看我们在element下创建一个color.json里面的代码如下所示 {color: [{name: white,value: #FFF},{name: black,value: #000},{name: blue,value: #FFA7D3FF},{name: bg_color,value: #F8F8F8},{name: gray,value: #989898}] }我们再修改一下scan_slice.xml中的代码如下所示 ?xml version1.0 encodingutf-8? DirectionalLayoutxmlns:ohoshttp://schemas.huawei.com/res/ohosohos:heightmatch_parentohos:widthmatch_parentohos:alignmentcenterohos:background_element$color:bg_colorohos:orientationverticalDirectionalLayoutohos:height50vpohos:widthmatch_parentohos:alignmentvertical_centerohos:background_element$color:blueohos:orientationhorizontalohos:start_padding12vpTextohos:id$id:titleohos:heightmatch_contentohos:widthmatch_contentohos:text选择设备ohos:text_color#FFFohos:text_fontHwChinese-mediumohos:text_size18fpohos:weight1/Textohos:id$id:tx_scan_statusohos:heightmatch_contentohos:widthmatch_contentohos:end_margin6vpohos:padding8vpohos:text搜索ohos:text_color#FFFohos:text_size14fp//DirectionalLayoutListContainerohos:id$id:lc_deviceohos:heightmatch_parentohos:widthmatch_parent//DirectionalLayout这个DirectionalLayout布局就是线性布局我们可以点击右侧导航栏的Previewer进行布局预览如下图所示。 右上角的T图标点击之后可以查看当前布局的层级。 这里说明一下有时候在通过资源使用颜色值的时候会无法生效所以就会直接使用#FFF在代码里也是如此这应该属于编译器的Bug。 标题栏就写好了还有状态栏我们没有改状态栏我们在MainAbility中进行修改代码如下所示 Overridepublic void onStart(Intent intent) {Window window WindowManager.getInstance().getTopWindow().get();window.setStatusBarColor(Color.getIntColor(#A7D3FF));super.onStart(intent);super.setMainRoute(ScanSlice.class.getName());}还是修改onStart()方法然后我们运行一下看看。 好了下面我们来写扫描需要的内容代码。 三、扫描 首先我们在com.llw.ble包下新建一个core包core包下创建一个BleCore类这里面就是控制Ble蓝牙相关的一切比如扫描连接读写数据等操作我们先不写代码。下面在core包下创建一个scan包。 ① 扫描接口 scan包下新建一个ScanCallback接口代码如下 public interface ScanCallback {void onScanResult(BleScanResult result);default void onGroupScanResultsEvent(ListBleScanResult results){}default void onScanFailed(String failed){} }② 扫描类 然后在scan包下新建一个BleScan类代码如下所示 public class BleScan {private final BleCentralManager centralManager;private boolean isScanning false;private ScanCallback scanCallback;// 创建扫描过滤器然后开始扫描private ListBleScanFilter filters;private static volatile BleScan mInstance;//初始化public static BleScan getInstance(Context context) {if (mInstance null) {synchronized (BleScan.class) {if (mInstance null) {mInstance new BleScan(context);}}}return mInstance;}public BleScan(Context context) {BleScanCallback centralManagerCallback new BleScanCallback();centralManager new BleCentralManager(context, centralManagerCallback);}/*** 当前是否正在扫描* return true 扫描中false 未扫描*/public boolean isScanning() {return isScanning;}/*** 设置过滤信息* param filters 蓝牙扫描过滤列表*/public void setFilters(ListBleScanFilter filters) {this.filters filters;}/*** 设置扫描回调页面需要实现才能获取扫描到的设备* param scanCallback 扫描回调*/public void setScanCallback(ScanCallback scanCallback) {this.scanCallback scanCallback;}/*** 开始扫描*/public void startScan() {if (centralManager null) {localScanFailed(Bluetooth not turned on.);return;}centralManager.startScan(filters);isScanning true;}/*** 停止扫描*/public void stopScan() {if (!isScanning) {localScanFailed(Not currently scanning, your stop has no effect.);return;}centralManager.stopScan();isScanning false;}/*** 实现扫描回调*/public class BleScanCallback implements BleCentralManagerCallback {Overridepublic void scanResultEvent(BleScanResult bleScanResult) {if (scanCallback ! null) {scanCallback.onScanResult(bleScanResult);}}Overridepublic void scanFailedEvent(int resultCode) {if (scanCallback ! null) {scanCallback.onScanFailed(String.valueOf(resultCode));}}Overridepublic void groupScanResultsEvent(final ListBleScanResult scanResults) {// 对扫描结果进行处理if (scanCallback ! null) {scanCallback.onGroupScanResultsEvent(scanResults);}}}/*** 本地扫描失败处理* param failed 错误信息*/private void localScanFailed(String failed) {if (scanCallback ! null) {scanCallback.onScanFailed(failed);}} }这里面采用单例模式在初始化之后直接调用然后再实现扫描回调接口返回扫描信息有开始、停止扫描和是否正在扫描方法。这个类你可以直接用也可以再封装到BleCore中这里我们封装到BleCore中修改BleCore中的代码如下所示 public class BleCore {private static volatile BleCore mInstance;private final BleScan bleScan;public BleCore(Context context) {//蓝牙扫描bleScan BleScan.getInstance(context);}public static BleCore getInstance(Context context) {if (mInstance null) {synchronized (BleCore.class) {if (mInstance null) {mInstance new BleCore(context);}}}return mInstance;}public void setPhyScanCallback(ScanCallback scanCallback) {bleScan.setScanCallback(scanCallback);}public boolean isScanning() {return bleScan.isScanning();}public void startScan() {bleScan.startScan();}public void stopScan() {bleScan.stopScan();} }四、业务处理 这里的业务处理主要是两个第一个是蓝牙开关监听第二个动态权限申请。 再进行业务处理之前我们先修改一下MyApplication类的名字修改为BleApp修改后再改动里面的代码如下所示 public class BleApp extends AbilityPackage {private static BleCore bleCore;Overridepublic void onInitialize() {super.onInitialize();bleCore BleCore.getInstance(getContext());}public static BleCore getBleCore() {return bleCore;} }① Slice的生命周期 首先我们来看一下Slice的生命周期这个就比较重要下面我们首先在com.llw.ble下创建一个utils包utils包下创建一个LogUtils类代码如下所示 public class LogUtils {static final HiLogLabel LABEL new HiLogLabel(HiLog.LOG_APP, 0x00201, HarmonyBle);private static HiLogLabel logLabel;public static void setLogLabel(HiLogLabel logLabel) {LogUtils.logLabel logLabel;}public static void Log(String content) {HiLog.info(LABEL, content);}public static void LogI(String TAG, String content) {HiLogLabel label new HiLogLabel(HiLog.LOG_APP, 0x00201, TAG);HiLog.info(label, content);}public static void LogD(String TAG, String content) {HiLogLabel label new HiLogLabel(HiLog.LOG_APP, 0x00201, TAG);HiLog.debug(label, content);}public static void LogE(String TAG, String content) {HiLogLabel label new HiLogLabel(HiLog.LOG_APP, 0x00201, TAG);HiLog.error(label, content);}public static void LogW(String TAG, String content) {HiLogLabel label new HiLogLabel(HiLog.LOG_APP, 0x00201, TAG);HiLog.warn(label, content);}}这是因为Harmony中打印日志比较麻烦所以写一个工具类封装一下下面我们修改一下ScanSlice类中的代码如下所示 public class ScanSlice extends AbilitySlice {private final String TAG ScanSlice.class.getSimpleName();Overridepublic void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_slice_scan);LogUtils.LogD(TAG, onStart);}Overridepublic void onActive() {LogUtils.LogD(TAG, onActive);}Overrideprotected void onInactive() {LogUtils.LogD(TAG, onInactive);}Overridepublic void onForeground(Intent intent) {LogUtils.LogD(TAG, onForeground);}Overrideprotected void onBackground() {LogUtils.LogD(TAG, onBackground);}Overrideprotected void onStop() {LogUtils.LogD(TAG, onStop);} }然后我们运行一下看看检查控制台日志 然后我们通过Home键回到桌面看看日志 然后我们点击桌面上的图标回到应用中看看日志 再回到桌面然后我们通过后台的运行程序进入应用看看日志 这两种回到应用的方式日志一样然后我们按返回键回到桌面看看日志 那么现在你对于Slice的生命周期就比较了解了下面我们进行代码的编写。 ② 蓝牙开关和动态权限请求 首先处理蓝牙相关的在BleCore中添加如下代码 private final BluetoothHost mBluetoothHost;在构造方法中实例化 public BleCore(Context context) {...// 获取蓝牙本机管理对象mBluetoothHost BluetoothHost.getDefaultHost(context);}然后我们再写两个方法 public boolean isEnableBt() {return mBluetoothHost.getBtState() BluetoothHost.STATE_ON;}public void enableBt() {mBluetoothHost.enableBt();}用于判断是否打开蓝牙和打开蓝牙回到ScanSlice中我们需要使用BleCore来处理蓝牙相关的工作代码如下所示 public class ScanSlice extends AbilitySlice {private final String TAG ScanSlice.class.getSimpleName();private BleCore bleCore;private Text txScanStatus;private ListContainer lcDevice;Overridepublic void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_slice_scan);bleCore BleApp.getBleCore();txScanStatus (Text) findComponentById(ResourceTable.Id_tx_scan_status);lcDevice (ListContainer) findComponentById(ResourceTable.Id_lc_device);//点击监听txScanStatus.setClickedListener(component - {});}Overridepublic void onActive() {// 判断是否打开蓝牙if (!bleCore.isEnableBt()) {//打开蓝牙bleCore.enableBt();return;}} }首先在onStart()中对BleCore进行实例化findComponentById就如同findViewById然后在onActive()中调用刚才我们所写的方法。 Overridepublic void onActive() {// 判断是否打开蓝牙if (!bleCore.isEnableBt()) {//打开蓝牙bleCore.enableBt();return;}}然后是定位权限的处理同样在onActive()中增加代码如下所示 Overridepublic void onActive() {// 判断是否打开蓝牙...// 是否获取定位权限String locationPermission ohos.permission.LOCATION;if (verifySelfPermission(locationPermission) ! IBundleManager.PERMISSION_GRANTED) {requestPermissionsFromUser(new String[]{locationPermission}, 100);return;}}这里首先我们定义一个权限然后判断是否授予没有授予则进行请求下面运行一下看看 那么我们就完成了蓝牙打开和定位权限动态申请你可以在运行一次你会发现你还需要请求权限的因为DS默认安装时不会保留应用的数据而蓝牙打开了属于系统层面的所以你可以不用再打开蓝牙而需要重新请求定位权限为了避免这一点我们点击Run→ Edit Configurations... 在弹出的窗口上勾选Keep Application Data 点击OK再运行即可。 五、扫描设备 接下来我们进行扫描的处理在ScanSlice中增加如下方法代码 private void startScan() {bleCore.startScan();txScanStatus.setText(停止);}private void stopScan() {bleCore.stopScan();txScanStatus.setText(搜索);}这里就是扫描和停止方法同时修改一下Text文本在onStart()中首先实现扫描回调监听然后处理再处理txScanStatus文本的点击事件代码如下所示 Overridepublic void onStart(Intent intent) {...bleCore.setPhyScanCallback(this);//点击监听txScanStatus.setClickedListener(component - {if (bleCore.isScanning()) {stopScan();//扫描开关停止扫描} else {startScan();//开始扫描}});}这里this会报错鼠标放在上面Alt Enter出现弹窗。 选择最后一个就会给你实现ScanCallback中的onScanResult()方法代码如下所示 Overridepublic void onScanResult(BleScanResult result) {LogUtils.LogD(TAG, result.getPeripheralDevice().getDeviceAddr());}我们在里面打印一下扫描到的设备Mac地址最后我们在onActive()中增加如下所示代码 Overridepublic void onActive() {...// 是否在扫描中if (!bleCore.isScanning()) {startScan();}}下面运行一下看看控制台日志 扫描出来了只不过目前还看不到所以我们要渲染一下让它可以看到。 六、显示设备 要显示设备首先我们需要写一个Bean。 ① 自定义蓝牙类 在core包下新建一个BleDevice类里面的代码如下所示 public class BleDevice {private String realName Unknown device; //蓝牙设备真实名称private String macAddress; //地址private int rssi; //信号强度private BlePeripheralDevice device; //设备public BleDevice(BleScanResult scanResult) {this.device scanResult.getPeripheralDevice();this.macAddress device.getDeviceAddr();String name device.getDeviceName().get();if (name ! null || !name.isEmpty()) {this.realName name;}this.rssi scanResult.getRssi();}public String getRealName() {return realName;}public void setRealName(String realName) {this.realName realName;}public String getMacAddress() {return macAddress;}public void setMacAddress(String macAddress) {this.macAddress macAddress;}public int getRssi() {return rssi;}public void setRssi(int rssi) {this.rssi rssi;}public BlePeripheralDevice getDevice() {return device;}public void setDevice(BlePeripheralDevice device) {this.device device;} }这个Bean没有什么好说的下面要做的就是列表Item的渲染在Android中我们使用的是适配器Adapter而在Harmony中使用的是提供者Provider。 ② 提供者 同样我们先写布局在layout下新建一个item_scan_device.xml代码如下所示 ?xml version1.0 encodingutf-8? DirectionalLayoutxmlns:ohoshttp://schemas.huawei.com/res/ohosohos:heightmatch_contentohos:widthmatch_parentohos:alignmentvertical_centerohos:background_element#FFFohos:bottom_padding8vpohos:orientationhorizontalohos:right_padding16vpohos:top_margin1vpohos:top_padding8vpImageohos:heightmatch_contentohos:widthmatch_contentohos:end_margin16vpohos:image_src$graphic:ic_bluetoothohos:start_margin16vp/DirectionalLayoutohos:heightmatch_contentohos:widthmatch_contentohos:orientationverticalohos:weight1Textohos:id$id:device_nameohos:heightmatch_contentohos:widthmatch_contentohos:text设备名称ohos:text_size16fp/Textohos:id$id:device_addressohos:heightmatch_contentohos:widthmatch_contentohos:text设备地址ohos:text_color$color:grayohos:text_size14fpohos:top_margin4vp//DirectionalLayoutTextohos:id$id:rssiohos:heightmatch_contentohos:widthmatch_contentohos:align_parent_endtrueohos:text_color#000000ohos:text_size10fp//DirectionalLayout几个主要内容设备名称、Mac地址、Rssi信号强度然后这里有一个图标在graphic下创建一个ic_bluetooth.xml代码如下所示 ?xml version1.0 encodingUTF-8?vectorxmlns:ohoshttp://schemas.huawei.com/res/ohosohos:height48vpohos:width48vpohos:viewportHeight1024ohos:viewportWidth1024pathohos:fillColor#A7D3FFohos:pathDataM53.31,512a458.69,458.69 0,1 1,917.38 0A458.69,458.69 0,0 1,53.31 512zM584.96,301.82a356.16,356.16 0,0 0,-39.81 -26.69c-12.1,-6.34 -32,-13.89 -52.74,-3.01 -20.48,10.82 -25.86,31.23 -27.78,44.67 -1.92,13.18 -1.92,30.21 -1.92,48.45v77.18l-57.92,-49.6a32,32 0,0 0,-41.6 48.64L445.44,512 363.2,582.4a32,32 0,1 0,41.6 48.64l57.92,-49.6v77.18c0,18.24 0,35.33 1.92,48.51 1.92,13.44 7.23,33.86 27.78,44.61 20.74,10.88 40.64,3.33 52.74,-2.94a356.48,356.48 0,0 0,39.81 -26.69l39.42,-28.8c10.62,-7.74 21.31,-15.55 29.06,-23.1 8.64,-8.58 18.56,-21.57 18.56,-40.06 0,-18.56 -9.92,-31.55 -18.56,-40.06 -7.68,-7.55 -18.43,-15.36 -29.06,-23.17L548.99,512l75.39,-54.98c10.62,-7.74 21.31,-15.55 29.06,-23.17 8.64,-8.51 18.56,-21.5 18.56,-40 0,-18.56 -9.92,-31.55 -18.56,-40.06 -7.68,-7.62 -18.43,-15.36 -29.06,-23.17l-39.42,-28.8zM526.72,367.36v64.77c0,7.36 0,11.01 2.37,12.16 2.3,1.28 5.25,-0.9 11.2,-5.25l44.8,-32.7 8.32,-6.08c3.97,-2.94 5.95,-4.42 5.95,-6.53 0,-2.18 -1.98,-3.65 -5.95,-6.53l-8.32,-6.14 -36.1,-26.3a3344.06,3344.06 0,0 0,-9.34 -6.78c-5.44,-3.97 -8.19,-5.95 -10.5,-4.8 -2.37,1.15 -2.37,4.54 -2.37,11.33v12.86zM526.72,656.45L526.72,591.74c0,-7.36 0,-11.01 2.37,-12.16 2.3,-1.22 5.25,0.96 11.2,5.25l44.8,32.7 8.32,6.14c3.97,2.88 5.95,4.35 5.95,6.53 0,2.11 -1.98,3.58 -5.95,6.53l-8.32,6.08 -36.1,26.37 -9.34,6.78c-5.44,3.97 -8.19,5.95 -10.5,4.74 -2.37,-1.15 -2.37,-4.48 -2.37,-11.33v-12.8z/path /vector下面我们写提供者在com.llw.ble下创建一个provider包包下创建一个ScanDeviceProvider类代码如下所示 public class ScanDeviceProvider extends BaseItemProvider {private final ListBleDevice deviceList;private final AbilitySlice slice;public ScanDeviceProvider(ListBleDevice list, AbilitySlice slice) {this.deviceList list;this.slice slice;}Overridepublic int getCount() {return deviceList null ? 0 : deviceList.size();}Overridepublic Object getItem(int position) {if (deviceList ! null position 0 position deviceList.size()) {return deviceList.get(position);}return null;}Overridepublic long getItemId(int position) {return position;}Overridepublic Component getComponent(int position, Component component, ComponentContainer componentContainer) {final Component cpt;ScanDeviceHolder holder;BleDevice device deviceList.get(position);if (component null) {cpt LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_item_scan_device, null, false);holder new ScanDeviceHolder(cpt);//将获取到的子组件信息绑定到列表项的实例中cpt.setTag(holder);} else {cpt component;// 从缓存中获取到列表项实例后直接使用绑定的子组件信息进行数据填充。holder (ScanDeviceHolder) cpt.getTag();}holder.deviceName.setText(device.getRealName());holder.deviceAddress.setText(device.getMacAddress());holder.rssi.setText(String.format(Locale.getDefault(), %d dBm, device.getRssi()));return cpt;}/*** 用于保存列表项的子组件信息*/public static class ScanDeviceHolder {Text deviceName;Text deviceAddress;Text rssi;public ScanDeviceHolder(Component component) {deviceName (Text) component.findComponentById(ResourceTable.Id_device_name);deviceAddress (Text) component.findComponentById(ResourceTable.Id_device_address);rssi (Text) component.findComponentById(ResourceTable.Id_rssi);}} }通过提供者的代码可以看到它和适配器的写法差不多不同的是你得注意getComponent()方法中的处理另外提供者默认提供了Item的点击方法所以我们不用再自己去写了。 ③ 显示设备 我们回到ScanSlice中使用首先我们创建几个变量代码如下所示 private final ListBleDevice mList new ArrayList();private ScanDeviceProvider provider;然后在onStart()方法中进行初始化 Overridepublic void onStart(Intent intent) {...provider new ScanDeviceProvider(mList, this);lcDevice.setItemProvider(provider);//列表item点击监听lcDevice.setItemClickedListener((listContainer, component, position, id) - {});}这里设置了列表提供者然后添加item点击监听最后我们在扫描回调中渲染数据修改代码如下所示 private int findIndex(BleDevice bleDevice, ListBleDevice deviceList) {int index 0;for (final BleDevice devi : deviceList) {if (bleDevice.getMacAddress().equals(devi.getDevice().getDeviceAddr())) return index;index 1;}return -1;}Overridepublic void onScanResult(BleScanResult result) {BleDevice bleDevice new BleDevice(result);int index findIndex(bleDevice, mList);if (index -1) {//添加新设备mList.add(bleDevice);} else {//更新已有设备的rssi和时间戳mList.get(index).setRssi(bleDevice.getRssi());}getUITaskDispatcher().syncDispatch(() - provider.notifyDataChanged());}这里添加一个findIndex()方法用于添加设备和更新设备最终通过UI线程同步刷新提供者再修改一个开始扫描和停止扫描的方法代码 private void startScan() {mList.clear();provider.notifyDataChanged();bleCore.startScan();txScanStatus.setText(停止);LogUtils.LogD(TAG,开始扫描设备);}private void stopScan() {bleCore.stopScan();txScanStatus.setText(搜索);LogUtils.LogD(TAG,已经停止扫描设备);}运行一下看看 七、源码 如果对你有所帮助的话不妨 Star 或 Fork山高水长后会有期~ 源码地址HarmonyBle-Java
http://www.hkea.cn/news/14269856/

相关文章:

  • 长春站建筑.net 网站关键字
  • 北流网站建设wordpress ssl
  • 成都建站优化公司建立平台网站需要花多少钱
  • 自己怎么建网站书籍扉页页面设计模板
  • 网站开发运营服务合同广州户外拓展训练基地
  • 怎么做网站访问被拒绝上海公司注册核名官网
  • 网站搜索框lnmp新手 wordpress
  • 网站建设的四个步骤wordpress微信授权访问
  • 我要看一集片做网站广东东莞房价2022最新价格
  • 高新区免费网站建设什么是网络工程师
  • 西安北郊做网站做app需要多少钱
  • 网站建设价格女淄博网络宣传
  • 网站建设怎么改栏目名称wordpress站内计费搜索
  • 西安网站建设 翼驰小网站下载渠道有哪些
  • 中国采购与招标网官方网站WordPress恶意扫描
  • 13572074638网站建设程序员都需要学什么
  • seo优化网站二维码制作网站有哪些
  • 上海网站建设方案服务百度信息流开户多少钱
  • 做h5的网站页面学技术网站
  • 抓取网站访客数据原理张家港百度网站制作
  • 做网站怎么优化网站推广任务 ip点击
  • 网站错误列表刷seo排名
  • 外贸建站推广哪家好网站首页分类怎么做的
  • 开源网站有哪些网站建设对接流程图
  • 3d云打印网站开发深圳住房建设局官方网站
  • 网站网站制作400多少钱城乡建设部注册建筑师网站
  • 男孩子怎么做网站推广wordpress上传图片x
  • 成都个人网站做网站思想
  • 城市建设模拟游戏登陆网站视频制作哪里可以学
  • 贵阳白云区城乡建设局网站土特产网站模板 织梦