bluetooth.js 4.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
import {
  invoke,
3 4 5
  publish,
  arrayBufferToBase64,
  base64ToArrayBuffer
fxy060608's avatar
fxy060608 已提交
6 7 8 9 10
} from '../../bridge'

/**
 * 执行蓝牙相关方法
 */
11
function bluetoothExec (method, callbackId, data = {}, beforeSuccess) {
fxy060608's avatar
fxy060608 已提交
12 13 14 15 16 17 18 19 20 21 22
  var deviceId = data.deviceId
  if (deviceId) {
    data.deviceId = deviceId.toUpperCase()
  }
  var serviceId = data.serviceId
  if (serviceId) {
    data.serviceId = serviceId.toUpperCase()
  }

  plus.bluetooth[method.replace('Changed', 'Change')](Object.assign(data, {
    success (data) {
23 24 25 26
      if (typeof beforeSuccess === 'function') {
        beforeSuccess(data)
      }
      invoke(callbackId, Object.assign({}, data, {
fxy060608's avatar
fxy060608 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
        errMsg: `${method}:ok`,
        code: undefined,
        message: undefined
      }))
    },
    fail (error = {}) {
      invoke(callbackId, {
        errMsg: `${method}:fail ${error.message || ''}`,
        errCode: error.code || 0
      })
    }
  }))
}
/**
 * 监听蓝牙相关事件
 */
43
function bluetoothOn (method, beforeSuccess) {
fxy060608's avatar
fxy060608 已提交
44
  plus.bluetooth[method.replace('Changed', 'Change')](function (data) {
45 46 47 48
    if (typeof beforeSuccess === 'function') {
      beforeSuccess(data)
    }
    publish(method, Object.assign({}, data, {
fxy060608's avatar
fxy060608 已提交
49 50 51 52 53 54 55
      code: undefined,
      message: undefined
    }))
  })
  return true
}

56 57 58 59 60 61 62 63 64 65
function checkDevices (data) {
  data.devices = data.devices.map(device => {
    var advertisData = device.advertisData
    if (advertisData && typeof advertisData !== 'string') {
      device.advertisData = arrayBufferToBase64(advertisData)
    }
    return device
  })
}

fxy060608's avatar
fxy060608 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
var onBluetoothAdapterStateChange
var onBluetoothDeviceFound
var onBLEConnectionStateChange
var onBLEConnectionStateChanged
var onBLECharacteristicValueChange

export function openBluetoothAdapter (data, callbackId) {
  onBluetoothAdapterStateChange = onBluetoothAdapterStateChange || bluetoothOn('onBluetoothAdapterStateChange')
  bluetoothExec('openBluetoothAdapter', callbackId)
}

export function closeBluetoothAdapter (data, callbackId) {
  bluetoothExec('closeBluetoothAdapter', callbackId)
}

export function getBluetoothAdapterState (data, callbackId) {
  bluetoothExec('getBluetoothAdapterState', callbackId)
}

export function startBluetoothDevicesDiscovery (data, callbackId) {
86
  onBluetoothDeviceFound = onBluetoothDeviceFound || bluetoothOn('onBluetoothDeviceFound', checkDevices)
fxy060608's avatar
fxy060608 已提交
87 88 89 90 91 92 93 94
  bluetoothExec('startBluetoothDevicesDiscovery', callbackId, data)
}

export function stopBluetoothDevicesDiscovery (data, callbackId) {
  bluetoothExec('stopBluetoothDevicesDiscovery', callbackId)
}

export function getBluetoothDevices (data, callbackId) {
95
  bluetoothExec('getBluetoothDevices', callbackId, {}, checkDevices)
fxy060608's avatar
fxy060608 已提交
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
}

export function getConnectedBluetoothDevices (data, callbackId) {
  bluetoothExec('getConnectedBluetoothDevices', callbackId, data)
}

export function createBLEConnection (data, callbackId) {
  onBLEConnectionStateChange = onBLEConnectionStateChange || bluetoothOn('onBLEConnectionStateChange')
  onBLEConnectionStateChanged = onBLEConnectionStateChanged || bluetoothOn('onBLEConnectionStateChanged')
  bluetoothExec('createBLEConnection', callbackId, data)
}

export function closeBLEConnection (data, callbackId) {
  bluetoothExec('closeBLEConnection', callbackId, data)
}

export function getBLEDeviceServices (data, callbackId) {
  bluetoothExec('getBLEDeviceServices', callbackId, data)
}

export function getBLEDeviceCharacteristics (data, callbackId) {
  bluetoothExec('getBLEDeviceCharacteristics', callbackId, data)
}

export function notifyBLECharacteristicValueChange (data, callbackId) {
121 122 123 124
  onBLECharacteristicValueChange = onBLECharacteristicValueChange || bluetoothOn('onBLECharacteristicValueChange',
    data => {
      data.value = arrayBufferToBase64(data.value)
    })
fxy060608's avatar
fxy060608 已提交
125 126 127 128
  bluetoothExec('notifyBLECharacteristicValueChange', callbackId, data)
}

export function notifyBLECharacteristicValueChanged (data, callbackId) {
129 130 131 132
  onBLECharacteristicValueChange = onBLECharacteristicValueChange || bluetoothOn('onBLECharacteristicValueChange',
    data => {
      data.value = arrayBufferToBase64(data.value)
    })
fxy060608's avatar
fxy060608 已提交
133 134 135 136 137 138 139 140
  bluetoothExec('notifyBLECharacteristicValueChanged', callbackId, data)
}

export function readBLECharacteristicValue (data, callbackId) {
  bluetoothExec('readBLECharacteristicValue', callbackId, data)
}

export function writeBLECharacteristicValue (data, callbackId) {
141 142
  data.value = base64ToArrayBuffer(data.value)
  bluetoothExec('writeBLECharacteristicValue', callbackId, data)
fxy060608's avatar
fxy060608 已提交
143
}