/**
 * 蓝牙相关接口
 */
import {callNative} from './util';
import shortId from 'shortid';

// 蓝牙通用
const bluetoothCommon = {
	/**
	 * 初始化蓝牙设备
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	openBluetoothAdapter(options = {
		mode: 'central'
	}) {
		callNative('openBluetoothAdapter', options, {}, 'openBluetoothAdapter', {
			errCode: true
		});
	},
	/**
	 * 关闭蓝牙模块
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	closeBluetoothAdapter(options = {}) {
		callNative('closeBluetoothAdapter', options, {}, 'closeBluetoothAdapter', {
			errCode: true
		});
	},
	/**
	 * 开始搜寻附近的蓝牙外围设备
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	startBluetoothDevicesDiscovery(options = {
		services: [],
		allowDuplicatesKey: false,
		interval: 0,
		powerLevel: 'medium'
	}) {
		callNative('startBluetoothDevicesDiscovery', options, {}, 'startBluetoothDevicesDiscovery', {
			errCode: true
		});
	},
	/**
	 * 停止搜寻附近的蓝牙外围设备
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	stopBluetoothDevicesDiscovery(options = {}) {
		callNative('stopBluetoothDevicesDiscovery', options, {}, 'stopBluetoothDevicesDiscovery', {
			errCode: true
		});
	},
	/**
	 * 根据主服务 UUID 获取已连接的蓝牙设备。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	getConnectedBluetoothDevices(options = {
		services: []
	}) {
		callNative('getConnectedBluetoothDevices', options, {
			services: 'Array'
		}, 'getConnectedBluetoothDevices', {
			errCode: true
		});
	},
	/**
	 * 获取在蓝牙模块生效期间所有搜索到的蓝牙设备。包括已经和本机处于连接状态的设备。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	getBluetoothDevices(options = {}) {
		let _successBackup = options && options.success ? options.success: ()=>{};
		let _success = (res) => {
			if (res && res.devices && Array.isArray(res.devices) && res.devices.length > 0) {
				res.devices.forEach(x => {
					if (x && x.advertisData && typeof x.advertisData === 'string') {
						x.advertisData = global.__biliCacheData.util.urlBase64ToArrayBuffer(x.advertisData);
					}
				})
			}
			_successBackup(res);
		}
		callNative('getBluetoothDevices', Object.assign(options, { success: _success }), {}, 'getBluetoothDevices', {
			errCode: true
		});
	},
	/**
	 * 获取本机蓝牙适配器状态。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	getBluetoothAdapterState(options = {}) {
		callNative('getBluetoothAdapterState', options, {}, 'getBluetoothAdapterState', {
			errCode: true
		});
	},
	/**
	 * 监听搜索到新设备的事件。
	 * @param {Function} callback
	 */
	onBluetoothDeviceFound(callback = () => {
	}) {
		if (typeof callback === 'function') {
			let _onBluetoothDeviceFoundCallback = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBluetoothDeviceFound;
			_onBluetoothDeviceFoundCallback.push(callback);
		}
	},
	/**
	 * 取消监听搜索到新设备的事件。
	 * @param {Function} callback
	 */
	offBluetoothDeviceFound(callback) {
		if (typeof callback === 'function') {
			const index = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBluetoothDeviceFound.indexOf(callback);
			if (index >= 0) {
				global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBluetoothDeviceFound.splice(index, 1);
			}
		} else {
			global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBluetoothDeviceFound = [];
		}
	},
	/**
	 * 监听蓝牙适配器状态变化事件。
	 * @param {Function} callback
	 */
	onBluetoothAdapterStateChange(callback = () => {
	}) {
		if (typeof callback === 'function') {
			let _onBluetoothAdapterStateChangeCallback = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBluetoothAdapterStateChange;
			_onBluetoothAdapterStateChangeCallback.push(callback);
		}
	},
	/**
	 * 取消监听蓝牙适配器状态变化事件。
	 * @param {Function} callback
	 */
	offBluetoothAdapterStateChange(callback) {
		if (typeof callback === 'function') {
			const index = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBluetoothAdapterStateChange.indexOf(callback);
			if (index >= 0) {
				global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBluetoothAdapterStateChange.splice(index, 1);
			}
		} else {
			global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBluetoothAdapterStateChange = [];
		}
	},
	/**
	 * 蓝牙配对接口，仅安卓支持。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	makeBluetoothPair(options = {
		deviceId: '',
		pin: '',
		timeout: 20000
	}) {
		callNative('makeBluetoothPair', options, {
			deviceId: 'String',
			pin: 'String'
		}, 'makeBluetoothPair', {
			errCode: true
		});
	},
	/**
	 * 查询蓝牙设备是否配对，仅安卓支持。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	isBluetoothDevicePaired(options = {
		deviceId: ''
	}) {
		callNative('isBluetoothDevicePaired', options, {
			deviceId: 'String'
		}, 'isBluetoothDevicePaired', {
			errCode: true
		});
	},

	getBondedBluetoothDevices(options = {}){
		callNative('getBondedBluetoothDevices', options, {}, 'getBondedBluetoothDevices', {
			errCode: true
		});
	}
};

// 蓝牙低功耗中心设备
const bluetoothCenter = {
	/**
	 * 连接蓝牙低功耗设备。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	createBLEConnection(options = {
		deviceId: ''
	}) {
		callNative('createBLEConnection', options, {
			deviceId: 'String'
		}, 'createBLEConnection', {
			errCode: true
		});
	},
	/**
	 * 断开蓝牙低功耗设备。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	closeBLEConnection(options = {
		deviceId: ''
	}) {
		callNative('closeBLEConnection', options, {
			deviceId: 'String'
		}, 'closeBLEConnection', {
			errCode: true
		});
	},
	/**
	 * 获取蓝牙低功耗设备所有服务 (service)。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	getBLEDeviceServices(options = {
		deviceId: ''
	}) {
		callNative('getBLEDeviceServices', options, {
			deviceId: 'String'
		}, 'getBLEDeviceServices', {
			errCode: true
		});
	},
	/**
	 * 获取蓝牙低功耗设备某个服务中所有特征 (characteristic)。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	getBLEDeviceCharacteristics(options = {
		deviceId: '',
		serviceId: ''
	}) {
		callNative('getBLEDeviceCharacteristics', options, {
			deviceId: 'String',
			serviceId: 'String'
		}, 'getBLEDeviceCharacteristics', {
			errCode: true
		});
	},
	/**
	 * 获取蓝牙低功耗设备某个服务中所有特征 (characteristic)。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	getBLEMTU(options = {
		deviceId: '',
		writeType: 'write'
	}) {
		callNative('getBLEMTU', options, {
			deviceId: 'String'
		}, 'getBLEMTU', {
			errCode: true
		});
	},
	/**
	 * 获取蓝牙低功耗设备的信号强度 (Received Signal Strength Indication, RSSI)。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	getBLEDeviceRSSI(options = {
		deviceId: ''
	}) {
		callNative('getBLEDeviceRSSI', options, {
			deviceId: 'String'
		}, 'getBLEDeviceRSSI', {
			errCode: true
		});
	},
	/**
	 * 获取蓝牙低功耗设备的信号强度 (Received Signal Strength Indication, RSSI)。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	notifyBLECharacteristicValueChange(options = {
		deviceId: '',
		serviceId: '',
		characteristicId: '',
		type: 'indication'
	}) {
		callNative('notifyBLECharacteristicValueChange', options, {
			deviceId: 'String',
			serviceId: 'String',
			characteristicId: 'String'
		}, 'notifyBLECharacteristicValueChange', {
			errCode: true
		});
	},
	/**
	 * 读取蓝牙低功耗设备特征值的二进制数据。注意：必须设备的特征支持 read 才可以成功调用。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	readBLECharacteristicValue(options = {
		deviceId: '',
		serviceId: '',
		characteristicId: ''
	}) {
		callNative('readBLECharacteristicValue', options, {
			deviceId: 'String',
			serviceId: 'String',
			characteristicId: 'String'
		}, 'readBLECharacteristicValue', {
			errCode: true
		});
	},
	/**
	 * 向蓝牙低功耗设备特征值中写入二进制数据。注意：必须设备的特征支持 write 才可以成功调用。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	writeBLECharacteristicValue(options = {
		deviceId: '',
		serviceId: '',
		characteristicId: '',
		value: [],
		writeType: ''
	}) {
		// 设置默认值
		if (options && options.writeType === '') {
			let temp = bl.getSystemInfoSync() || { platform: '' }
			if (temp.platform.toLowerCase() === 'ios') {
				options.writeType = 'write'
			} else {
				options.writeType = 'writeNoResponse'
			}
		}
		callNative('writeBLECharacteristicValue', options, {
			deviceId: 'String',
			serviceId: 'String',
			characteristicId: 'String',
			value: 'ArrayBuffer',
			writeType: 'String'
		}, 'writeBLECharacteristicValue', {
			errCode: true,
			requestFn: (callbackId, options)=>{
				// 特殊处理 arraybuffer
				options.value = global.__biliCacheData.util.transformArrayBufferToBase64(options.value)
			}
		});
	},
	/**
	 * 协商设置蓝牙低功耗的最大传输单元 (Maximum Transmission Unit, MTU)。需在 bl.createBLEConnection 调用成功后调用。仅安卓系统 5.1 以上版本有效，iOS 因系统限制不支持。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	setBLEMTU(options = {
		deviceId: '',
		mtu: 0
	}) {
		callNative('setBLEMTU', options, {
			deviceId: 'String',
			mtu: 'Number'
		}, 'setBLEMTU', {
			errCode: true
		});
	},
	/**
	 * 监听蓝牙低功耗连接状态的改变事件。包括开发者主动连接或断开连接，设备丢失，连接异常断开等等
	 * @param {Function} callback
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	onBLEConnectionStateChange(callback = ()=>{}) {
		if (typeof callback === 'function') {
			let _onBLEConnectionStateChange = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBLEConnectionStateChange;
			_onBLEConnectionStateChange.push(callback);
		}
	},
	/**
	 * 监听蓝牙低功耗连接状态的改变事件。包括开发者主动连接或断开连接，设备丢失，连接异常断开等等
	 * @param {Function} callback
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	onBLECharacteristicValueChange(callback = ()=>{}) {
		if (typeof callback === 'function') {
			let _onBLECharacteristicValueChange = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBLECharacteristicValueChange;
			_onBLECharacteristicValueChange.push(callback);
		}
	},
	/**
	 * 监听蓝牙低功耗连接状态的改变事件。包括开发者主动连接或断开连接，设备丢失，连接异常断开等等
	 * @param {Function} callback
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	onBLEMTUChange(callback = ()=>{}) {
		if (typeof callback === 'function') {
			let _onBLEMTUChange = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBLEMTUChange;
			_onBLEMTUChange.push(callback);
		}
	},
	/**
	 * 取消监听蓝牙低功耗连接状态的改变事件
	 * @param {Function} callback
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	offBLEConnectionStateChange(callback) {
		if (typeof callback === 'function') {
			const index = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBLEConnectionStateChange.indexOf(callback);
			if (index >= 0) {
				global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBLEConnectionStateChange.splice(index, 1);
			}
		} else {
			global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBLEConnectionStateChange = [];
		}
	},
	/**
	 * 取消监听蓝牙低功耗设备的特征值变化事件。
	 * @param {Function} callback
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	offBLECharacteristicValueChange(callback) {
		if (typeof callback === 'function') {
			const index = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBLECharacteristicValueChange.indexOf(callback);
			if (index >= 0) {
				global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBLECharacteristicValueChange.splice(index, 1);
			}
		} else {
			global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBLECharacteristicValueChange = [];
		}
	},
	/**
	 * 取消监听蓝牙低功耗的最大传输单元变化事件。
	 * @param {Function} callback
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	offBLEMTUChange(callback) {
		if (typeof callback === 'function') {
			const index = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBLEMTUChange.indexOf(callback);
			if (index >= 0) {
				global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBLEMTUChange.splice(index, 1);
			}
		} else {
			global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBLEMTUChange = [];
		}
	},
}

class BLEPeripheralServer {
	constructor(id) {
		this.id = id;
	}

	/**
	 * 添加服务。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	addService(options = {
		service: {}
	}) {
		callNative('peripheralServer.addService', {
			id: this.id,
			...options
		}, {
			service: 'Object'
		}, 'peripheralServer.addService', {
			errCode: true,
			requestFn: (callbackId, options) => {
				if (options.service.characteristics && Array.isArray(options.service.characteristics)) {
					options.service.characteristics.forEach(x => {
						if (x.value instanceof ArrayBuffer) {
							x.value = global.__biliCacheData.util.transformArrayBufferToBase64(x.value);
						}
						// 处理其他 arraybuff
						if (x.value && x.value.descriptors && Array.isArray(x.value.descriptors) && Array.isArray(x.value.descriptors).length > 0) {
							x.value.descriptors.forEach(y => {
								if (y.value instanceof ArrayBuffer) {
									y.value = global.__biliCacheData.util.transformArrayBufferToBase64(y.value);
								}
							})
						}
					})
				}
			}
		});
	}

	/**
	 * 移除服务。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	removeService(options = {
		serviceId: ''
	}) {
		callNative('peripheralServer.removeService', {
			id: this.id,
			...options
		}, {
			serviceId: 'String'
		}, 'peripheralServer.removeService', {
			errCode: true
		});
	}

	/**
	 * 开始广播本地创建的外围设备。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	startAdvertising(options = {
		advertiseRequest: {}
	}) {
		callNative('peripheralServer.startAdvertising', {
			id: this.id,
			...options
		}, {
			advertiseRequest: 'Object'
		}, 'peripheralServer.startAdvertising',{
			errCode: true,
			requestFn: (callbackId, options) => {
				// 特殊处理 base64
				if (options.advertiseRequest && Array.isArray(options.advertiseRequest)) {
					options.advertiseRequest.forEach(x => {
						if (x.manufacturerData && Array.isArray(x.manufacturerData)) {
							x.manufacturerData.forEach(y => {
								if (y.manufacturerSpecificData instanceof ArrayBuffer) {
									y.manufacturerSpecificData = global.__biliCacheData.util.transformArrayBufferToBase64(y.manufacturerSpecificData);
								}
							})
						}
					})
				}
			}
		});
	}

	/**
	 * 停止广播。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	stopAdvertising(options = {}) {
		callNative('peripheralServer.stopAdvertising', {
			id: this.id,
			...options
		}, {}, 'peripheralServer.stopAdvertising', {
			errCode: true
		});
	}

	/**
	 * 往指定特征写入二进制数据值，并通知已连接的主机，从机的特征值已发生变化，该接口会处理是走回包还是走订阅。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	writeCharacteristicValue(options = {
		serviceId: '',
		characteristicId: '',
		value: [],
		needNotify: false
	}) {
		callNative('peripheralServer.writeCharacteristicValue', {
			id: this.id,
			...options
		}, {
			serviceId: 'String',
			characteristicId: 'String',
			value: 'ArrayBuffer',
			needNotify: 'Boolean'
		}, 'peripheralServer.writeCharacteristicValue', {
			errCode: true,
			requestFn: (callbackId, options)=> {
				options.value = global.__biliCacheData.util.transformArrayBufferToBase64(options.value);
			}
		});
	}

	/**
	 * 关闭当前服务端。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	close(options = {}) {
		callNative('peripheralServer.close', {
			id: this.id,
			...options
		}, {}, 'peripheralServer.close', {
			errCode: true
		});
	}

	/**
	 * 监听已连接的设备请求读当前外围设备的特征值事件。收到该消息后需要立刻调用 writeCharacteristicValue 写回数据，否则主机不会收到响应。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	onCharacteristicWriteRequest(callback = () => {}) {
		if (typeof callback === 'function') {
			let _onCharacteristicWriteRequest = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onCharacteristicWriteRequest;
			_onCharacteristicWriteRequest.push(callback);
		}
	}

	/**
	 * 监听已连接的设备请求读当前外围设备的特征值事件。收到该消息后需要立刻调用 writeCharacteristicValue 写回数据，否则主机不会收到响应。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	onCharacteristicReadRequest(callback = () => {}) {
		if (typeof callback === 'function') {
			let _onCharacteristicReadRequest = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onCharacteristicReadRequest;
			_onCharacteristicReadRequest.push(callback);
		}
	}

	/**
	 * 监听特征订阅事件，仅 iOS 支持。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	onCharacteristicSubscribed(callback = () => {}) {
		if (typeof callback === 'function') {
			let _onCharacteristicSubscribed = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onCharacteristicSubscribed;
			_onCharacteristicSubscribed.push(callback);
		}
	}

	/**
	 * 监听取消特征订阅事件，仅 iOS 支持。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	onCharacteristicUnsubscribed(callback = () => {}) {
		if (typeof callback === 'function') {
			let _onCharacteristicUnsubscribed = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onCharacteristicUnsubscribed;
			_onCharacteristicUnsubscribed.push(callback);
		}
	}

	/**
	 * 取消监听已连接的设备请求写当前外围设备的特征值事件
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	offCharacteristicWriteRequest(callback) {
		if (typeof callback === 'function') {
			const index = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onCharacteristicWriteRequest.indexOf(callback);
			if (index >= 0) {
				global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onCharacteristicWriteRequest.splice(index, 1);
			}
		} else {
			global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onCharacteristicWriteRequest = [];
		}
	}

	/**
	 * 取消监听已连接的设备请求读当前外围设备的特征值事件
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	offCharacteristicReadRequest(callback) {
		if (typeof callback === 'function') {
			const index = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onCharacteristicReadRequest.indexOf(callback);
			if (index >= 0) {
				global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onCharacteristicReadRequest.splice(index, 1);
			}
		} else {
			global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onCharacteristicReadRequest = [];
		}
	}

	/**
	 * 取消监听特征订阅事件
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	offCharacteristicSubscribed(callback) {
		if (typeof callback === 'function') {
			const index = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onCharacteristicSubscribed.indexOf(callback);
			if (index >= 0) {
				global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onCharacteristicSubscribed.splice(index, 1);
			}
		} else {
			global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onCharacteristicSubscribed = [];
		}
	}

	/**
	 * 取消监听取消特征订阅事件
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	offCharacteristicUnsubscribed(callback) {
		if (typeof callback === 'function') {
			const index = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onCharacteristicUnsubscribed.indexOf(callback);
			if (index >= 0) {
				global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onCharacteristicUnsubscribed.splice(index, 1);
			}
		} else {
			global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onCharacteristicUnsubscribed = [];
		}
	}

}

// 蓝牙低功耗外围设备
const bluetoothBeside = {
	/**
	 * 建立本地作为蓝牙低功耗外围设备的服务端，可创建多个。
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	createBLEPeripheralServer(options = {}) {
		const _shortId = shortId.generate();
		const _successCallback = () => {
			if (options && options.success && typeof options.success === 'function') {
				options.success({
					server: new BLEPeripheralServer(_shortId)
				})
			}
		}
		callNative('createBLEPeripheralServer', {
			id: _shortId,
			success: _successCallback,
			fail: options && options.fail && typeof options.fail === 'function'? options.fail : ()=>{},
			complete: options && options.complete && typeof options.complete === 'function'? options.complete : ()=>{}
		}, {}, 'createBLEPeripheralServer', {
			errCode: true
		});
	},
	/**
	 * 监听当前外围设备被连接或断开连接事件
	 * @param {Object} options
	 * @param {Function} options.success 成功回调
	 * @param {Function} options.fail 失败回调
	 */
	onBLEPeripheralConnectionStateChanged(callback = () => {}) {
		if (typeof callback === 'function') {
			let _onBLEPeripheralConnectionStateChanged = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBLEPeripheralConnectionStateChanged;
			_onBLEPeripheralConnectionStateChanged.push(callback);
		}
	},
	/**
	 * 取消监听当前外围设备被连接或断开连接事件
	 * @param {Function} callback
	 */
	offBLEPeripheralConnectionStateChanged(callback) {
		if (typeof callback === 'function') {
			const index = global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBLEPeripheralConnectionStateChanged.indexOf(callback);
			if (index >= 0) {
				global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBLEPeripheralConnectionStateChanged.splice(index, 1);
			}
		} else {
			global.__biliCacheData.eventHandlers.bluetoothChangeHandlers.onBLEPeripheralConnectionStateChanged = [];
		}
	}
}

export default {
	...bluetoothCommon,
	...bluetoothCenter,
	...bluetoothBeside
};
