蓝牙设备App线下开发——蓝牙 H5 开发指导

bingfeng
发布于 2020-10-13 10:30
浏览
0收藏

智慧生活 App 发现蓝牙设备

 

  • 开发者修改设备蓝牙广播名称,使之符合蓝牙 HiLink 命名规则。
  • 完成设备蓝牙广播名称修改后,由华为方进行后台配置,使 App 支持扫描该名称。(目前由人工配置,后续自动配置)
  • 智慧生活 App 扫描到的蓝牙名字满足 HiLink 命名规则,即当做智慧生活设备处理。根据设备注册时传入的 SN 判断此设备是否已注册,未注册的显示,已注册的不显示。

 

蓝牙HiLink命名规则: Hi-AA…AA-XYYYYSSNNNN

 

  • Hi- : 为固定前缀,3字节
  • AA…AA :厂商名加上设备名,由厂商决定,1-14个字符。可以包含字母、数字、下划线,不支持其他字符
  • - :为固定分隔符
  • X:版本号,非0,目前填1. Hi-和-X可以做识别,减少误添加的几率
  • YYYY:产品的唯一标识符,即产品的 ProductID,在开发者联盟网站上,注册产品后就会生成。
  • SS: 预留扩展字段,缺省为00
  • NNNN:序列号 SN,用于区分相同产品的多个设备。

 

说明


不足4位均做空字符串“”处理,大于等于4位截取前4位当做 SN,可以包含字母、数字、下划线,不支持其他字符。


H5 开发调试前准备


代码文件夹需遵循如下结构,方可在智慧生活 App 中正常访问相应页面。

蓝牙设备App线下开发——蓝牙 H5 开发指导 -鸿蒙开发者社区蓝牙设备App线下开发——蓝牙 H5 开发指导 -鸿蒙开发者社区蓝牙设备App线下开发——蓝牙 H5 开发指导 -鸿蒙开发者社区

蓝牙设备App线下开发——蓝牙 H5 开发指导 -鸿蒙开发者社区

表1 目录说明

 

蓝牙设备注册流程


此部分内容需配合蓝牙设备 JSAPI 接口描述使用。

获取手机系统信息,判断手机操作系统是 Android 还是 iOS。

function getSystemInfo() {

   window.getSystemInfoSyncCallBack = info => {

       let data = JSON.parse(info);

       if(data.platform == "iOS") {

           console.log("iOS设备")

           isIOS = true;

       } else {

           console.log("andorid设备")

           isIOS = false;

       }

       listeningBleChange();

       getBluetoothAdapterState();

   }

   window.hilink.getSystemInfoSync('getSystemInfoSyncCallBack')

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.

 

调用 hilink.onBluetoothAdapterStateChange() 监听蓝牙状态变化。

 

监听到蓝牙开启,则进入注册流程;监听到关闭,则调用 hilink.openBluetoothAdapter() 打开蓝牙。

function listeningBleChange() {

   window.hilink.onBluetoothAdapterStateChange('onBlueToothAdapterStateChangeCallback') // 监听蓝牙状态

   window.onBlueToothAdapterStateChangeCallback = res => { // 监听蓝牙状态回调函数

       let data = JSON.parse(res)

       console.log('监听蓝牙状态', data)

       if(data.available) {

           getUnRegisterDeviceFun();

       } else {

           openBlueTooth();

       }

   }

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

 

调用 hilink.getBluetoothAdapterState() 获取手机蓝牙开关状态。

 

如果未打开,则调用 hilink.openBluetoothAdapter() 打开蓝牙;如果是打开状态,则进入注册流程。

function getBluetoothAdapterState() {

   window.hilink.getBluetoothAdapterState("getBlueToothAdapterStateCallback")

   window.getBlueToothAdapterStateCallback = res => {

       let data = JSON.parse(res)

       console.log('1.蓝牙当前状态', data)

       if(data.available) {

           getUnRegisterDeviceFun();

       } else {

           openBlueTooth();

       }

   }

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

 

设备注册


注册时,蓝牙设备固件版本(fwv,如1.0)及蓝牙设备硬件版本(hwv,如1.0)需与 HiLink 开发者平台上的版本号一致。
注册页建议有连接设备的进度提示,进度条或百分比、连接成功、连接失败等。30秒连接不上,可以提示连接超时。


Android 系统


iOS系统

 

iOS 的注册流程分为两种情况,都需要蓝牙硬件暴露 MAC 地址。

 

  • 情况一:通过 hilink.startBluetoothDevicesDiscovery([],0,1) 开启扫描,在 hilink.onBluetoothDeviceFound() 回调参数中包含 MAC 地址
  • 情况二:通过 hilink.startBluetoothDevicesDiscovery([],0,1) 开启扫描,在 hilink.onBluetoothDeviceFound() 回调参数中不包含 MAC 地址

 

说明

 

  • 如果蓝牙已打开,则获取当前页面被选中的未注册的设备,调用 hilink.getCurrentUnregisteredDevice() ,获取设备 deviceId (iOS 为 UUID)。
  • 监听寻找到新设备, 同时调用 hilink.onBluetoothDeviceFound ()、hilink.startBluetoothDevicesDiscovery([],0,1)。

通过 hilink.onBluetoothDeviceFound () 的回调,查看监听到的新设备中的 deviceId 属性,与 getCurrentUnregisteredDevice 中获取的 UUID 对比,如匹配上,说明已经找到要注册设备。

通过扫描设备回调参数中的 manufacturerDate,解析获得 MAC 地址。

  • 调用 hilink.stopBluetoothDevicesDiscovery() 停止扫描附近的蓝牙设备。
  • 通过 UUID 连接蓝牙。连接成功后使用 mac 地址调用 hilink.registerBleDevice() 注册设备到 App 九宫格界面。
    function getUnRegisterDeviceFun() {
    
       window.getCurrentUnregisteredDeviceCallback = res => {
    
           let data = JSON.parse(res)
    
           deviceId = data.deviceId
    
           console.log('2.获取A的MAC地址(ios的uuid)', data)
    
           if(isIOS) {
    
               getIOSdevices();
    
           } else {
    
               mac = deviceId;
    
               connectDevice();
    
           }        
    
       }
    
       window.hilink.getCurrentUnregisteredDevice('getCurrentUnregisteredDeviceCallback')
    
    }
    
    function registerBleDevice(mac) {
    
       console.log('注册设备:', mac)
    
       window.hilink.registerBleDevice(mac, fwv, hwv, 'registerBleDeviceCallback')
    
       window.registerBleDeviceCallback = res => {
    
           console.log(res)
    
       }
    
    }​
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
    • 16.
    • 17.
    • 18.
    • 19.
    • 20.
    • 21.
    • 22.
    • 23.
    • 24.
    • 25.
    • 26.
    • 27.
    • 28.
    • 29.
    • 30.
    • 31.
    • 32.
    • 33.
    • 34.
    • 35.
    • 36.
    • 37.
    • 38.
    • 39.
    • 40.
    • 41.

 

如果蓝牙已打开,则获取当前页面被选中的未注册的设备,调用 hilink.getCurrentUnregisteredDevice() ,获取设备 deviceId (iOS 为 UUID)。
通过 UUID 调用 hilink.createBLEConnection() 连接蓝牙。

 

连接成功后。通过 180a 服务和 2a23 特征值读取 MAC。首先调用 hilink.notifyBLECharacteristicValueChange (deviceId, '0000180a-0000-1000-8000-00805f9b34fb','00002a23-0000-1000-8000-00805f9b34fb', true),当返回0时,表示通知成功。

 

调用 readBLECharacteristicValue(deviceId, '0000180a-0000-1000-8000-00805f9b34fb','00002a23-0000-1000-8000-00805f9b34fb', callback),读取 MAC。
调用 hilink.registerBleDevice() 注册。

function getUnRegisterDeviceFun() {

   window.getCurrentUnregisteredDeviceCallback = res => {

       let data = JSON.parse(res)

       deviceId = data.deviceId

       console.log('2.获取A的MAC地址(ios的uuid)', data)

       if(isIOS) {

           getIOSdevices();

       } else {

           mac = deviceId;

           connectDevice();

       }        

   }

   window.hilink.getCurrentUnregisteredDevice('getCurrentUnregisteredDeviceCallback')

}

function connectDevice() {

   //去连接设备

   window.hilink.createBLEConnection(deviceId)

   window.hilink.onBLEConnectionStateChange('onBLEConnectionStateChangeCallback') // 监听蓝牙设备连接返回

   window.onBLEConnectionStateChangeCallback = res => { // 监听蓝牙设备连接返回函数

       let data = JSON.parse(res)

       console.log('3.连接监听结果', data)

       if(data.connected) {

           registerBleDevice(mac);

       } else {

           getSystemInfo();

       }

   }

}

function getMacByRead() {

   window.onBLECharacteristicValueChangeCallback = res => {

   }

   window.hilink.onBLECharacteristicValueChange("onBLECharacteristicValueChangeCallback")

   window.hilink.createBLEConnection(deviceId)

   window.hilink.onBLEConnectionStateChange('onIOSBLEConnectionStateChangeCallback')

   window.onIOSBLEConnectionStateChangeCallback = res => {

       let data = JSON.parse(res)

       if(data.connected) {

           let timer = null

           clearInterval(timer);

           timer = setInterval(() => {

               let status = window.hilink.notifyBLECharacteristicValueChange(deviceId, serviceId, characteristicId, true)

               console.log('notify status:',status)

               if(status === 0) {

                   clearInterval(timer);

                   window.hilink.readBLECharacteristicValue (deviceId, serviceId, characteristicId, 'readBLECharacteristicValueCallback')

                   window.readBLECharacteristicValueCallback = res =>{

                       console.log('readBLECharacteristicValueCallback:',res)

                       registerBleDevice(res)

                   }

               }

           }, 200)

       } else {

           getSystemInfo();

       }

   }

}

function registerBleDevice(mac) {

   console.log('注册设备:', mac)

   window.hilink.registerBleDevice(mac, fwv, hwv, 'registerBleDeviceCallback')

   window.registerBleDeviceCallback = res => {

       console.log(res)

   }

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.

 

如果蓝牙已打开,则获取当前页面被选中的未注册的设备,调用 hilink.getCurrentUnregisteredDevice() ,获取设备 deviceId (安卓为 MAC 地址)。
获取 deviceId 后,调用 hilink.createBLEConnection() 连接蓝牙设备 。

连接蓝牙设备时,需要监听蓝牙连接状态的改变事件。调用 hilink.onBLEConnectionStateChange() 可以获得连接结果。

 

说明


如果连接异常,则重新启动注册流程。
成功连接蓝牙设备,则通知底层在IOT云端注册蓝牙设备,调用 hilink.registerBleDevice() 注册设备到 App 九宫格界面。

function getUnRegisterDeviceFun() {

   window.getCurrentUnregisteredDeviceCallback = res => {

       let data = JSON.parse(res)

       deviceId = data.deviceId

       console.log('2.获取A的MAC地址(ios的uuid)', data)

       if(isIOS) {

           getIOSdevices();

       } else {

           mac = deviceId;

           connectDevice();

       }        

   }

   window.hilink.getCurrentUnregisteredDevice('getCurrentUnregisteredDeviceCallback')

}

function connectDevice() {

   //去连接设备

   window.hilink.createBLEConnection(deviceId)

   window.hilink.onBLEConnectionStateChange('onBLEConnectionStateChangeCallback') // 监听蓝牙设备连接返回

   window.onBLEConnectionStateChangeCallback = res => { // 监听蓝牙设备连接返回函数

       let data = JSON.parse(res)

       console.log('3.连接监听结果', data)

       if(data.connected) {

           registerBleDevice(mac);

       } else {

           getSystemInfo();

       }

   }

}

function registerBleDevice(mac) {

   console.log('注册设备:', mac)

   window.hilink.registerBleDevice(mac, fwv, hwv, 'registerBleDeviceCallback')

   window.registerBleDeviceCallback = res => {

       console.log(res)

   }

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.

 

 

 

 

分类
收藏
回复
举报


回复
    相关推荐