uniapp 开发安卓应用连接蓝牙打印

uniapp安卓打印

在main.js 中将打印挂载到 全局的原型链上代码如下

import Vue from 'vue'
import App from './App'
import store from './store'
import $http from '@/config';

Vue.config.productionTip = false

App.mpType = 'app'

// 引入全局uView
import uView from 'uview-ui'
Vue.use(uView)
 
let vuexStore = require('@/store/$u.mixin.js')
Vue.mixin(vuexStore)

Vue.prototype.$http = $http

let main, Context, BluetoothManager, BluetoothAdapter, BManager, BAdapter, BluetoothDevice, IntentFilter,
	bluetoothSocket, device;
let that = this;

Vue.prototype.print_connect = function (val) {
	uni.showLoading({
		title: '正在打印',
		mask: true
	});
	// 打开蓝牙模块
	plus.bluetooth.openBluetoothAdapter({
		success: function (e) {
			console.log('初始化蓝牙适配器成功')
			console.log('open success: ' + JSON.stringify(e));
			plus.bluetooth.openBluetoothAdapter({
				success: function (e) {
					console.log('open success: ' + JSON.stringify(e));
					plus.bluetooth.startBluetoothDevicesDiscovery({
						success: function (e) {
							console.log('start discovery success: ' + JSON.stringify(e));
						},
						fail: function (e) {
							console.log('start discovery failed: ' + JSON.stringify(e));
						}
					});
				},
				fail: function (e) {
					console.log('请打开蓝牙和定位功能')
					console.log('open failed: ' + JSON.stringify(e));
				}
			});
		},
		fail: function (e) {
			console.log('open failed: ' + JSON.stringify(e));
		}
	});
	
	//获取android应用Activity对象
	main = plus.android.runtimeMainActivity();
	console.log()
	Context = plus.android.importClass("android.content.Context");
	BManager = main.getSystemService(Context.BLUETOOTH_SERVICE);
	//蓝牙适配器
	BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
	//蓝牙本地适配器
	BAdapter = BluetoothAdapter.getDefaultAdapter();
	//蓝牙设备
	BluetoothDevice = plus.android.importClass('android.bluetooth.BluetoothDevice');
	//过滤器
	IntentFilter = plus.android.importClass('android.content.IntentFilter');
	// this.PrintTemplete(this.model)
	// this.bluetooth_list()
	// this.search_pipei()


	// console.log('当前位置的经度:' + res.longitude);
	// console.log('当前位置的纬度:' + res.latitude);
	//先清空
	// that.match_list = []

	var lists = BAdapter.getBondedDevices();
	console.log("蓝牙列表",lists);
	plus.android.importClass(lists);
	var iterator = lists.iterator();
	plus.android.importClass(iterator);
	while (iterator.hasNext()) {
		var d = iterator.next();
		plus.android.importClass(d);
		let arr = {
			"name": d.getName(),
			"SN": d.getAddress()
		}
		console.log(arr)
		// that.match_list.push(arr)
		// console.log(that.match_list[0].SN)
		// that.print_bluetooth(that.match_list[0].SN)
		let UUID = plus.android.importClass("java.util.UUID");
		let uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");//自动识别打印机的 uuid码
		device = BAdapter.getRemoteDevice(arr.SN);
		plus.android.importClass(device);
		bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
		plus.android.importClass(bluetoothSocket);
		bluetoothSocket.connect();

		console.log('设备已连接')
		var receiver = plus.android.implements('io.dc

你可能感兴趣的:(vue,前端,android,javascript)