Turn Bluetooth on and off
var main = (); var Context = (""); var BManager = (Context.BLUETOOTH_SERVICE); (BManager);//Introduce related method functionsvar BAdapter = (); (BAdapter);//Introduce related method functions, so that the isEnabled function will be supported after thatif(!()) { (); }
Monitor Bluetooth switch status
var main = (); var BluetoothAdapter = (""); var BAdapter = new (); var resultDiv = ('output'); var receiver=('', { onReceive: function(context, intent) { //Implement the onReceiver callback function (intent); (()); += '\nAction :' + (); (receiver); } }); var IntentFilter = (''); var filter = new IntentFilter(); (BAdapter.ACTION_STATE_CHANGED); // Monitor Bluetooth switch(receiver, filter); //Register monitoring if (!()) { (); //Start Bluetooth}else{ (); }
Get a list of Bluetooth devices
unction bluetooth_list(){ var main = (); var Context = (""); var lists = (); (lists); var resultDiv = ('bluetooth_list'); var iterator = (); (iterator); while (()) { var d = (); (d); (()); } }
Bluetooth Connected Ticket Printer
Testing machine: Jiabo PT-280 portable printer
Mobile phone: Huawei's low-end
Function: Scan the list of surrounding Bluetooth devices to join, click on unpaired devices, automatically pair the devices, click on paired devices, and print tests
html page code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="js/"></script> </head> <body> <div> <p><input type="button" value="Search for Devices" onclick="searchDevices('a')"></p> </div> <div> Unpaired Bluetooth devices <ul > </ul> </div> <div> Paired Bluetooth devices <ul > </ul> </div> </body> </html>
Js file
//address=""Search for Bluetooth//address=device mac address, automatically pair the device that gives the mac addressfunction searchDevices(address) { //Register var main = (); var IntentFilter = (""); var BluetoothAdapter = (""); var BluetoothDevice = (""); var BAdapter = (); ("Start search for devices"); var filter = new IntentFilter(); var bdevice = new BluetoothDevice(); var on = null; var un = null; var vlist1 = ("list1"); //Register container is used to display unpaired devices = ""; //Empty the container var vlist2 = ("list2"); //Register container is used to display unpaired devices = ""; //Empty the container var button1 = ("bt1"); =true; ="Searching, please wait"; (); //Start search var receiver; receiver = ("", { onReceive: function(context, intent) { //Implement the onReceiver callback function (intent); //Introduce the intent class through the intent instance to facilitate the future '.' operation (()); //Get action if(() == ".DISCOVERY_FINISHED"){ (receiver);//Cancel the monitoring =false; ="Search for Devices"; ("Search End"); }else{ BleDevice = (BluetoothDevice.EXTRA_DEVICE); //Judge whether it is matched if (() == bdevice.BOND_NONE) { ("Unpaired Bluetooth Devices:" + () + " " + ()); //If the parameters are the same as the obtained mac address, it will be matched if (address == ()) { if (()) { //Pairing command.createBond() ("Pairing Successfully"); var li2 = ("li"); //register ("id", ()); //Printer mac address ("onclick", "print(id)"); //Register click to click the list to print = (); (li2); } } else { if(() != on ){ //Judge to prevent repeated addition var li1 = ("li"); //register ("id", ()); //Printer mac address ("onclick", "searchDevices(id)"); //Register click the list to pair on = (); = on; (li1); } } } else { if(() != un ){ //Judge to prevent repeated addition ("Paired Bluetooth Devices:" + () + " " + ()); var li2 = ("li"); //register ("id", ()); //Printer mac address ("onclick", "print(id)"); //Register click to click the list to print un = (); = un; (li2);} }} } }); (bdevice.ACTION_FOUND); (BAdapter.ACTION_DISCOVERY_STARTED); (BAdapter.ACTION_DISCOVERY_FINISHED); (BAdapter.ACTION_STATE_CHANGED); (receiver, filter); //Register monitoring} var device = null, BAdapter = null, BluetoothAdapter = null, uuid = null, main = null, bluetoothSocket = null; function print(mac_address) { if (!mac_address) { ("Please select Bluetooth printer"); return; } main = (); BluetoothAdapter = (""); UUID = (""); uuid = ("00001101-0000-1000-8000-00805F9B34FB"); BAdapter = (); device = (mac_address); (device); bluetoothSocket = (uuid); (bluetoothSocket); if (!()) { ("The device is not connected, try to connect..."); (); } ("The device is connected"); if (()) { var outputStream = (); (outputStream); var string = "Print Test\r\n"; var bytes = (string, "getBytes", "gbk"); (bytes); (); device = null //The key here (); //The Bluetooth connection must be turned off, otherwise the print error will be performed if it is accidentally disconnected. } }
The above is the operation example code for Android Bluetooth devices. If you want to know other instance codes or native Android Bluetooth operation methods, please check the relevant links below.