var main = plus.android.runtimeMainActivity()
var BluetoothAdapter = plus.android.importClass(
'android.bluetooth.BluetoothAdapter'
)
var IntentFilter = plus.android.importClass('android.content.IntentFilter')
var BluetoothDevice = plus.android.importClass(
'android.bluetooth.BluetoothDevice'
)
var BAdapter = new BluetoothAdapter.getDefaultAdapter()
BAdapter.isEnabled()
BAdapter.enable()
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('open failed: ' + JSON.stringify(e))
}
})
plus.bluetooth.getBluetoothDevices({
success: function(e) {
var devices = e.devices
console.log('get devices success: ' + e.length)
for (var i in devices) {
console.log(i + ': ' + JSON.stringify(devices[i]))
}
},
fail: function(e) {
console.log('get devices failed: ' + JSON.stringify(e))
}
})
plus.bluetooth.stopBluetoothDevicesDiscovery({
success: function(e) {
console.log('stop discovery success: ' + JSON.stringify(e))
plus.bluetooth.closeBluetoothAdapter({
success: function(e) {
console.log('close success: ' + JSON.stringify(e))
},
fail: function(e) {
console.log('close failed: ' + JSON.stringify(e))
}
})
},
fail: function(e) {
console.log('stop discovery failed: ' + JSON.stringify(e))
}
})
UUID = plus.android.importClass('java.util.UUID')
uuid = UUID.fromString('00001101-0000-1000-8000-00805F9B34FB') //不需要更改
device = BAdapter.getRemoteDevice('DC:1D:30:34:62:A6') //这里是蓝牙打印机的蓝牙地址
plus.android.importClass(device)
bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid)
plus.android.importClass(bluetoothSocket)
bluetoothSocket.connect() //连接
bluetoothSocket.isConnected()
var outputStream = bluetoothSocket.getOutputStream()
plus.android.importClass(outputStream)
// 打印字符串
var s = plus.android.importClass('java.lang.String')
var string = new s('测试数据' + new Date().getMilliseconds() + '\n\n\n\n') //必须以创建字符串对象的形式创建对象,否则返回NULL
var bytes = string.getBytes('gbk')
outputStream.write(bytes)
outputStream.flush()
let Util = {
RESET: [27, 64], // 复位
ALIGN_LEFT: [27, 97, 0], // 左对齐
ALIGN_CENTER: [27, 97, 1], // 居中
ALIGN_RIGHT: [27, 97, 2], // 右对齐
BOLD: [27, 69, 1], // 加粗
BOLD_CANCEL: [27, 69, 0], //取消加粗
DOUBLE_HEIGHT_WIDTH: [29, 33, 17], //宽高加倍
DOUBLE_WIDTH: [29, 33, 16], //宽加倍
DOUBLE_HEIGHT: [29, 33, 1], //高加倍
NORMAL: [29, 33, 0], //字体不放大
LINE_SPACING_DEFAULT: [27, 50] // 设置默认间距
}
function setFormat(command) {
outputStream.write(command)
outputStream.flush()
}
function PrintText(text) {
var s = plus.android.importClass('java.lang.String')
var string = new s(text)
var bytes = string.getBytes('gbk')
outputStream.write(bytes)
outputStream.flush()
}
// 获取字符长度
function GetLength(str) {
return str.replace(/[\u0391-\uFFE5]/g, 'aa').length //先把中文替换成两个字节的英文,在计算长度
}
// 输出两列,不能超过32字符
function printTowData(text1, text2) {
let textLength1 = GetLength(text1)
let textLength2 = GetLength(text2)
let blankLength = 32 - textLength1 - textLength2
let blank = ''
for (let i = 0; i < blankLength; i++) {
blank = blank + ' '
}
return text1 + blank + text2
}
PrintText(printTowData('哈哈哈', '嘿嘿嘿'))

8***@qq.com
- 发布:2019-04-17 08:27
- 更新:2019-05-01 15:56
- 阅读:2269
1 个评论
要回复文章请先登录或注册
n***@qq.com