bluetoothSocket.connect();首次执行,可以成功连接,之后就一直连接不上。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title></title>
<script src="js/mui.min.js"></script>
<link href="http://ask.dcloud.net.cn/css/mui.min.css" rel="stylesheet" />
</head>
<body>
<header class="mui-bar mui-bar-nav">
<h1 class="mui-title">打印</h1>
</header>
<footer id="footer" class="mui-bar mui-bar-footer">
<h1 class="mui-title" id='search'>
<span class="mui-icon mui-icon-search"></span>
<span style="font-size:16px;">搜索蓝牙设备</span>
</h1>
</footer>
<div class="mui-content mui-content-padded">
<h5 class="mui-content-padded">点击设备开始打印</h5>
<ul id="list" class="mui-table-view mui-table-view-chevron">
</ul>
</div>
<script type="text/javascript" charset="utf-8">
mui.init();
mui.plusReady(function(){
scan();
})
//获取已配对的蓝牙设备列表
function scan() {
var main = plus.android.runtimeMainActivity();
var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
var BAdapter = BluetoothAdapter.getDefaultAdapter(); //获得本机蓝牙适配器
if (!BAdapter.isEnabled()) {
console.log('检测到未打开蓝牙,尝试打开中....');
BAdapter.enable();
}
var lists = BAdapter.getBondedDevices();//获取配对的设备列表
plus.android.importClass(lists);
var iterator = lists.iterator();
plus.android.importClass(iterator);
var ul = document.getElementById('list');
ul.innerHTML = '';
while (iterator.hasNext()) {
var d = iterator.next();
plus.android.importClass(d);
var li = document.createElement('li');
li.setAttribute('id', d.getAddress());
li.className = 'mui-table-view-cell';
var a = document.createElement('a');
a.setAttribute('class', 'mui-navigate-right')
a.innerText = d.getName();
li.appendChild(a);
ul.appendChild(li);
}
}
//mac_address:打印机的mac地址
function print(mac_address) {
if (!mac_address) {
mui.toast('请选择蓝牙打印机');
return;
}
var main = plus.android.runtimeMainActivity();
var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
var UUID = plus.android.importClass("java.util.UUID");
var uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
var BAdapter = BluetoothAdapter.getDefaultAdapter();
var device = BAdapter.getRemoteDevice(mac_address);
plus.android.importClass(device);
var bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
plus.android.importClass(bluetoothSocket);
if (!bluetoothSocket.isConnected()) {
console.log('检测到设备未连接,尝试连接....');
bluetoothSocket.connect();
}
console.log('bluetoothSocket.isConnected()=' bluetoothSocket.isConnected());
//为什么只能是首次连接成功,之后就一直是未连接
if (bluetoothSocket.isConnected()) {
var outputStream = bluetoothSocket.getOutputStream();
plus.android.importClass(outputStream);
var string ='测试测试\r\n\r\n\r\n';
var bytes = plus.android.invoke(string, 'getBytes', 'gbk');
outputStream.write(bytes);
outputStream.flush();
}
}
mui('#list').on('tap', 'li', function() {
print(this.id);
//localStorage.setItem('mac_address',this.id);
})
document.getElementById("search").addEventListener('tap',scan);
</script>
</body>
</html>
blk
00001101-0000-1000-8000-00805F9B34FB 这个uuid是写死的 还是在别处也需要定义一个??
2015-12-01 14:03
huyong1978 (作者)
蓝牙服务 uuid固定值
2015-12-02 11:47
zyimm
还是 不成功 望楼主 告知详细代码
2016-05-12 21:51
a***@qq.com
中文乱码能怎么解决下
2017-10-19 12:59