Introduction:You need conduct command communication with the Bluetooth on a phone for the process of cloud printer network distribution and cloud binding needs to be realized through Bluetooth. The processes of network configuration with Bluetooth has been encapsulated in the SunmiPrinterClient class of the SDK, with which you can complete the printer pairing through few steps in the APP. Before using this function, please make sure that your Bluetooth is on and the APP can access to your location. The following is the instruction of SDK interfaces. For details, please refer to DEMO.The newly-received printer has not configured WiFi, so it is not quite inconvenient during development. Therefore, we provide a network configuration tool which can be installed on Android phones for you to use. Please click to download WIFI configuration tool at DEMO Downoad
The callback class of the cloud printer bluetooth interface
2)Callback interface
In the process of configuring WiFi for printer, it is necessary to send messages to the printer through the BLE protocol. So we define a unified callback that is used to handle the Bluetooth data transmission.
Interface name:
sendDataFail
Callback method:
public void sendDataFail(int code, String msg) {
}
Callback parameter:
Parameter
Type
Specification
code
int
Message error code: 0 – failed to connect to Bluetooth; 1 – Bluetooth Notify failure
msg
String
The error message
3.2 Instruction of network configuration SDK interface(Android)
1)Start scanning the printer
Interface name:
startScan
Interface declaration:
sunmiPrinterClient.startScan();
Callback method:
public void onPrinterFount(PrinterDevice printerDevice) {
}
Callback parameter:
Parameter
Type
Specification
printerDevice
PrinterDevice
Bluetooth printer class. The printer data which is scanned are only the default name and Bluetooth MAC address
2) Stop scanning the printer
Stopping scanning the printer before the current page is disposed or starts configurating the printer if there is no callback for the interface.
Interface name:
stopScan
Interface declaration:
sunmiPrinterClient.stopScan();
3) Get printer SN
Interface name:
getPrinterSn
Interface declaration:
sunmiPrinterClient.getPrinterSn(btAddress);
Input parameter:
Parameter
Type
Specification
btAddress
String
The Bluetooth MAC address of the printer
Callback methods:
Method 1:
public void getSnRequestSuccess() {
}
Specification: this method indicates that the command of getting the SN has been successfully sent and you can wait to get the SN. A timeout mechanism can be added to the method.
Method 2:
public void onSnReceived(String sn) {
}
Callback parameter:
Parameter
Type
Specification
sn
String
打印机的sn
4)Get the list of WiFi which is searched by the printer
Interface name:
getPrinterWifiList
Interface declaration:
sunmiPrinterClient.getPrinterWifiList(btAddress);
Input parameter:
Parameter
Type
Specification
btAddress
String
The Bluetooth MAC address of the printer
Callback methods:
Method 1:
public void routerFound(Router router) {
}
Callback parameters:
Parameter
Type
Specification
router
Router
The WIFI class which is searched by the printer
Router class parameter specification
Parameter
Type
Specification
name
String
WIFI name
hasPwd
boolean
Whether the WIFI has a password
pwd
String
WIFI password
rssi
int
The strength (0-4) of WIFI (the larger the value, the stronger the signal)
essid
byte[]
WIFI essid which is needed in network configuration
Specification: the printer receives the information of the WIFI to be configured and tries to connect to the WIFI. The timeout mechanism is recommended to be added here.
Method 2:
public void wifiConfigSuccess() {
}
Specification: the printer has successfully configured WIFI.
Method 3:
public void onWifiConfigFail() {
}
Specification: The reason why the printer configure WIFI failed is usually caused by a wrong password.
6)Exit network configuration
Specification: this method only has a unified callback for sending Bluetooth message failed. It is recommended to be called in onBackPressed method.
Interface name:
quitConfig
Interface declaration:
sunmiPrinterClient.quitConfig(btAddress);
Input parameter:
Parameter
Type
Specification
btAddress
String
The Bluetooth MAC address of the printer
7)Request deleting WIFI configuration and using 2G network
Specification: this method only has a unified callback for sending Bluetooth message failed. This method can be called without WIFI if the printer is in 2G network.
Interface name:
deleteWifiInfo
Interface declaration:
sunmiPrinterClient.deleteWifiInfo(btAddress);
Input parameter:
Parameter
Type
Specification
btAddress
String
The Bluetooth MAC address of the printer
8)Disconnect Bluetooth from printer
Specification: this method needs to be called when destroying the page
Interface name:
disconnect
Interface declaration:
sunmiPrinterClient.disconnect(btAddress);
Input parameter:
Parameter
Type
Declaration
btAddress
String
The Bluetooth MAC address of the printer
3.3 Import the network configuration SDK (WeChat mini program)
Specification: please make sure the Bluetooth on your phone is on before using this function.
Please put the sdk.js file in the folder where you want to import the interface.
const sdk = require('../blueConnect/sdk.js');
3.4 Instruction of network configuration SDK interface (WeChat mini program)
1)the Bluetooth module and start searching the devices nearby.
Interface name:
openBluetoothAdapter
Interface declaration:
//the parameter of the interface is a callback function status
sdk.openBluetoothAdapter((res) =› {
console.log('the result of turning on Bluetooth', res);
});
Callback parameter:
Parameter
Value
Specification
status
0
Initialized the Bluetooth module successfully.
status
10001
Failed to initialize the Bluetooth module because the user has not turned on Bluetooth.
2)Get the list of the Bluetooth devices nearby
Interface name:
onBluetoothDeviceFound
Interface declaration:
//The parameter of the interface is a callback function devicesList
sdk.onBluetoothDeviceFound((devicesList) =› {
console.log('devicesList', devicesList);
})
Callback parameter:
Parameter
Value
Specification
devicesList
[ ]
Array
3)Stop searching Bluetooth devices nearby
Interface name:
stopScanDevices
Interface declaration:
sdk.startScanDevices();
4)Turn off the Bluetooth module. You can call this method to disconnect all established connection and release system resources.
The device list can be obtained through onBluetoothDeviceFound interface and the deviceId can be found in each device information.
Callback parameter:
parameter
Value
Specification
status
0
Obtain eigenvalues after connect successfully. Parameter as follow
10001
Connected failed
10002
Connected successfully but failed to obtain eigenvalues (Device does not support R&W eigenvalues)
characteristicId
string
Eigenvalues ID
deviceId
string
Device ID
serviceId
string
Service ID
6)Get the cloud printer SN
Interface name:
getSN
Interface declaration:
//The parameter of the interface is a callback function
sdk.getSN((data) =› {
console.log('sn', data);
this.setData({
sn: data
});
});
Callback parameter:
Parameter
Value
Specification
data
string
SN character string
7)Get the list of WiFi which is searched by the cloud printer
Interface name:
getWifiList
Interface declaration:
//The parameter of the interface is a callback function
sdk.getWifiList((data) =› {
this.setData({
wifiList: data
})
console.log('wifiList', data);
});
Callback parameter:
Parameter
Value
Specification
data
[ ]
Array, among which each element stands for a piece of WIFI information including SSID (WIFI name), mode (WIFI mode), RSSI (WIFI strength) and complete (the complete information reply to the command)