猛先生
猛先生
  • 发布:2016-05-14 04:12
  • 更新:2024-07-30 14:56
  • 阅读:79404

安卓Native.js蓝牙连接票据打印机完整代码已测试修改

分类:Native.js

测试机:佳博PT-280便携打印机
手机:华为低端
功能:扫描周围蓝牙设备加入列表,点击未配对设备,自动配对设备,点击已配对设备,进行打印测试
先上html

<!DOCTYPE html>  
<html>  

    <head>  
        <meta charset="UTF-8">  

        <title></title>  

        <script src="js/pr.js"></script>  
    </head>  

    <body>  

        <p><input id="bt1" type="button" value="搜索设备" onclick="searchDevices('a')"></p>  

               </button>  

        </div>  

        <div>  
            未配对蓝牙设备  
            <ul id="list1">  

            </ul>  
        </div>  

        <div>  
            已配对蓝牙设备  

            <ul id="list2">  

            </ul>  
        </div>  

    </body>  

</html>

下面是js文件

//address=""搜索蓝牙//address=设备mac地址,自动配对给出mac地址的设备  
function searchDevices(address) {  
    //注册类  
    var main = plus.android.runtimeMainActivity();  
    var IntentFilter = plus.android.importClass('android.content.IntentFilter');  
    var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");  
    var BluetoothDevice = plus.android.importClass("android.bluetooth.BluetoothDevice");  
    var BAdapter = BluetoothAdapter.getDefaultAdapter();  
    console.log("开始搜索设备");  
    var filter = new IntentFilter();  
    var bdevice = new BluetoothDevice();  
    var on = null;  
    var un = null;  
    var vlist1 = document.getElementById('list1'); //注册容器用来显示未配对设备  
    vlist1.innerHTML = ''; //清空容器  
    var vlist2 = document.getElementById('list2'); //注册容器用来显示未配对设备  
    vlist2.innerHTML = ''; //清空容器  
    var button1 = document.getElementById('bt1');  
    button1.disabled=true;  
    button1.value='正在搜索请稍候';  
    BAdapter.startDiscovery(); //开启搜索  
    var receiver;  
    receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {  
        onReceive: function(context, intent) { //实现onReceiver回调函数  
            plus.android.importClass(intent); //通过intent实例引入intent类,方便以后的‘.’操作  
            console.log(intent.getAction()); //获取action  
            if(intent.getAction() == "android.bluetooth.adapter.action.DISCOVERY_FINISHED"){  
                main.unregisterReceiver(receiver);//取消监听  
                button1.disabled=false;  
                button1.value='搜索设备';  
                console.log("搜索结束")  
            }else{  
            BleDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
            //判断是否配对  
            if (BleDevice.getBondState() == bdevice.BOND_NONE) {  
                console.log("未配对蓝牙设备:" + BleDevice.getName() + '    ' + BleDevice.getAddress());  
                //参数如果跟取得的mac地址一样就配对  
                if (address == BleDevice.getAddress()) {  
                    if (BleDevice.createBond()) { //配对命令.createBond()  
                        console.log("配对成功");  
                        var li2 = document.createElement('li'); //注册  
                        li2.setAttribute('id', BleDevice.getAddress()); //打印机mac地址  
                        li2.setAttribute('onclick', 'print(id)'); //注册click点击列表进行打印  
                        li2.innerText = BleDevice.getName();  
                        vlist2.appendChild(li2);  
                    }  

                } else {  
                    if(BleDevice.getName() != on ){ //判断防止重复添加  
                    var li1 = document.createElement('li'); //注册  
                    li1.setAttribute('id', BleDevice.getAddress()); //打印机mac地址  
                    li1.setAttribute('onclick', 'searchDevices(id)'); //注册click点击列表进行配对  
                    on = BleDevice.getName();  
                    li1.innerText = on;  
                    vlist1.appendChild(li1);  

                    }  

                }  
            } else {  
                if(BleDevice.getName() != un ){ //判断防止重复添加  
                console.log("已配对蓝牙设备:" + BleDevice.getName() + '    ' + BleDevice.getAddress());  
                var li2 = document.createElement('li'); //注册  
                li2.setAttribute('id', BleDevice.getAddress()); //打印机mac地址  
                li2.setAttribute('onclick', 'print(id)'); //注册click点击列表进行打印  
                un = BleDevice.getName();                 
                li2.innerText = un;  
                vlist2.appendChild(li2);}  
            }}  

        }  
    });  

    filter.addAction(bdevice.ACTION_FOUND);  
    filter.addAction(BAdapter.ACTION_DISCOVERY_STARTED);  
    filter.addAction(BAdapter.ACTION_DISCOVERY_FINISHED);  
    filter.addAction(BAdapter.ACTION_STATE_CHANGED);  

    main.registerReceiver(receiver, filter); //注册监听  
}  

var device = null,  
    BAdapter = null,  
    BluetoothAdapter = null,  
    uuid = null,  
    main = null,  
    bluetoothSocket = null;  

function print(mac_address) {  
    if (!mac_address) {  
        mui.toast('请选择蓝牙打印机');  
        return;  
    }  

    main = plus.android.runtimeMainActivity();  
    BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");  
    UUID = plus.android.importClass("java.util.UUID");  
    uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");  
    BAdapter = BluetoothAdapter.getDefaultAdapter();  
    device = BAdapter.getRemoteDevice(mac_address);  
    plus.android.importClass(device);  
    bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);  
    plus.android.importClass(bluetoothSocket);  

    if (!bluetoothSocket.isConnected()) {  
        console.log('检测到设备未连接,尝试连接....');  
        bluetoothSocket.connect();  
    }  

    console.log('设备已连接');  

    if (bluetoothSocket.isConnected()) {  
        var outputStream = bluetoothSocket.getOutputStream();  
        plus.android.importClass(outputStream);  
        var string = "打印测试\r\n";  
        var bytes = plus.android.invoke(string, 'getBytes', 'gbk');  
        outputStream.write(bytes);  
        outputStream.flush();  
        device = null //这里关键  
        bluetoothSocket.close(); //必须关闭蓝牙连接否则意外断开的话打印错误  

    }  

}
24 关注 分享
4***@qq.com everyday666 PercyCJ 1***@qq.com EtoYang g***@qq.com FYC k***@163.com 信不过你开车 首席劝退师 s***@qq.com 9***@qq.com 5***@qq.com 7***@qq.com 康林 kim猴 7***@qq.com hws007 1***@qq.com 1***@qq.com 1***@qq.com 1***@qq.com 1***@qq.com r***@163.com

要回复文章请先登录注册

2***@qq.com

2***@qq.com

问个问题 如何打印图片
2017-05-09 15:17
米虫

米虫

请问下 为什么在搜索的过程中 点击搜到的设备进行配对 会执行开始搜索呢?只有搜索完 配对才起作用。这些代码哪里出现问题了?怎么排除呢?
2017-05-01 16:19
z***@hotmail.com

z***@hotmail.com

打印的时候,需要设置打印的字体,需要怎么控制
2017-05-01 16:07
z***@sohu.com

z***@sohu.com

这个是普通蓝牙的吧,因为BLE肯定要连接GATT进行通讯啊
2017-03-22 11:18
2***@qq.com

2***@qq.com

打印机如果返回数据怎么接收?
2017-02-27 11:42
fredzhu

fredzhu

回复 HTML5寻路者 :
看看这个有用不?
真机测试完全没问题,但是问题出现在了打包之后....在搜索开启蓝牙和搜索蓝牙设备的时候都不行。一想是权限的问题,吧蓝牙权限加上之后就可以了。。。

这三个权限是

android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
android.permission.BLUETOOTH_PRIVILEGED
2017-02-10 21:38
8***@qq.com

8***@qq.com

请问一下获取设备mac地址的getAddress()是怎么写的啊
2017-02-09 11:01
ganjue123

ganjue123

Uncaught java.io.IOException: Bluetooth is off;at android.bluetooth.BluetoothSocket.connect
2017-01-11 11:52
6***@qq.com

6***@qq.com

??
2016-12-01 11:18
6***@qq.com

6***@qq.com

这个对打印机有什么要求吗??
2016-12-01 11:17