Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mes-pad
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ximai
mes-pad
Commits
e1bfa2c2
Commit
e1bfa2c2
authored
Aug 14, 2024
by
沈翠玲
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
蓝牙打印机对接
parent
a1998bc4
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
100 additions
and
34 deletions
+100
-34
BluetoothTool.js
components/gprint/BluetoothTool.js
+52
-8
$u.mixin.js
store/$u.mixin.js
+48
-26
No files found.
components/gprint/BluetoothTool.js
View file @
e1bfa2c2
...
...
@@ -70,7 +70,7 @@ var blueToothTool = {
init
(
setOptions
)
{
Object
.
assign
(
this
.
options
,
setOptions
);
this
.
state
.
bluetoothEnable
=
this
.
getBluetoothStatus
();
if
(
!
this
.
state
.
bluetoothEnable
)
shortToast
(
"没有开启蓝牙"
);
if
(
!
this
.
state
.
bluetoothEnable
)
this
.
shortToast
(
"没有开启蓝牙"
);
this
.
listenBluetoothStatus
();
},
shortToast
(
msg
)
{
...
...
@@ -103,12 +103,12 @@ var blueToothTool = {
*/
turnOnBluetooth
()
{
if
(
btAdapter
==
null
)
{
shortToast
(
"没有蓝牙"
);
this
.
shortToast
(
"没有蓝牙"
);
return
;
}
if
(
!
btAdapter
.
isEnabled
())
{
if
(
activity
==
null
)
{
shortToast
(
"未获取到activity"
);
this
.
shortToast
(
"未获取到activity"
);
return
;
}
else
{
let
intent
=
new
Intent
(
BluetoothAdapter
.
ACTION_REQUEST_ENABLE
);
...
...
@@ -117,7 +117,7 @@ var blueToothTool = {
return
;
}
}
else
{
shortToast
(
"蓝牙已经打开"
);
this
.
shortToast
(
"蓝牙已经打开"
);
}
},
/**
...
...
@@ -141,9 +141,9 @@ var blueToothTool = {
if
(
btAdapter
!=
null
&&
btAdapter
.
isEnabled
())
{
btAdapter
.
disable
();
shortToast
(
"蓝牙关闭成功"
);
this
.
shortToast
(
"蓝牙关闭成功"
);
}
else
{
shortToast
(
"蓝牙已经关闭"
);
this
.
shortToast
(
"蓝牙已经关闭"
);
}
},
/**
...
...
@@ -158,7 +158,7 @@ var blueToothTool = {
if
(
btAdapter
!=
null
&&
btAdapter
.
isEnabled
())
{
pairedDevicesAndroid
=
btAdapter
.
getBondedDevices
();
}
else
{
shortToast
(
"蓝牙未开启"
);
this
.
shortToast
(
"蓝牙未开启"
);
}
if
(
!
pairedDevicesAndroid
)
{
...
...
@@ -348,7 +348,50 @@ var blueToothTool = {
btSocket
=
invoke
(
device
,
"createRfcommSocketToServiceRecord"
,
MY_UUID
);
}
catch
(
e
)
{
console
.
error
(
e
);
shortToast
(
"连接失败,获取Socket失败!"
);
this
.
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 {Stirng} address
* @return {Boolean}
*/
connPairDevice
(
device
,
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"
);
if
(
btSocket
!=
null
)
{
this
.
closeBtSocket
();
}
this
.
state
.
readThreadState
=
false
;
try
{
console
.
log
(
'device'
,
device
)
btSocket
=
invoke
(
device
,
"createRfcommSocketToServiceRecord"
,
MY_UUID
);
}
catch
(
e
)
{
console
.
error
(
e
);
this
.
shortToast
(
"连接失败,获取Socket失败!"
);
callback
(
false
)
return
false
;
}
...
...
@@ -427,6 +470,7 @@ var blueToothTool = {
try
{
btInStream
=
invoke
(
btSocket
,
"getInputStream"
);
btOutStream
=
invoke
(
btSocket
,
"getOutputStream"
);
console
.
log
(
'btOutStream'
,
btOutStream
)
}
catch
(
e
)
{
console
.
error
(
e
);
this
.
shortToast
(
"创建输入输出流失败!"
);
...
...
store/$u.mixin.js
View file @
e1bfa2c2
...
...
@@ -48,28 +48,35 @@ module.exports = {
initBle
(){
//#ifdef APP-PLUS
// 蓝牙
try
{
this
.
$u
.
vuex
(
'vuex_printConnect'
,
false
);
bluetoothTool
.
init
({
listenBTStatusCallback
:
(
state
)
=>
{
console
.
log
(
'listenBTStatusCallback'
,
state
);
if
(
state
===
'STATE_OFF'
)
{
this
.
$u
.
vuex
(
'vuex_printConnect'
,
false
);
this
.
$u
.
toast
(
'未连接蓝牙'
)
}
},
discoveryDeviceCallback
:
this
.
onDevice
,
discoveryFinishedCallback
:
function
()
{
console
.
log
(
'搜索到的设备'
,
that
.
devices
)
discoveryFinishedCallback
:
()
=>
{
console
.
log
(
'搜索到的设备'
,
this
.
devices
)
},
readDataCallback
:
function
(
dataByteArr
)
{
readDataCallback
:
(
dataByteArr
)
=>
{
this
.
$u
.
vuex
(
'vuex_printConnect'
,
false
);
},
connExceptionCallback
:
function
(
e
)
{
connExceptionCallback
:
(
e
)
=>
{
console
.
log
(
'connExceptionCallback'
,
e
);
this
.
$u
.
vuex
(
'vuex_printConnect'
,
false
);
}
});
this
.
first
=
true
this
.
searchBle
()
}
catch
(
error
)
{
console
.
log
(
'error'
,
error
)
}
//#endif
},
searchBle
()
{
...
...
@@ -79,26 +86,41 @@ module.exports = {
uni
.
openBluetoothAdapter
({
success
(
res
)
{
this
.
devices
=
[]
console
.
log
(
"打开 蓝牙模块,开始搜索模式..."
,
res
)
uni
.
getBluetoothAdapterState
({
success
:
function
(
res
)
{
console
.
log
(
res
)
if
(
res
.
available
)
{
const
list
=
bluetoothTool
.
getPairedDevices
()
const
item
=
list
.
find
(
v
=>
v
.
name
===
that
.
vuex_printerInfo
.
printerName
)
if
(
item
&&
item
.
device
)
{
bluetoothTool
.
connPairDevice
(
item
.
device
,(
result
)
=>
{
if
(
result
)
{
that
.
$u
.
vuex
(
'vuex_printConnect'
,
true
);
}
console
.
log
(
'连接结果:'
,
result
)
});
}
//搜索蓝牙
//开始搜寻附近的蓝牙外围设备
uni
.
startBluetoothDevicesDiscovery
({
success
(
res
)
{
console
.
log
(
'开始搜寻附近的蓝牙外围设备'
,
res
)
if
(
!
that
.
vuex_printConnect
)
{
bluetoothTool
.
discoveryNewDevice
();
}
}
})
}
else
{
console
.
log
(
'本机蓝牙不可用'
)
}
},
fali
:
()
=>
{
console
.
log
(
'本机蓝牙不可用'
)
}
})
//that.onDevice()
},
fail
:
function
(
msg
)
{
console
.
log
(
msg
)
}
})
},
...
...
@@ -110,7 +132,7 @@ module.exports = {
name
:
newDevice
.
name
,
address
:
newDevice
.
address
})
if
(
newDevice
.
name
===
'CT-428D'
&&
newDevice
.
type
===
1
&&
this
.
first
)
{
if
(
newDevice
.
name
===
this
.
vuex_printerInfo
.
printerName
&&
newDevice
.
type
===
1
&&
this
.
first
)
{
this
.
onConn
(
newDevice
)
bluetoothTool
.
cancelDiscovery
()
this
.
first
=
false
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment