| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 | /** * html 5+ 串口蓝牙操作 * 2021.04.23 uni-app版本 * @auth boolTrue *//** * 初始化参数 */ //#ifdef APP-PLUSlet BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");let Intent = plus.android.importClass("android.content.Intent");let IntentFilter = plus.android.importClass("android.content.IntentFilter");let BluetoothDevice = plus.android.importClass("android.bluetooth.BluetoothDevice");let UUID = plus.android.importClass("java.util.UUID");let Toast = plus.android.importClass("android.widget.Toast");//连接串口设备的 UUIDlet MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");let invoke = plus.android.invoke;let btAdapter = BluetoothAdapter.getDefaultAdapter();let activity = plus.android.runtimeMainActivity();let btSocket = null;let btInStream = null;let btOutStream = null;let setIntervalId = 0;let btFindReceiver = null; //蓝牙搜索广播接收器let btStatusReceiver = null; //蓝牙状态监听广播//#endif/** * 构造对象 */ var blueToothTool = {	state : {		bluetoothEnable: false, //蓝牙是否开启		bluetoothState: "", //当前蓝牙状态		discoveryDeviceState: false, //是否正在搜索蓝牙设备		readThreadState: false, //数据读取线程状态	},	options : {		/**		 * 监听蓝牙状态回调		 * @param {String} state		 */		listenBTStatusCallback: function(state) {},		/**		 * 搜索到新的蓝牙设备回调		 * @param {Device} newDevice		 */		discoveryDeviceCallback: function(newDevice) {},		/**		 * 蓝牙搜索完成回调		 */		discoveryFinishedCallback: function() {},		/**		 * 接收到数据回调		 * @param {Array} dataByteArr		 */		readDataCallback: function(dataByteArr) {},		/**		 * 蓝牙连接中断回调		 * @param {Exception} e		 */		connExceptionCallback: function(e) {}	},	init(setOptions) {		Object.assign(this.options, setOptions);		this.state.bluetoothEnable = this.getBluetoothStatus();		this.listenBluetoothStatus();	},	shortToast(msg) {		Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();	},	/**	 * 是否支持蓝牙	 * @return {boolean}	 */	isSupportBluetooth() {		if(btAdapter != null) {			return true;		}		return false;	},	/**	 * 获取蓝牙的状态	 * @return {boolean} 是否已开启	 */	getBluetoothStatus() {		if(btAdapter != null) {			return btAdapter.isEnabled();		}		return false;	},	/**	 * 打开蓝牙	 * @param activity	 * @param requestCode	 */	turnOnBluetooth() {		if(btAdapter == null) {			shortToast("没有蓝牙");			return;		}		if(!btAdapter.isEnabled()) {			if(activity == null) {				shortToast("未获取到activity");				return;			} else {				let intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);				let requestCode = 1;				activity.startActivityForResult(intent, requestCode);				return;			}		} else {			shortToast("蓝牙已经打开");		}	},	/**	 * 关闭蓝牙	 */	turnOffBluetooth() {		if(btAdapter != null && btAdapter.isEnabled()) {			btAdapter.disable();		}		if(btFindReceiver != null) {			try {				activity.unregisterReceiver(btFindReceiver);			} catch(e) {			}			btFindReceiver = null;		}		this.state.bluetoothEnable = false;		this.cancelDiscovery();		closeBtSocket();		if(btAdapter != null && btAdapter.isEnabled()) {			btAdapter.disable();			shortToast("蓝牙关闭成功");		} else {			shortToast("蓝牙已经关闭");		}	},	/**	 * 获取已经配对的设备	 * @return {Array} connetedDevices	 */	getPairedDevices() {		let pairedDevices = [];		//蓝牙连接android原生对象,是一个set集合		let pairedDevicesAndroid = null;		if(btAdapter != null && btAdapter.isEnabled()) {			pairedDevicesAndroid = btAdapter.getBondedDevices();		} else {			shortToast("蓝牙未开启");		}		if(!pairedDevicesAndroid) {			return pairedDevices;		}		//遍历连接设备的set集合,转换为js数组		let it = invoke(pairedDevicesAndroid, "iterator");		while(invoke(it, "hasNext")) {			let device = invoke(it, "next");			pairedDevices.push({				"name": invoke(device, "getName"),				"address": invoke(device, "getAddress")			});		}		return pairedDevices;	},	/**	 * 发现设备	 */	discoveryNewDevice() {		if(btFindReceiver != null) {			try {				activity.unregisterReceiver(btFindReceiver);			} catch(e) {				console.error(e);			}			btFindReceiver = null;			this.cancelDiscovery();		}		let Build = plus.android.importClass("android.os.Build");				 //6.0以后的如果需要利用本机查找周围的wifi和蓝牙设备, 申请权限        if(Build.VERSION.SDK_INT >= 6.0){			        }		let options = this.options		btFindReceiver = plus.android.implements("io.dcloud.android.content.BroadcastReceiver", {			"onReceive": function(context, intent) {				plus.android.importClass(context);				plus.android.importClass(intent);				let action = intent.getAction();				if(BluetoothDevice.ACTION_FOUND == action) { // 找到设备					let device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);					let newDevice = {						"name": plus.android.invoke(device, "getName"),						"address": plus.android.invoke(device, "getAddress")					}					options.discoveryDeviceCallback && options.discoveryDeviceCallback(newDevice);				}				if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED == action) { // 搜索完成					cancelDiscovery();					options.discoveryFinishedCallback && options.discoveryFinishedCallback();				}			}		});		let filter = new IntentFilter();		filter.addAction(BluetoothDevice.ACTION_FOUND);		filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);		activity.registerReceiver(btFindReceiver, filter);		btAdapter.startDiscovery(); //开启搜索		this.state.discoveryDeviceState = true;	},	/**	 * 蓝牙状态监听	 * @param {Activity} activity	 */	listenBluetoothStatus() {		if(btStatusReceiver != null) {			try {				activity.unregisterReceiver(btStatusReceiver);			} catch(e) {				console.error(e);			}			btStatusReceiver = null;		}		btStatusReceiver = plus.android.implements("io.dcloud.android.content.BroadcastReceiver", {			"onReceive": (context, intent)=> {				plus.android.importClass(context);				plus.android.importClass(intent);				let action = intent.getAction();				switch(action) {					case BluetoothAdapter.ACTION_STATE_CHANGED:						let blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);						let stateStr = "";						switch(blueState) {							case BluetoothAdapter.STATE_TURNING_ON:								stateStr = "STATE_TURNING_ON";								break;							case BluetoothAdapter.STATE_ON:								this.state.bluetoothEnable = true;								stateStr = "STATE_ON";								break;							case BluetoothAdapter.STATE_TURNING_OFF:								stateStr = "STATE_TURNING_OFF";								break;							case BluetoothAdapter.STATE_OFF:								stateStr = "STATE_OFF";								this.state.bluetoothEnable = false;								break;						}						this.state.bluetoothState = stateStr;						this.options.listenBTStatusCallback && this.options.listenBTStatusCallback(stateStr);						break;				}			}		});		let filter = new IntentFilter();		filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);		activity.registerReceiver(btStatusReceiver, filter);		// 首次连接 状态回调		if(this.state.bluetoothEnable) {			this.options.listenBTStatusCallback && this.options.listenBTStatusCallback('STATE_ON');		}	},	/**	 * 根据蓝牙地址,连接设备	 * @param {Stirng} address	 * @return {Boolean}	 */	connDevice(address, callback) {		let InputStream = plus.android.importClass("java.io.InputStream");		let OutputStream = plus.android.importClass("java.io.OutputStream");		let BluetoothSocket = plus.android.importClass("android.bluetooth.BluetoothSocket");		this.cancelDiscovery();		if(btSocket != null) {			this.closeBtSocket();		}		this.state.readThreadState = false;		try {			let device = invoke(btAdapter, "getRemoteDevice", address);			btSocket = invoke(device, "createRfcommSocketToServiceRecord", MY_UUID);		} catch(e) {			console.error(e);			shortToast("连接失败,获取Socket失败!");			callback(false)			return false;		}		try {			invoke(btSocket, "connect");			this.readData(); //读数据			this.shortToast("连接成功");			callback(true)		} catch(e) {			console.error(e);			this.shortToast("连接失败");			callback(false)			try {				btSocket.close();				btSocket = null;			} catch(e1) {				console.error(e1);			}			return false;		}		return true;	},	/**	 * 断开连接设备	 * @param {Object} address	 * @return {Boolean}	 */	disConnDevice() {		if(btSocket != null) {			this.closeBtSocket();		}		this.state.readThreadState = false;		this.shortToast("断开连接成功");	},	/**	 * 断开连接设备	 * @param {Object} address	 * @return {Boolean}	 */	closeBtSocket() {		this.state.readThreadState = false;		if(!btSocket) {			return;		}		try {			btSocket.close();		} catch(e) {			console.error(e);			btSocket = null;		}	},	/**	 * 取消发现	 */	cancelDiscovery() {		if(btAdapter.isDiscovering()) {			btAdapter.cancelDiscovery();		}		if(btFindReceiver != null) {			activity.unregisterReceiver(btFindReceiver);			btFindReceiver = null;		}		this.state.discoveryDeviceState = false;	},	/**	 * 读取数据	 * @param {Object} activity	 * @param {Function} callback	 * @return {Boolean}	 */	readData() {		if(!btSocket) {			this.shortToast("请先连接蓝牙设备!");			return false;		}		try {			btInStream = invoke(btSocket, "getInputStream");			btOutStream = invoke(btSocket, "getOutputStream");		} catch(e) {			console.error(e);			this.shortToast("创建输入输出流失败!");			this.closeBtSocket();			return false;		}		this.read();		this.state.readThreadState = true;		return true;	},	/**	 * 模拟java多线程读取数据	 */	read() {		let setTimeCount = 0;		clearInterval(setIntervalId);		setIntervalId = setInterval(()=> {			setTimeCount++;			if(this.state.readThreadState) {				let t = new Date().getTime();				//心跳检测				if(setTimeCount % 20 == 0) {					try {						btOutStream.write([0b00]);					} catch(e) {						this.state.readThreadState = false;						this.options.connExceptionCallback && this.options.connExceptionCallback(e);					}				}				let dataArr = [];				while(invoke(btInStream, "available") !== 0) {					let data = invoke(btInStream, "read");					dataArr.push(data);					let ct = new Date().getTime();					if(ct - t > 20) {						break;					}				}				if(dataArr.length > 0) {					this.options.readDataCallback && this.options.readDataCallback(dataArr);				}			}		}, 40);	},	/**	 * 发送数据	 * @param {String} dataStr	 * @return {Boolean}	 */	sendData(dataStr) {		if(!btOutStream) {			this.shortToast("创建输出流失败!");			return;		}		let bytes = invoke(dataStr, 'getBytes', 'gbk');		try {			btOutStream.write(bytes);		} catch(e) {			return false;		}		return true;	},	sendByteData(byteData) {		if(!btOutStream) {			this.shortToast("创建输出流失败!");			return;		}		try {			btOutStream.write(byteData);		} catch(e) {			return false;		}		return true;	}}module.exports = blueToothTool
 |