Commit 19902484 by lirandong

添加 蓝牙监听、微信页面刷新功能

parent b21801d3
......@@ -28,39 +28,40 @@
"author": "",
"license": "MIT",
"dependencies": {
"@tarojs/async-await": "1.3.12",
"@tarojs/components": "1.3.12",
"@tarojs/redux": "1.3.12",
"@tarojs/redux-h5": "1.3.12",
"@tarojs/router": "1.3.12",
"@tarojs/taro": "1.3.12",
"@tarojs/taro-alipay": "1.3.12",
"@tarojs/taro-h5": "1.3.12",
"@tarojs/taro-swan": "1.3.12",
"@tarojs/taro-tt": "1.3.12",
"@tarojs/taro-weapp": "1.3.12",
"@tarojs/async-await": "1.3.14",
"@tarojs/components": "1.3.14",
"@tarojs/components-rn": "^1.3.14",
"@tarojs/redux": "1.3.14",
"@tarojs/redux-h5": "1.3.14",
"@tarojs/router": "1.3.14",
"@tarojs/taro": "1.3.14",
"@tarojs/taro-alipay": "1.3.14",
"@tarojs/taro-h5": "1.3.14",
"@tarojs/taro-redux-rn": "^1.3.14",
"@tarojs/taro-rn": "^1.3.14",
"@tarojs/taro-router-rn": "^1.3.14",
"@tarojs/taro-swan": "1.3.14",
"@tarojs/taro-tt": "1.3.14",
"@tarojs/taro-weapp": "1.3.14",
"axios": "^0.19.0",
"nerv-devtools": "^1.4.0",
"nervjs": "^1.4.0",
"react": "16.3.1",
"react-native": "0.59.8",
"redux": "^4.0.0",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.3.0",
"taro-axios": "^0.5.0",
"taro-listview": "^1.0.18",
"taro-ui": "^2.2.1",
"tslib": "^1.8.0",
"@tarojs/components-rn": "^1.3.12",
"@tarojs/taro-rn": "^1.3.12",
"@tarojs/taro-router-rn": "^1.3.12",
"@tarojs/taro-redux-rn": "^1.3.12",
"react": "16.3.1",
"react-native": "0.59.8"
"tslib": "^1.8.0"
},
"devDependencies": {
"@tarojs/plugin-babel": "1.3.12",
"@tarojs/plugin-csso": "1.3.12",
"@tarojs/plugin-sass": "1.3.12",
"@tarojs/plugin-uglifyjs": "1.3.12",
"@tarojs/webpack-runner": "1.3.12",
"@tarojs/plugin-babel": "1.3.14",
"@tarojs/plugin-csso": "1.3.14",
"@tarojs/plugin-sass": "1.3.14",
"@tarojs/plugin-uglifyjs": "1.3.14",
"@tarojs/webpack-runner": "1.3.14",
"@types/react": "^16.4.8",
"@types/webpack-env": "^1.13.6",
"@typescript-eslint/parser": "^1.6.0",
......@@ -71,17 +72,17 @@
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.6.1",
"eslint": "^5.16.0",
"eslint-config-taro": "1.3.12",
"eslint-config-taro": "1.3.14",
"eslint-plugin-import": "^2.12.0",
"eslint-plugin-react": "^7.8.2",
"eslint-plugin-react-hooks": "^1.6.1",
"eslint-plugin-taro": "1.3.12",
"eslint-plugin-taro": "1.3.14",
"husky": "^3.0.2",
"stylelint": "^10.1.0",
"stylelint-config-standard": "^18.3.0",
"stylelint-config-taro-rn": "1.3.12",
"stylelint-config-taro-rn": "1.3.14",
"stylelint-scss": "^3.9.3",
"stylelint-taro-rn": "1.3.12",
"stylelint-taro-rn": "1.3.14",
"tslint": "^5.17.0",
"tslint-react": "^4.0.0",
"typescript": "^3.0.1"
......
......@@ -34,9 +34,9 @@ class App extends Component {
*/
config: Config = {
pages: [
'pages/home/device/index',
'pages/home/tempaltes/index',
'pages/home/device/device_bind/index',
'pages/home/device/index',
'pages/home/user/index',
'pages/home/device/my_device/index',
'pages/home/device/add/index',
......
......@@ -5,3 +5,106 @@ export function scanCode(OBJECT?: Taro.scanCode.Param) {
return Taro.scanCode(OBJECT)
}
}
/**
* 关闭蓝牙模块,使其进入未初始化状态。
* 调用该方法将断开所有已建立的链接并释放系统资源。
* 建议在使用小程序蓝牙流程后调用,与Taro.openBluetoothAdapter成对调用。
*/
export function closeBluetoothAdapter() {
return Taro.closeBluetoothAdapter()
}
/**
* 初始化小程序蓝牙模块,
* 生效周期为调用Taro.openBluetoothAdapter至调用Taro.closeBluetoothAdapter或小程序被销毁为止。
* 在小程序蓝牙适配器模块生效期间,开发者可以正常调用下面的小程序API,并会收到蓝牙模块相关的on回调。
*/
export function openBluetoothAdapter() {
return Taro.openBluetoothAdapter()
}
/** 监听寻找到新设备的事件 */
export function onBluetoothDeviceFound(CALLBACK: Taro.onBluetoothDeviceFound.Param) {
return Taro.onBluetoothDeviceFound(CALLBACK)
}
/**
* 开始搜寻附近的蓝牙外围设备。
* 注意,该操作比较耗费系统资源,请在搜索并连接到设备后调用 stop 方法停止搜索。
*/
export function startBluetoothDevicesDiscovery(OBJECT?: Taro.startBluetoothDevicesDiscovery.Param) {
return Taro.startBluetoothDevicesDiscovery(OBJECT)
}
/**
*
* 连接低功耗蓝牙设备。
* 若小程序在之前已有搜索过某个蓝牙设备,并成功建立链接,
* 可直接传入之前搜索获取的deviceId直接尝试连接该设备,无需进行搜索操作。
*/
export function createBLEConnection(OBJECT: Taro.createBLEConnection.Param) {
return Taro.createBLEConnection(OBJECT)
}
/** 获取蓝牙设备某个服务中的所有 characteristic(特征值) */
export function getBLEDeviceCharacteristics(OBJECT: Taro.getBLEDeviceCharacteristics.Param) {
return Taro.getBLEDeviceCharacteristics(OBJECT)
}
/** 获取蓝牙设备所有 service(服务) */
export function getBLEDeviceServices(OBJECT: Taro.getBLEDeviceServices.Param) {
return Taro.getBLEDeviceServices(OBJECT)
}
/**
* 读取低功耗蓝牙设备的特征值的二进制数据值。
* 注意:必须设备的特征值支持`read`才可以成功调用,
* 具体参照 characteristic 的 properties 属性
*/
export function readBLECharacteristicValue(OBJECT: Taro.readBLECharacteristicValue.Param) {
return Taro.readBLECharacteristicValue(OBJECT)
}
/**
* 启用低功耗蓝牙设备特征值变化时的 notify 功能,订阅特征值。
* 注意:必须设备的特征值支持notify或者indicate才可以成功调用,
* 具体参照 characteristic 的 properties 属性另外,必须先启用notify才能监听到设备 characteristicValueChange 事件
*/
export function notifyBLECharacteristicValueChange(
OBJECT: Taro.notifyBLECharacteristicValueChange.Param
) {
return Taro.notifyBLECharacteristicValueChange(OBJECT)
}
/**
* 监听低功耗蓝牙设备的特征值变化。
* 必须先启用notify接口才能接收到设备推送的notification。
*/
export function onBLECharacteristicValueChange(
CALLBACK: Taro.onBLECharacteristicValueChange.Param
) {
return Taro.onBLECharacteristicValueChange(CALLBACK)
}
/**
* 停止搜寻附近的蓝牙外围设备。
* 若已经找到需要的蓝牙设备并不需要继续搜索时,建议调用该接口停止蓝牙搜索。
*/
export function stopBluetoothDevicesDiscovery() {
return Taro.stopBluetoothDevicesDiscovery()
}
export const bluetooth = {
closeBluetoothAdapter,
openBluetoothAdapter,
onBluetoothDeviceFound,
startBluetoothDevicesDiscovery,
createBLEConnection,
getBLEDeviceCharacteristics,
getBLEDeviceServices,
readBLECharacteristicValue,
notifyBLECharacteristicValueChange,
onBLECharacteristicValueChange,
stopBluetoothDevicesDiscovery
}
export const FETCH_TIME_OUT = 5000
export const BASE_URL = 'https://server.bdideal.com'
export const INTERFACE_REPEAT = '接口重复,请求已取消'
/** 蓝牙的服务 uuid */
export const BLE_SERVICE_ID = '000088A0-0000-1000-8000-00805F9B34FB'
/** 获取WiFi列表的特征值 uuid */
export const WIFI_CHARACTERISTIC_ID = '000088A1-0000-1000-8000-00805F9B34FB'
......@@ -47,3 +47,11 @@ export function getWindiwHeight() {
const HeaderHeight = process.env.TARO_ENV === 'rn' ? Header.HEIGHT : 0
return windowHeight - HeaderHeight
}
/** ArrayBuffer转16进度字符串 */
export function ArrayBufferToString(buffer: ArrayBuffer) {
const hexArr: any[] = Array.prototype.map.call(new Uint8Array(buffer), bit => {
return ('00' + bit.toString(16)).slice(-2)
})
return hexArr.join('')
}
import api from '@/api/index'
import { ComponentClass } from 'react'
import { AtSwipeAction } from 'taro-ui'
import { showMyToast } from '@/common/utils'
import { showMyToast, ArrayBufferToString } from '@/common/utils'
import Taro, { Component } from '@tarojs/taro'
import DeviceItem from '@/conpoments/device_item'
import { getFilmList } from '@/actions/asyncCounter'
import { View, Text, ScrollView } from '@tarojs/components'
import { bluetooth } from '@/common/adapter'
import './index.scss'
import { connect } from '@tarojs/redux'
import { WIFI_CHARACTERISTIC_ID, BLE_SERVICE_ID } from '@/common'
export interface IDeviceItem {
filmId: any
......@@ -23,7 +25,7 @@ export interface IDeviceItem {
export interface IBluetoothServerListItem {
deviceId: string
characteristics: string
characteristics: Taro.getBLEDeviceCharacteristics.PromisedPropCharacteristics
}
type PageStateProps = {
......@@ -40,9 +42,11 @@ type PageOwnProps = {}
type PageState = {
page: number
count: number
deviceId: string
deviceCode: string
showModal: boolean
deviceList: IDeviceItem[]
bluetoothServerList: IBluetoothServerListItem[]
}
type IProps = PageStateProps & PageDispatchProps & PageOwnProps
......@@ -69,9 +73,11 @@ class MyDevice extends Component {
this.state = {
page: 1,
count: 0,
deviceId: '',
deviceCode: '',
deviceList: [],
showModal: false
showModal: false,
bluetoothServerList: []
}
this.scanCode = this.scanCode.bind(this)
this.cancelModal = this.cancelModal.bind(this)
......@@ -125,15 +131,11 @@ class MyDevice extends Component {
this.cancelModal()
if (Taro.getEnv() !== Taro.ENV_TYPE.RN) {
try {
await this.bluetooth('40261886')
// const { result } = await Taro.scanCode({
// onlyFromCamera: true,
// scanType: ['qrCode', 'barCode']
// })
// await this.bluetooth(result)
// await api.common.addDeviceToken(result)
// showMyToast({ title: '添加成功~' })
// this.getDate()
const { result } = await Taro.scanCode({
onlyFromCamera: true,
scanType: ['qrCode', 'barCode']
})
await this.bluetooth(result)
} catch (error) {
showMyToast({ result: error, title: '添加失败~' })
console.error(error)
......@@ -143,50 +145,25 @@ class MyDevice extends Component {
async bluetooth(bluetoothCode: string) {
try {
await Taro.closeBluetoothAdapter()
await bluetooth.closeBluetoothAdapter()
Taro.showLoading({ title: '打开蓝牙...' })
await Taro.openBluetoothAdapter()
Taro.onBluetoothDeviceFound(async ({ devices }) => {
await bluetooth.openBluetoothAdapter()
bluetooth.onBluetoothDeviceFound(async ({ devices }) => {
Taro.showLoading({ title: '连接蓝牙...' })
for (const item of devices) {
const newAdvertisData = item.advertisData.slice(2, item.advertisData.byteLength)
const advertisData = String.fromCharCode.apply(null, new Uint8Array(newAdvertisData))
if (advertisData === bluetoothCode) {
const { deviceId } = item
await Taro.createBLEConnection({ deviceId })
Taro.stopBluetoothDevicesDiscovery()
showMyToast({ title: '蓝牙连接成功~' })
Taro.showLoading({ title: '获取蓝牙参数...' })
const { services } = await Taro.getBLEDeviceServices({ deviceId }) // 获取服务列表
const bluetoothServerList: IBluetoothServerListItem[] = []
for (const iterator of services) {
// const setverItem: IBluetoothServerListItem = {}
const { characteristics } = await Taro.getBLEDeviceCharacteristics({
deviceId,
serviceId: iterator.uuid
}) // 获取特征服务
console.log({ characteristics })
// setverItem.deviceId = deviceId
// setverItem.characteristics = characteristics
// bluetoothServerList.push(setverItem)
}
// await Taro.getBLEDeviceCharacteristics({ deviceId }) // 获取特征服务
// Taro.notifyBLECharacteristicValueChange()
console.log({ services })
await Taro.readBLECharacteristicValue({ deviceId, serviceId, characteristicId })
// setTimeout(async () => {
// Taro.closeBLEConnection({ deviceId })
// showMyToast({ title: '连接已断开~' })
// }, 10000)
this.createBLEConnection(item)
return
}
}
})
Taro.showLoading({ title: '搜索蓝牙...' })
await Taro.startBluetoothDevicesDiscovery({ services: ['8888'] })
await bluetooth.startBluetoothDevicesDiscovery({ services: ['8888'] })
} catch (error) {
console.error({ error })
Taro.stopBluetoothDevicesDiscovery()
bluetooth.stopBluetoothDevicesDiscovery()
const { errCode } = error
if (errCode === 10001) {
showMyToast({ title: '请打开手机蓝牙~' })
......@@ -194,6 +171,60 @@ class MyDevice extends Component {
}
}
/** 连接蓝牙 */
async createBLEConnection(item) {
const { deviceId } = item
await bluetooth.createBLEConnection({ deviceId })
bluetooth.stopBluetoothDevicesDiscovery()
showMyToast({ title: '蓝牙连接成功~' })
this.setState({ deviceId })
setTimeout(() => {
this.getWiFiList()
}, 0)
}
/** 获取蓝牙服务列表 */
async getBLEDeviceServices() {
Taro.showLoading({ title: '获取蓝牙参数...' })
const { deviceId } = this.state
const { services } = await bluetooth.getBLEDeviceServices({ deviceId }) // 获取服务列表
Taro.showLoading({ title: '获取特征值...' })
const serveList: Array<Promise<Taro.getBLEDeviceCharacteristics.Promised>> = []
for (const iterator of services) {
const serve = bluetooth.getBLEDeviceCharacteristics({
deviceId,
serviceId: iterator.uuid
}) // 获取特征服务
serveList.push(serve)
}
const serveListRes = await Promise.all(serveList)
const bluetoothServerList = serveListRes.map(i => i.characteristics)
Taro.hideLoading()
this.setState({ bluetoothServerList })
setTimeout(() => {
this.getWiFiList()
}, 0)
}
async getWiFiList() {
try {
const { deviceId } = this.state
await bluetooth.notifyBLECharacteristicValueChange({
deviceId,
state: true,
serviceId: BLE_SERVICE_ID,
characteristicId: WIFI_CHARACTERISTIC_ID
})
bluetooth.onBLECharacteristicValueChange(({ value }) => {
const str1 = ArrayBufferToString(value)
console.log({ str1 })
})
} catch (error) {
console.error(error)
}
}
shouldComponentUpdate(nextProps: IProps, _nextState) {
const { list } = this.props
const { list: _list } = nextProps
......
import api from '@/api/index'
import ListView from 'taro-listview'
import { ComponentClass } from 'react'
import { AtSwipeAction } from 'taro-ui'
import { connect } from '@tarojs/redux'
......@@ -84,6 +85,31 @@ class Films extends Component {
Taro.navigateTo({ url: `/pages/home/tempaltes/film_detail?filmId=${filmId}` })
}
// onPullDownRefresh(done) {
// this.setState({ page: 1 }, async () => {
// await this.getData()
// done()
// })
// }
onDownRefresh(done: any) {
console.log('object')
this.setState({ page: 1 }, async () => {
await this.getData()
done()
console.log('object')
})
}
onScrollToLower(done) {
this.setState({ page: this.state.page++ }, async () => {
await this.getData()
done()
console.log('object')
})
console.log('object1')
}
shouldComponentUpdate(nextProps: IProps) {
const { list } = this.props
const { list: _list } = nextProps
......@@ -91,10 +117,16 @@ class Films extends Component {
}
render() {
const { list } = this.props
const { list, count } = this.props
return (
<View className="films">
<ScrollView className="scroll-view" scrollY>
<ListView
className="scroll-view"
isEmpty={list.length === 0}
hasMore={list.length < count}
onScrollToLower={done => this.onScrollToLower(done)}
onPullDownRefresh={done => this.onDownRefresh(done)}
>
{list.map(item => (
<AtSwipeAction
autoClose
......@@ -112,7 +144,7 @@ class Films extends Component {
<FilmListItem onClick={() => this.goDetail(item)} {...item} />
</AtSwipeAction>
))}
</ScrollView>
</ListView>
</View>
)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment