当前位置:   article > 正文

如何写一个简单轻松的OpenHarmony蓝牙应用_indicates the local bluetooth is in le only mode

indicates the local bluetooth is in le only mode

此文件就是介绍 OpenHarmony 中的蓝牙接口和如何自己开发一个简单的蓝牙应用程序。

OpenHarmony 蓝牙接口简介

接口名称参数返回值作用
enableBluetooth()boolean开启蓝牙
disableBluetooth()boolean关闭蓝牙
getState()BluetoothState:enum获取蓝牙状态
setLocalName(name: string)boolean设置蓝牙名称
getLocalName()string获取蓝牙名称
setBluetoothScanMode(mode: ScanMode, duration: number)boolean设置蓝牙扫描模式
startBluetoothDiscovery()boolean发现蓝牙
pairDevice(deviceId: string)boolean配对设备
on.pinRequired(type: "pinRequired", callback: Callback<PinRequiredParam>)void侦听配对请求事件
disableBluetooth()boolean
  • getState
  1. enum BluetoothState {
  2. /** Indicates the local Bluetooth is off */
  3. STATE_OFF = 0,
  4. /** Indicates the local Bluetooth is turning on */
  5. STATE_TURNING_ON = 1,
  6. /** Indicates the local Bluetooth is on, and ready for use */
  7. STATE_ON = 2,
  8. /** Indicates the local Bluetooth is turning off */
  9. STATE_TURNING_OFF = 3,
  10. /** Indicates the local Bluetooth is turning LE mode on */
  11. STATE_BLE_TURNING_ON = 4,
  12. /** Indicates the local Bluetooth is in LE only mode */
  13. STATE_BLE_ON = 5,
  14. /** Indicates the local Bluetooth is turning off LE only mode */
  15. STATE_BLE_TURNING_OFF = 6
  • setBluetoothScanMode
  1. enum ScanMode {
  2. /** Indicates the scan mode is none */
  3. SCAN_MODE_NONE = 0,
  4. /** Indicates the scan mode is connectable */
  5. SCAN_MODE_CONNECTABLE = 1,
  6. /** Indicates the scan mode is general discoverable */
  7. SCAN_MODE_GENERAL_DISCOVERABLE = 2,
  8. /** Indicates the scan mode is limited discoverable */
  9. SCAN_MODE_LIMITED_DISCOVERABLE = 3,
  10. /** Indicates the scan mode is connectable and general discoverable */
  11. SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE = 4,
  12. /** Indicates the scan mode is connectable and limited discoverable */
  13. SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE = 5
  14. }

OpenHarmony 典蓝牙配对流程

开启蓝牙

  1. //开蓝牙
  2. ListItem() {
  3. TitleComponent({
  4. title: "enableBluetooth", //显示功能的名称
  5. bgColor: this.currentClick === 0 ? $r('app.color.font_color_007DFF') : $r('app.color.white_bg_color')
  6. }); //点击后颜色变化
  7. }
  8. .padding({ top: $r('app.float.distance_2'), bottom: $r('app.float.distance_2') })
  9. .onClick(() => { //点击事件
  10. if (this.btEnable) { //判断蓝牙是否已经打开
  11. this.message = '蓝牙已经使能';
  12. return;
  13. }
  14. let ret = BluetoothModel.enableBluetooth(); //打开蓝牙
  15. this.btEnable = ret; //如果启用了蓝牙,则返回true,否则返回false
  16. AppStorage.SetOrCreate('bluetoothIsOn', this.btEnable); //存储蓝牙打开的结果,方便调用
  17. AppStorage.SetOrCreate('currentClick', this.currentClick);
  18. this.message = "蓝牙使能执行结果:" + ret; //输出结果
  19. })

设置发现模式

  1. ListItem() {
  2. TitleComponent({
  3. title: "setBluetoothScanMode", //显示功能的名称
  4. bgColor: this.currentClick === 6 ? $r('app.color.font_color_007DFF') : $r('app.color.white_bg_color')
  5. }); //点击后颜色变化
  6. }
  7. .padding({ top: $r('app.float.distance_2'), bottom: $r('app.float.distance_2') })
  8. .onClick(() => { //点击事件
  9. this.currentClick = 6;
  10. if (this.btEnable) { //判断蓝牙是否已经打开
  11. retObj = {mod: 0, duration: -1} //mode表示要设置的蓝牙扫描模式{@link ScanMode}。 //*@param duration指示主机可发现的持续时间(秒)。
  12. setLocalNameRet = BluetoothModel.setBluetoothScanMode(mode, dur);
  13. if (setLocalNameRet) {
  14. AppStorage.SetOrCreate('setScanModeRet', setLocalNameRet);
  15. retObj.mod = mode;
  16. retObj.duration = dur;
  17. } else { //如果设置了蓝牙扫描模式,返回true,否则返回false
  18. console.info("BluetoothModel.setBluetoothScanMode failed!")
  19. onsole.info("BluetoothModel.setBluetoothScanMode success!", retObj)
  20. this.message = "setBluetoothScanMode执行" + this.setLocalNameRet ? '成功' : '失败';
  21. }
  22. }else {
  23. this.message = "蓝牙未使能";
  24. }
  25. })

注册 pinRequest

  1. ListItem() {
  2. TitleComponent({
  3. title: "btPinRequired" //显示功能的名称
  4. bgColor: this.currentClick === 18 ? $r('app.color.font_color_007DFF') : $r('app.color.white_bg_color')
  5. }); //点击后颜色变化
  6. }
  7. .padding({ top: $r('app.float.distance_2'), bottom: $r('app.float.distance_2') })
  8. .onClick(() => { //点击事件
  9. if (!this.btEnable) { //判断蓝牙是否已经打开
  10. this.message = "蓝牙未使能";
  11. return;
  12. }
  13. if (this.isPinRequiredClick) { //判断监听是否开启,即为一个“开关”,并存储数据
  14. bluetooth.off('pinRequired', () => {
  15. })
  16. this.message = 'on:pinRequired监听已关闭,再次点击将开启监听';
  17. this.isPinRequiredClick = false;
  18. AppStorage.SetOrCreate('on_pinRequired', this.btPinRequired);
  19. return;
  20. }
  21. this.isPinRequiredClick = true;
  22. AppStorage.SetOrCreate('on_pinRequired', this.btPinRequired); //type要侦听的配对请求事件的类型为:pinRequired
  23. this.pinMessage = 'PIN: ';
  24. bluetooth.on('pinRequired', (data) => { //callback回调用于侦听配对请求事件。
  25. this.pinMessage = 'PIN: ';
  26. this.pinMessage += data.pinCode;
  27. })
  28. this.message = 'on:pinRequired监听已启动,再次点击将关闭监听。';
  29. })

配对设备

  1. ListItem() {
  2. TitleComponent({
  3. title: "pairDevice", //显示功能的名称
  4. bgColor: this.currentClick === 10 ? $r('app.color.font_color_007DFF') : $r('app.color.white_bg_color')
  5. }); //点击后颜色变化
  6. }
  7. .padding({ top: $r('app.float.distance_2'), bottom: $r('app.float.distance_2') })
  8. .onClick(() => { //点击事件
  9. if (!this.btEnable) { //判断蓝牙是否已经打开
  10. this.message = '蓝牙未使能';
  11. return;
  12. }
  13. if (this.deviceId == '') { //deviceId为要配对的远程设备的地址
  14. this.message = "请输入目标设备的MAC"; //若为空,请输入目标MAC
  15. return;
  16. } else {
  17. this.pairDevice(this.deviceId); //如果配对过程已启动,则返回true,否则返回false
  18. }
  19. })

OpenHarmony 经典蓝牙设备发现流程

  1. 开启蓝牙(同上)
  2. 设置发现模式(同上)
  3. 开始蓝牙发现
  1. ListItem() {
  2. TitleComponent({
  3. title: "startBluetoothDiscovery", //显示功能的名称
  4. bgColor: this.currentClick === 8 ? $r('app.color.font_color_007DFF') : $r('app.color.white_bg_color')
  5. }); //点击后颜色变化
  6. }
  7. .padding({ top: $r('app.float.distance_2'), bottom: $r('app.float.distance_2') })
  8. .onClick(() => {//点击事件
  9. LogUtil.info(this.TAG_PAGE + 'startBluetoothDiscovery 111');
  10. if (!this.btEnable) { //判断蓝牙是否已经打开
  11. this.message = '蓝牙未使能';
  12. return;
  13. }
  14. Router.push({ uri: PAGE_URI_DEVICE_FOUND_MODE }); //搜索发现蓝牙的分界面
  15. })

搜索发现蓝牙的分界面

  1. Scroll() {
  2. Column() {
  3. if (this.isOn) {
  4. PairedDeviceComponent({ //显示已配对的设备
  5. controller: this.deviceController
  6. })
  7. AvailableDeviceComponent({ //显示可被配对的设备
  8. controller: this.deviceController
  9. })
  10. }
  11. }
  12. .width(ConfigData.WH_100_100)
  13. }
  14. //PairedDeviceComponent
  15. build() {
  16. Column() {
  17. if (this.pairedDevices && this.pairedDevices.length > 0) {
  18. // paired devices title
  19. Row() {
  20. Text($r('app.string.bluetooth_paired_devices')) //已配对的设备
  21. .fontSize($r('app.float.font_14'))
  22. .fontColor($r('app.color.font_color_182431'))
  23. }
  24. .width(ConfigData.WH_100_100)
  25. .padding({
  26. left: $r('app.float.distance_24'),
  27. top: $r('app.float.distance_19_5'),
  28. bottom: $r('app.float.distance_9_5')
  29. })
  30. List() {
  31. // paired devices list
  32. ForEach(this.pairedDevices, (item: BluetoothDevice, index: number) => {
  33. ListItem() {
  34. Row() {
  35. PairedItem({
  36. name: item.deviceName,
  37. type: item.deviceType,
  38. state: item.connectionState.toString(),
  39. mac: item.deviceId
  40. })
  41. }
  42. .width(ConfigData.WH_100_100)
  43. .borderRadius($r("app.float.radius_24"))
  44. .padding($r('app.float.distance_4'))
  45. .backgroundColor($r("app.color.white_bg_color"))
  46. .onClick(() => {
  47. this.itemClicked(item);
  48. })
  49. }
  50. }, item => JSON.stringify(item));
  51. }
  52. .height(200)
  53. .divider({
  54. strokeWidth: 1,
  55. color: $r('app.color.color_E3E3E3_grey'),
  56. startMargin: $r('app.float.wh_value_52'),
  57. endMargin: $r('app.float.wh_value_12')
  58. })
  59. .backgroundColor($r("app.color.white_bg_color"))
  60. .borderRadius($r("app.float.radius_24"))
  61. }
  62. }
  63. }
  64. //AvailableDeviceComponent
  65. build() {
  66. Column() {
  67. Row() {
  68. // available devices title
  69. Text($r('app.string.bluetooth_available_devices')) //可用设备
  70. .fontSize($r('app.float.font_14'))
  71. .fontColor($r('app.color.font_color_182431'))
  72. .width(ConfigData.WH_100_100)
  73. .padding({
  74. left: $r('app.float.distance_24'),
  75. top: $r('app.float.distance_19_5'),
  76. bottom: $r('app.float.distance_9_5')
  77. })
  78. Blank()
  79. // bluetooth discovering
  80. if (this.isDeviceDiscovering) {
  81. DiscoveringAnimatorComponent()
  82. }
  83. }
  84. .width(ConfigData.WH_100_100)
  85. if (this.availableDevices && this.availableDevices.length >= 1) {
  86. Scroll() {
  87. List() {
  88. // paired devices list
  89. ForEach(this.availableDevices, (item: BluetoothDevice) => {
  90. ListItem() {
  91. Row() {
  92. AvailableItem({
  93. name: item.deviceName,
  94. type: item.deviceType,
  95. state: item.connectionState.toString(),
  96. mac: item.deviceId
  97. })
  98. }
  99. .width(ConfigData.WH_100_100)
  100. .borderRadius($r("app.float.radius_24"))
  101. .padding($r('app.float.distance_4'))
  102. .backgroundColor($r("app.color.white_bg_color"))
  103. .onClick(() => {
  104. LogUtil.info(this.TAG_PAGE + 'item on click : ' + JSON.stringify(item));
  105. AppStorage.SetOrCreate('pairedMac', item.deviceId);
  106. // AppStorage.SetOrCreate('pairedName', item.deviceName);
  107. this.pairDevice(item)
  108. })
  109. }
  110. }, item => JSON.stringify(item));
  111. }
  112. .backgroundColor($r("app.color.white_bg_color"))
  113. .borderRadius($r("app.float.radius_24"))
  114. .height("80%")
  115. .divider({
  116. strokeWidth: 1,
  117. color: $r('app.color.color_E3E3E3_grey'),
  118. startMargin: $r('app.float.wh_value_52'),
  119. endMargin: $r('app.float.wh_value_12')
  120. })
  121. }
  122. .scrollBar(BarState.Auto)
  123. .scrollBarWidth(20)
  124. } else {
  125. Row() {
  126. // Scanning...
  127. Text($r('app.string.scanning'))
  128. .fontSize($r('app.float.font_20'))
  129. .textCase(TextCase.UpperCase);
  130. }
  131. }
  132. }
  133. }

整体代码

  1. ├─Component
  2. │ │ └─controller
  3. │ ├─pageTitle.ets
  4. │ ├─headComponent.ets
  5. │ └─titleComponent.ets
  6. ├─MainAbility
  7. │ ├─controller
  8. │ ├─model
  9. │ ├─BluetoothDevice.ts
  10. │ └─BluetoothModel.ts
  11. │ └─pages
  12. │ ├─homePage.ets
  13. │ └─deviceFound.ets
  14. └─Utils

page

  1. //homePage
  2. build() {
  3. Column() {
  4. GridContainer({
  5. columns: 12,
  6. sizeType: SizeType.Auto,
  7. gutter: vp2px(1) === 2 ? '12vp' : '0vp',
  8. margin: vp2px(1) === 2 ? '24vp' : '0vp'
  9. }) {
  10. Row({}) {
  11. Column() {
  12. }
  13. .width(ConfigData.WH_100_100)
  14. .height(ConfigData.WH_100_100)
  15. .useSizeType({
  16. xs: { span: 0, offset: 0 }, sm: { span: 0, offset: 0 },
  17. md: { span: 0, offset: 0 }, lg: { span: 2, offset: 0 }
  18. });
  19. Column() {
  20. Text(this.message)
  21. .fontSize($r("app.float.font_30"))
  22. .lineHeight($r("app.float.lineHeight_41"))
  23. .fontWeight(FontWeight.Bold)
  24. .fontFamily('HarmonyHeiTi')
  25. .textAlign(TextAlign.Start)
  26. .width(ConfigData.WH_100_100)
  27. .padding({
  28. left: $r('app.float.distance_26'),
  29. top: $r('app.float.distance_12'),
  30. bottom: $r('app.float.distance_17')
  31. })
  32. Column() {
  33. HeadComponent({ headName: $r('app.string.test1'), isActive: true })
  34. Scroll() {
  35. Column({ space: '12vp' }) {
  36. List() {
  37. //开蓝牙
  38. ListItem() {
  39. TitleComponent({
  40. title: "enableBluetooth",
  41. bgColor: this.currentClick === 0 ? $r('app.color.font_color_007DFF') : $r('app.color.white_bg_color')
  42. });
  43. }
  44. .padding({ top: $r('app.float.distance_2'), bottom: $r('app.float.distance_2') })
  45. .onClick(() => {
  46. this.currentClick = 0;
  47. if (this.btEnable) {
  48. this.message = '蓝牙已经使能';
  49. return;
  50. }
  51. let ret = BluetoothModel.enableBluetooth();
  52. this.btEnable = ret;
  53. AppStorage.SetOrCreate('bluetoothIsOn', this.btEnable);
  54. AppStorage.SetOrCreate('currentClick', this.currentClick);
  55. this.message = "蓝牙使能执行结果:" + ret;
  56. })
  57. //设置状态
  58. ListItem() {
  59. TitleComponent({
  60. title: "getState",
  61. bgColor: this.currentClick === 2 ? $r('app.color.font_color_007DFF') : $r('app.color.white_bg_color')
  62. });
  63. }
  64. .padding({ top: $r('app.float.distance_2'), bottom: $r('app.float.distance_2') })
  65. .onClick(() => {
  66. this.currentClick = 2;
  67. let ret = BluetoothModel.getState();
  68. switch (ret) {
  69. case 0:
  70. this.stateBT = 'STATE_OFF';
  71. break;
  72. case 1:
  73. this.stateBT = 'STATE_TURNING_ON';
  74. break;
  75. case 2:
  76. this.stateBT = 'STATE_ON';
  77. break;
  78. case 3:
  79. this.stateBT = 'STATE_TURNING_OFF';
  80. break;
  81. case 4:
  82. this.stateBT = 'STATE_BLE_TURNING_ON';
  83. break;
  84. case 5:
  85. this.stateBT = 'STATE_BLE_ON';
  86. break;
  87. case 6:
  88. this.stateBT = 'STATE_BLE_TURNING_OFF';
  89. break;
  90. default:
  91. this.stateBT = '未知状态';
  92. break;
  93. }
  94. this.message = "当前蓝牙的状态是:" + this.stateBT;
  95. })
  96. //设置本地名称
  97. ListItem() {
  98. TitleComponent({
  99. title: "setLocalName",
  100. bgColor: this.currentClick === 4 ? $r('app.color.font_color_007DFF') : $r('app.color.white_bg_color')
  101. });
  102. }
  103. .padding({ top: $r('app.float.distance_2'), bottom: $r('app.float.distance_2') })
  104. .onClick(() => {
  105. this.currentClick = 4;
  106. if (this.btEnable) {
  107. Router.push({ uri: PAGE_URI_DEVICE_NAME });
  108. this.message = "设置SCAN MODE " + this.setScanModeRet ? '成功' : '失败';
  109. } else {
  110. this.message = "蓝牙未使能";
  111. }
  112. })
  113. //设置扫描模式
  114. ListItem() {
  115. TitleComponent({
  116. title: "setBluetoothScanMode",
  117. bgColor: this.currentClick === 6 ? $r('app.color.font_color_007DFF') : $r('app.color.white_bg_color')
  118. });
  119. }
  120. .padding({ top: $r('app.float.distance_2'), bottom: $r('app.float.distance_2') })
  121. .onClick(() => {
  122. this.currentClick = 6;
  123. if (!this.btEnable) {
  124. Router.push({ uri: PAGE_URI_SET_SCAN_MODE });
  125. this.message = "setBluetoothScanMode执行" + this.setLocalNameRet ? '成功' : '失败';
  126. } else {
  127. this.message = "蓝牙未使能";
  128. }
  129. })
  130. // 开始蓝牙发现
  131. ListItem() {
  132. TitleComponent({
  133. title: "startBluetoothDiscovery",
  134. bgColor: this.currentClick === 8 ? $r('app.color.font_color_007DFF') : $r('app.color.white_bg_color')
  135. });
  136. }
  137. .padding({ top: $r('app.float.distance_2'), bottom: $r('app.float.distance_2') })
  138. .onClick(() => {
  139. LogUtil.info(this.TAG_PAGE + 'startBluetoothDiscovery 111');
  140. if (this.btEnable) {
  141. this.message = '蓝牙未使能';
  142. return;
  143. }
  144. this.currentClick = 8;
  145. Router.push({ uri: PAGE_URI_DEVICE_FOUND_MODE });
  146. })
  147. //监听PinRequired
  148. ListItem() {
  149. TitleComponent({
  150. title: this.btPinRequired,
  151. pinRequiredFlag: true,
  152. bgColor: this.currentClick === 18 ? $r('app.color.font_color_007DFF') : $r('app.color.white_bg_color')
  153. });
  154. }
  155. .padding({ top: $r('app.float.distance_2'), bottom: $r('app.float.distance_2') })
  156. .onClick(() => {
  157. if (!this.btEnable) {
  158. this.message = "蓝牙未使能";
  159. return;
  160. }
  161. if (this.isPinRequiredClick) {
  162. bluetooth.off('pinRequired', () => {
  163. })
  164. this.message = 'on:pinRequired监听已关闭,再次点击将开启监听';
  165. this.isPinRequiredClick = false;
  166. this.btPinRequired = 'on:pinRequired';
  167. AppStorage.SetOrCreate('on_pinRequired', this.btPinRequired);
  168. return;
  169. }
  170. this.isPinRequiredClick = true;
  171. this.currentClick = 18;
  172. this.btPinRequired = 'off:pinRequired';
  173. AppStorage.SetOrCreate('on_pinRequired', this.btPinRequired);
  174. this.pinMessage = 'PIN: ';
  175. bluetooth.on('pinRequired', (data) => {
  176. this.pinMessage = 'PIN: ';
  177. this.pinMessage += data.pinCode;
  178. })
  179. this.message = 'on:pinRequired监听已启动,再次点击将关闭监听。';
  180. })
  181. //配对设备
  182. ListItem() {
  183. TitleComponent({
  184. title: "pairDevice",
  185. bgColor: this.currentClick === 10 ? $r('app.color.font_color_007DFF') : $r('app.color.white_bg_color')
  186. });
  187. }
  188. .padding({ top: $r('app.float.distance_2'), bottom: $r('app.float.distance_2') })
  189. .onClick(() => {
  190. this.currentClick = 10;
  191. if (!this.btEnable) {
  192. this.message = '蓝牙未使能';
  193. return;
  194. }
  195. if (this.deviceId == '') {
  196. this.message = "请输入目标设备的MAC";
  197. return;
  198. } else {
  199. this.pairDevice(this.deviceId);
  200. }
  201. })
  202. //关蓝牙
  203. ListItem() {
  204. TitleComponent({
  205. title: "disableBluetooth",
  206. bgColor: this.currentClick === 1 ? $r('app.color.font_color_007DFF') : $r('app.color.white_bg_color')
  207. });
  208. }
  209. .padding({ top: $r('app.float.distance_2'), bottom: $r('app.float.distance_2') })
  210. .onClick(() => {
  211. this.currentClick = 1;
  212. if (!this.btEnable) {
  213. this.message = '蓝牙还未使能';
  214. return;
  215. }
  216. let ret = BluetoothModel.disableBluetooth();
  217. this.btEnable = false;
  218. AppStorage.SetOrCreate('bluetoothIsOn', this.btEnable);
  219. this.message = "蓝牙去使能执行结果:" + ret;
  220. })
  221. }
  222. }
  223. .width(ConfigData.WH_100_100)
  224. .height(600)
  225. }
  226. .scrollable(ScrollDirection.Vertical).scrollBar(BarState.On)
  227. .scrollBarColor(Color.Gray).scrollBarWidth(30)
  228. }
  229. .padding({ left: $r('app.float.distance_2'), right: $r('app.float.distance_2') })
  230. .width(ConfigData.WH_100_100)
  231. .height(ConfigData.WH_100_100)
  232. .useSizeType({
  233. xs: { span: 12, offset: 0 }, sm: { span: 12, offset: 0 },
  234. md: { span: 12, offset: 0 }, lg: { span: 8, offset: 2 }
  235. });
  236. }
  237. .padding({ left: $r('app.float.distance_2'), right: $r('app.float.distance_2') })
  238. .width(ConfigData.WH_100_100)
  239. .height(ConfigData.WH_100_100)
  240. .useSizeType({
  241. xs: { span: 12, offset: 0 }, sm: { span: 12, offset: 0 },
  242. md: { span: 12, offset: 0 }, lg: { span: 8, offset: 2 }
  243. });
  244. }
  245. .width(ConfigData.WH_100_100)
  246. .height(ConfigData.WH_100_100);
  247. }
  248. .backgroundColor($r("sys.color.ohos_id_color_sub_background"))
  249. .width(ConfigData.WH_100_100)
  250. .height(ConfigData.WH_100_100);
  251. }
  252. }

component:

  1. // titlecomoponent
  2. import ConfigData from '../Utils/ConfigData';
  3. @Component
  4. export struct TitleComponent{
  5. private title:string | Resource;
  6. private fontSize:string ='35vp';
  7. private stateChangeFlag: boolean = false;
  8. private pinRequiredFlag: boolean = false;
  9. private bondStateChangeFlag: boolean = false;
  10. @State isTouched:boolean = false;
  11. @State bgColor: Resource = $r('app.color.font_color_007DFF');
  12. @StorageLink('on_stateChange') state: string = 'on:stateChange';
  13. @StorageLink('on_pinRequired') pin: string = 'on:pinRequired';
  14. @StorageLink('on_bondStateChange') bondState: string = 'on:bondStateChange';
  15. build(){
  16. Column(){
  17. Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems:ItemAlign.Center }) {
  18. Text(this.stateChangeFlag ? this.state : (this.pinRequiredFlag ? this.pin : (this.bondStateChangeFlag ? this.bondState : this.title)))
  19. .textAlign(TextAlign.Center)
  20. .width("100%")
  21. .height(100)
  22. .fontSize(this.fontSize)
  23. .fontColor($r('app.color.font_color_182431'))
  24. .fontWeight(FontWeight.Medium)
  25. }
  26. .height(ConfigData.WH_100_100)
  27. .width(ConfigData.WH_100_100)
  28. .backgroundColor(this.bgColor)
  29. .linearGradient(this.isTouched ? {
  30. angle: 90,
  31. direction: GradientDirection.Right,
  32. colors: [[$r("app.color.DCEAF9"), 0.0], [$r("app.color.FAFAFA"), 1.0]]
  33. } : {
  34. angle: 90, direction: GradientDirection.Right,
  35. colors: [[$r("sys.color.ohos_id_color_foreground_contrary"), 1], [$r("sys.color.ohos_id_color_foreground_contrary"), 1]]})
  36. .onTouch((event: TouchEvent) => {
  37. if (event.type === TouchType.Down) {
  38. this.isTouched = true;
  39. }
  40. if (event.type === TouchType.Up) {
  41. this.isTouched = false;
  42. }
  43. })
  44. }
  45. .padding($r('app.float.distance_4'))
  46. .height("100vp")
  47. .borderRadius($r('app.float.radius_12'))
  48. }
  49. }

model:

  1. import { DeviceState } from './BluetoothModel'
  2. export class Profile {
  3. profileId: number = -1;
  4. profileConnectionState: number = -1
  5. constructor() {
  6. }
  7. }
  8. /**
  9. * Bluetooth device class
  10. */
  11. export default class BluetoothDevice {
  12. deviceId: string = '';
  13. deviceName: string = '';
  14. deviceType: string = '';
  15. connectionState: number = 0;
  16. profiles: Map<number, Profile> = new Map();
  17. constructor() {
  18. }
  19. setProfiles(data: Array<{
  20. profileId: number;
  21. profileConnectionState: number;
  22. }>): void{
  23. data.forEach((item: {
  24. profileId: number;
  25. profileConnectionState: number;
  26. }) => {
  27. this.setProfile({
  28. profileId: item.profileId,
  29. deviceId: this.deviceId,
  30. profileConnectionState: item.profileConnectionState
  31. })
  32. })
  33. }
  34. setProfile(data: {
  35. profileId: number;
  36. deviceId: string;
  37. profileConnectionState: number;
  38. }): void{
  39. if (this.deviceId !== data.deviceId) {
  40. return;
  41. }
  42. this.profiles.set(data.profileId, data)
  43. let countStateDisconnect = 0;
  44. let countStateConnecting = 0;
  45. let countStateConnected = 0;
  46. let countStateDisconnecting = 0;
  47. this.profiles.forEach((profile, key) => {
  48. if (profile.profileConnectionState == 0) {
  49. // 0:the current profile is disconnected
  50. countStateDisconnect++;
  51. } else if (profile.profileConnectionState == 1) {
  52. // 1:the current profile is being connected
  53. countStateConnecting++;
  54. } else if (profile.profileConnectionState == 2) {
  55. // 2:the current profile is connected
  56. countStateConnected++;
  57. } else if (profile.profileConnectionState == 3) {
  58. // 3:the current profile is being disconnected
  59. countStateDisconnecting++;
  60. }
  61. });
  62. if (countStateConnected > 0 || countStateDisconnecting > 0) {
  63. this.connectionState = DeviceState.STATE_CONNECTED;
  64. } else if (countStateConnecting > 0) {
  65. this.connectionState = DeviceState.STATE_CONNECTING;
  66. } else {
  67. this.connectionState = DeviceState.STATE_DISCONNECTED;
  68. }
  69. }
  70. }

预览图

                                                   

拥抱鸿蒙,拥抱未来,选择远方,风雨兼程。

最后

如果你想成为一名鸿蒙开发者,以下这些资料将是十分优质且有价值,让你的鸿蒙开发之路事半功倍!相对于网上那些碎片化的知识内容,这份学习资料的知识点更加系统化,更容易理解和记忆。

鸿蒙Next全套VIP学习资料←点击领取!(安全链接,放心点击

包含了:【OpenHarmony多媒体技术、Stage模型、ArkUI多端部署、分布式应用开发、音频、视频、WebGL、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战】等技术知识点。

1.鸿蒙(HarmonyOS NEXT)最新学习路线

有了路线图,怎么能没有学习资料呢,小编也准备了一份联合鸿蒙官方发布笔记整理收纳的一套系统性的鸿蒙(OpenHarmony )学习手册(共计1236页)与鸿蒙(OpenHarmony )开发入门教学视频,内容包含:ArkTS、ArkUI、Web开发、应用模型、资源分类…等知识点。

获取以上完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料

2.大厂面试必问面试题

3.鸿蒙南向开发技术

 4.鸿蒙APP开发必备

 5.HarmonyOS Next 最新全套视频教程

 6.鸿蒙生态应用开发白皮书V2.0PDF

获取以上完整鸿蒙HarmonyOS学习资料,请点击→纯血版全套鸿蒙HarmonyOS学习资料

总结
总的来说,华为鸿蒙不再兼容安卓,对中年程序员来说是一个挑战,也是一个机会。只有积极应对变化,不断学习和提升自己,他们才能在这个变革的时代中立于不败之地。 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/981386
推荐阅读
相关标签
  

闽ICP备14008679号