Skip to content
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ed9904e
Update index.ios.js
dos1in Mar 6, 2026
812c7cc
fix: 修复 !deviceId && !mtu 永远为 false
dos1in Mar 6, 2026
b7b6f68
fix: offBluetoothAdapterStateChange 需要移除所有的监听
dos1in Mar 10, 2026
d7ffca5
fix: 不传此参数则移除所有监听函数
dos1in Mar 10, 2026
01d8e3e
fix: 鸿蒙需要调用 write 方法
dos1in Mar 10, 2026
8c2d331
fix: 优化错误日志信息
dos1in Mar 10, 2026
82782c3
Merge branch 'master' into fix/bluetooth-msg
dos1in Mar 10, 2026
c216df7
fix: 调整 offBluetoothAdapterStateChange 支持移除指定 callback
dos1in Mar 13, 2026
2bd5479
fix: bluetoothDeviceFound 只允许添加一个回调
dos1in Mar 13, 2026
bfedfe4
fix: 仅允许一个 BLE 特征值变化回调,优化回调管理
dos1in Mar 13, 2026
fae00a5
fix: 增加回调类型检查,确保只接受函数类型的回调
dos1in Mar 13, 2026
118b48c
fix: 增加回调类型检查并输出警告信息,确保只接受函数类型的回调
dos1in Mar 13, 2026
fa4bf8e
Merge branch 'master' into fix/bluetooth-msg
dos1in Mar 27, 2026
b2a0d6d
Merge branch 'master' into fix/bluetooth-msg
dos1in Apr 24, 2026
d6deaed
Merge branch 'master' into fix/bluetooth-msg
dos1in Apr 27, 2026
d255a70
Merge branch 'master' into fix/bluetooth-msg
dos1in May 13, 2026
7399dba
Merge branch 'master' into fix/bluetooth-msg
dos1in May 14, 2026
bc11106
Merge branch 'master' into fix/bluetooth-msg
dos1in May 15, 2026
89061eb
Merge branch 'master' into fix/bluetooth-msg
dos1in May 28, 2026
3ed97ff
Merge branch 'master' into fix/bluetooth-msg
hiyuki Jul 1, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 77 additions & 65 deletions packages/api-proxy/src/platform/api/ble-connection/index.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ let DiscoverPeripheralSubscription = null
let updateStateSubscription = null
let discovering = false
let getDevices = [] // 记录已扫描的设备列表
const deviceFoundCallbacks = []
let deviceFoundCallback = null // 仅允许一个回调,重复添加会被覆盖
let characteristicCallback = null // 仅允许一个回调,重复添加会被覆盖
const onStateChangeCallbacks = []
const characteristicCallbacks = []
const onBLEConnectionStateCallbacks = []
let characteristicSubscriptions = {}
const connectedDevices = new Set()
Expand Down Expand Up @@ -56,7 +56,10 @@ const removeUpdateStateSubscription = function () {
updateStateSubscription = null
}
}
const commonFailHandler = function (errMsg, fail, complete) {
const commonFailHandler = function (errMsg, fail, complete, reason) {
if (reason != null) {
errMsg = errMsg + ' ' + (reason.message != null ? reason.message : String(reason))
}
const result = {
errMsg
}
Expand All @@ -80,12 +83,12 @@ function openBluetoothAdapter (options = {}) {
// 先请求权限,再初始化蓝牙管理器
bluetoothPermission().then((hasPermissions) => {
if (!hasPermissions) {
commonFailHandler('openBluetoothAdapter:fail no permission', fail, complete)
commonFailHandler('openBluetoothAdapter:fail', fail, complete, 'no permission')
return
}

if (bleManagerInitialized) {
commonFailHandler('openBluetoothAdapter:fail already opened', fail, complete)
commonFailHandler('openBluetoothAdapter:fail', fail, complete, 'already opened')
return
}

Expand All @@ -103,17 +106,17 @@ function openBluetoothAdapter (options = {}) {
success(result)
complete(result)
} else {
commonFailHandler('openBluetoothAdapter:fail bluetooth not enabled', fail, complete)
commonFailHandler('openBluetoothAdapter:fail', fail, complete, 'bluetooth not enabled')
}
}).catch((error) => {
commonFailHandler('openBluetoothAdapter:fail ' + (typeof error === 'string' ? error : ''), fail, complete)
commonFailHandler('openBluetoothAdapter:fail', fail, complete, error)
})
}, 1000)
}).catch((error) => {
commonFailHandler('openBluetoothAdapter:fail ' + (typeof error === 'string' ? error : ''), fail, complete)
commonFailHandler('openBluetoothAdapter:fail', fail, complete, error)
})
}).catch(() => {
commonFailHandler('openBluetoothAdapter:fail no permission', fail, complete)
}).catch((error) => {
commonFailHandler('openBluetoothAdapter:fail', fail, complete, error)
})
}

Expand Down Expand Up @@ -150,9 +153,9 @@ function closeBluetoothAdapter (options = {}) {
BleManager.disconnect(id).catch(() => {})
})
connectedDevices.clear()
deviceFoundCallbacks.length = 0
deviceFoundCallback = null
onStateChangeCallbacks.length = 0
characteristicCallbacks.length = 0
characteristicCallback = null
onBLEConnectionStateCallbacks.length = 0
if (valueForCharacteristicSubscriptions) {
valueForCharacteristicSubscriptions.remove()
Expand Down Expand Up @@ -194,7 +197,7 @@ function startBluetoothDevicesDiscovery (options = {}) {
} = options

if (!bleManagerInitialized) {
commonFailHandler('startBluetoothDevicesDiscovery:fail ble adapter hans\'t been opened or ble is unavailable.', fail, complete)
commonFailHandler('startBluetoothDevicesDiscovery:fail', fail, complete, 'ble adapter hans\'t been opened or ble is unavailable.')
return
}
DiscoverPeripheralSubscription = BleManager.onDiscoverPeripheral((device) => {
Expand All @@ -219,13 +222,11 @@ function startBluetoothDevicesDiscovery (options = {}) {
return
}
}
deviceFoundCallbacks.forEach(cb => {
if (type(cb) === 'Function') {
cb({
devices: [deviceInfo]
})
}
})
if (type(deviceFoundCallback) === 'Function') {
deviceFoundCallback({
devices: [deviceInfo]
})
}
getDevices.push(deviceInfo)
// 处理设备发现逻辑
})
Expand All @@ -247,7 +248,7 @@ function startBluetoothDevicesDiscovery (options = {}) {
success(result)
complete(result)
}).catch((error) => {
commonFailHandler('startBluetoothDevicesDiscovery:fail ' + (typeof error === 'string' ? error : ''), fail, complete)
commonFailHandler('startBluetoothDevicesDiscovery:fail', fail, complete, error)
})
}

Expand All @@ -256,7 +257,7 @@ function stopBluetoothDevicesDiscovery (options = {}) {
const { success = noop, fail = noop, complete = noop } = options

if (!bleManagerInitialized) {
commonFailHandler('stopBluetoothDevicesDiscovery:fail ble adapter hans\'t been opened or ble is unavailable.', fail, complete)
commonFailHandler('stopBluetoothDevicesDiscovery:fail', fail, complete, 'ble adapter hans\'t been opened or ble is unavailable.')
return
}
removeBluetoothDevicesDiscovery()
Expand All @@ -276,29 +277,28 @@ function stopBluetoothDevicesDiscovery (options = {}) {
success(result)
complete(result)
}).catch((error) => {
commonFailHandler('stopBluetoothDevicesDiscovery:fail ' + (typeof error === 'string' ? error : ''), fail, complete)
commonFailHandler('stopBluetoothDevicesDiscovery:fail', fail, complete, error)
})
}

function onBluetoothDeviceFound (callback) {
if (deviceFoundCallbacks.indexOf(callback) === -1) {
deviceFoundCallbacks.push(callback)
if (type(callback) !== 'Function') {
console.warn('onBluetoothDeviceFound: callback 应为函数,已忽略')
return
}
deviceFoundCallback = callback // 只允许一个回调,重复添加被最新覆盖
}

function offBluetoothDeviceFound (callback) {
const index = deviceFoundCallbacks.indexOf(callback)
if (index > -1) {
deviceFoundCallbacks.splice(index, 1)
}
function offBluetoothDeviceFound () {
deviceFoundCallback = null // 移除所有回调(当前仅有一个)
}

function getConnectedBluetoothDevices (options = {}) {
const BleManager = require('react-native-ble-manager').default
const { services = [], success = noop, fail = noop, complete = noop } = options

if (!bleManagerInitialized) {
commonFailHandler('getConnectedBluetoothDevices:fail 请先调用 wx.openBluetoothAdapter 接口进行初始化操作', fail, complete)
commonFailHandler('getConnectedBluetoothDevices:fail', fail, complete, '请先调用 wx.openBluetoothAdapter 接口进行初始化操作')
return
}

Expand All @@ -314,7 +314,7 @@ function getConnectedBluetoothDevices (options = {}) {
success(result)
complete(result)
}).catch((error) => {
commonFailHandler('getConnectedBluetoothDevices:fail ' + (typeof error === 'string' ? error : ''), fail, complete)
commonFailHandler('getConnectedBluetoothDevices:fail', fail, complete, error)
})
}

Expand All @@ -323,7 +323,7 @@ function getBluetoothAdapterState (options = {}) {
const { success = noop, fail = noop, complete = noop } = options

if (!bleManagerInitialized) {
commonFailHandler('getBluetoothAdapterState:fail ble adapter need open first.', fail, complete)
commonFailHandler('getBluetoothAdapterState:fail', fail, complete, 'ble adapter need open first.')
return
}

Expand All @@ -336,7 +336,7 @@ function getBluetoothAdapterState (options = {}) {
success(result)
complete(result)
}).catch((error) => {
commonFailHandler('getBluetoothAdapterState:fail ' + (typeof error === 'string' ? error : ''), fail, complete)
commonFailHandler('getBluetoothAdapterState:fail', fail, complete, error)
})
}
function onDidUpdateState () {
Expand Down Expand Up @@ -366,6 +366,10 @@ function onDidUpdateState () {
}

function onBluetoothAdapterStateChange (callback) {
if (type(callback) !== 'Function') {
console.warn('onBluetoothAdapterStateChange: callback 应为函数,已忽略')
return
}
if (!updateStateSubscription) {
onDidUpdateState()
}
Expand All @@ -374,12 +378,17 @@ function onBluetoothAdapterStateChange (callback) {
}
}

// 注意:微信官方文档标识没有标识 API 不支持传入 callback 参数,但实际测试发现支持传入 callback 进行移除指定回调
function offBluetoothAdapterStateChange (callback) {
const index = onStateChangeCallbacks.indexOf(callback)
if (index > -1) {
onStateChangeCallbacks.splice(index, 1)
if (callback == null) {
onStateChangeCallbacks.length = 0
} else {
const index = onStateChangeCallbacks.indexOf(callback)
if (index !== -1) {
onStateChangeCallbacks.splice(index, 1)
}
}
if (deviceFoundCallbacks.length === 0) {
if (onStateChangeCallbacks.length === 0) {
removeUpdateStateSubscription()
}
}
Expand Down Expand Up @@ -416,8 +425,8 @@ function writeBLECharacteristicValue (options = {}) {
}
let writeTypeValue = writeType
if (!writeType) {
// 与小程序拉齐 iOS 未传值的情况优先 write,安卓优先 writeNoResponse 。
writeTypeValue = __mpx_mode__ === 'ios' ? 'write' : 'writeNoResponse'
// 与小程序拉齐iOS / 鸿蒙未传值优先 write,安卓优先 writeNoResponse 。
writeTypeValue = (__mpx_mode__ === 'ios' || __mpx_mode__ === 'harmony') ? 'write' : 'writeNoResponse'
}
// 将ArrayBuffer转换为byte array
const bytes = Array.from(new Uint8Array(value))
Expand Down Expand Up @@ -531,10 +540,15 @@ function notifyBLECharacteristicValueChange (options = {}) {

let valueForCharacteristicSubscriptions = null
function onBLECharacteristicValueChange (callback) {
if (type(callback) !== 'Function') {
console.warn('onBLECharacteristicValueChange: callback 应为函数,已忽略')
return
}
characteristicCallback = callback // 只允许一个回调,重复添加被最新覆盖
const BleManager = require('react-native-ble-manager').default
if (characteristicCallbacks.length === 0) {
if (!valueForCharacteristicSubscriptions) {
valueForCharacteristicSubscriptions = BleManager.onDidUpdateValueForCharacteristic((data) => {
// 将byte array转换为ArrayBuffer
if (type(characteristicCallback) !== 'Function') return
const buffer = new ArrayBuffer(data.value.length)
const view = new Uint8Array(buffer)
data.value.forEach((byte, index) => {
Expand All @@ -546,24 +560,14 @@ function onBLECharacteristicValueChange (callback) {
characteristicId: data.characteristic,
value: buffer
}
characteristicCallbacks.forEach(cb => {
if (type(cb) === 'Function') {
cb(result)
}
})
characteristicCallback(result)
})
}
if (characteristicCallbacks.indexOf(callback) === -1) {
characteristicCallbacks.push(callback)
}
}

function offBLECharacteristicValueChange (callback) {
const index = characteristicCallbacks.indexOf(callback)
if (index > -1) {
characteristicCallbacks.splice(index, 1)
}
if (characteristicCallbacks.length === 0 && valueForCharacteristicSubscriptions) {
function offBLECharacteristicValueChange () {
characteristicCallback = null
if (valueForCharacteristicSubscriptions) {
valueForCharacteristicSubscriptions.remove()
valueForCharacteristicSubscriptions = null
}
Expand All @@ -572,16 +576,16 @@ function offBLECharacteristicValueChange (callback) {
function setBLEMTU (options = {}) {
const BleManager = require('react-native-ble-manager').default
const { deviceId, mtu, success = noop, fail = noop, complete = noop } = options
if (!mtu) {
commonFailHandler('setBLEMTU:fail parameter error: parameter.mtu should be Number instead of Undefined;', fail, complete)
if (!deviceId && !mtu) {
commonFailHandler('setBLEMTU:fail', fail, complete, 'parameter error: parameter.deviceId should be String instead of Undefined;parameter.mtu should be Number instead of Undefined;')
return
}
if (!deviceId) {
commonFailHandler('setBLEMTU:fail parameter error: parameter.deviceId should be String instead of Undefined;', fail, complete)
if (!mtu) {
commonFailHandler('setBLEMTU:fail', fail, complete, 'parameter error: parameter.mtu should be Number instead of Undefined;')
return
}
if (!deviceId && !mtu) {
commonFailHandler('setBLEMTU:fail parameter error: parameter.deviceId should be String instead of Undefined;parameter.mtu should be Number instead of Undefined;', fail, complete)
if (!deviceId) {
commonFailHandler('setBLEMTU:fail', fail, complete, 'parameter error: parameter.deviceId should be String instead of Undefined;')
return
}

Expand Down Expand Up @@ -815,6 +819,10 @@ function closeBLEConnection (options = {}) {
}

function onBLEConnectionStateChange (callback) {
if (type(callback) !== 'Function') {
console.warn('onBLEConnectionStateChange: callback 应为函数,已忽略')
return
}
if (!updateStateSubscription) {
onDidUpdateState()
}
Expand All @@ -824,9 +832,13 @@ function onBLEConnectionStateChange (callback) {
}

function offBLEConnectionStateChange (callback) {
const index = onBLEConnectionStateCallbacks.indexOf(callback)
if (index !== -1) {
onBLEConnectionStateCallbacks.splice(index, 1)
if (callback == null) {
onBLEConnectionStateCallbacks.length = 0
} else {
const index = onBLEConnectionStateCallbacks.indexOf(callback)
if (index !== -1) {
onBLEConnectionStateCallbacks.splice(index, 1)
}
}
if (onBLEConnectionStateCallbacks.length === 0) {
removeUpdateStateSubscription()
Expand Down
Loading