Example code of Android mobile phone connected to Jiabo printer through Bluetooth

The printer used is Jiabo printer, which supports Bluetooth, WiFi and USB. I use Bluetooth to connect.

I found an app source code officially developed by Jiabo for Android on the Internet, but I didn't understand all kinds of jumps, so I went to ask Du Niang and found a good article

Android supports Bluetooth development only from SDK version 2.0, and the simulator does not support it. Testing requires at least two mobile phones, so it restricts the development of many technicians.

1. First, to operate Bluetooth, add permissions in androidmanifest.xml

2. Turn on Bluetooth

Obtain the Bluetooth adapter (Android. Bluetooth. Bluetooth adapter), check whether the device supports Bluetooth, and if so, turn on Bluetooth.

3. Obtain the paired bluetooth device (Android. Bluetooth. Bluetooth device)

A Bluetooth device needs to be paired for the first time. Once the pairing is successful, the information of the device will be saved. There is no need to pair again when connecting later, so the paired device may not be able to connect.

4. Search the surrounding Bluetooth devices

After the adapter searches the Bluetooth device, the results will be sent out in the form of broadcast. Therefore, it is necessary to customize a class that inherits the broadcast to obtain and process the search results of the Bluetooth device in the onReceive method.

5. Pairing and status monitoring of Bluetooth devices

6. Bluetooth device connection

1. Bluetooth adapter as the name suggests, the Bluetooth adapter must be operated continuously until we establish a Bluetooth socket connection

There are many methods in Bluetooth adapter, including the following:

Canceldiscovery () literally means to cancel discovery, that is, when we are searching for a device, calling this method will not continue the search

Disable() turns off Bluetooth

Enable() turns on Bluetooth. This method will not pop up a prompt when turning on Bluetooth. More often, we need to ask the user whether to turn it on. The following two lines of code also turns on Bluetooth, but will prompt the user:

Intemtenabler=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enabler,reCode);// Same as startactivity (enabler);

Getaddress() get local Bluetooth address

Getdefaultadapter () gets the default Bluetooth adapter. In fact, this is the only way to get the Bluetooth adapter

Getname() get local Bluetooth name

Getremotedevice (string address) obtains the remote Bluetooth device according to the Bluetooth address

Getstate() gets the current state of the local Bluetooth adapter (it seems that it may be more necessary during debugging)

Isdiscovering() determines whether the device is currently being searched, and returns true

Isenabled() determines whether Bluetooth is turned on. If it is turned on, it returns true; otherwise, it returns false

Listenusingrfcommwithservicerecord (string name, UUID, UUID) creates and returns a Bluetooth ServerSocket according to the name and UUID. This is the first step to create a Bluetooth socket server

Startdiscovery() starts the search, which is the first step in the search

2. Bluetooth device is known by its name. This class describes a Bluetooth device

Createrfcommsockettoservicerecord (uuiduuid) creates and returns a Bluetooth socket based on the UUID

This method is also the purpose of obtaining Bluetooth device - creating Bluetooth socket

Other methods of this class, such as getaddress(), getname(), are the same as Bluetooth adapter

3. If Bluetooth is removed from the Bluetooth server socket, you must be familiar with it. Since it is a socket, the methods should be the same,

This class has only three methods

The difference between the two overloaded accept(), accept (inttimeout) is that the latter method specifies the expiration time. It should be noted that when these two methods are executed, the thread will be blocked until the client's request is received (or expired). It should be run in a new thread!

It should also be noted that both methods return a Bluetooth socket, and the final connection is also the connection between the server and the client

Close() needless to say, translate it - close!

4. Bluetoothsocket, opposite to bluetoothserversocket, is a client

There are five methods in total. No accident, they will be used

Close()

Connect

Getinptustream() gets the input stream

Getoutputstream() gets the output stream

Getremotedevice() gets the remote device. This refers to the remote Bluetooth device that gets the connection specified by Bluetooth socket

According to this article, only the last Bluetooth connection part is changed (because Jiabo printer has its own SDK, and the SDK has its own connection method). Import the two packages gprintersdkv22.jar and xutils-2.6.14.jar in the downloaded demo. The connection method is as follows:

Don't forget that mgpservice is one of Jiabo SDK's own services, which should be obtained first and bound with activity;

Also configure the service in the manifest file

After that, you're done. You can connect to the printer, but you still can't print. You have to configure some printing. Because of my project, the connection and printing are not in the same interface, so continue to study the Jiabo source code and find that if the previous link has been successful, On the other interface, you can print directly with only one configuration. The printing code is as follows:

So far, it's done. I feel great after printing something

The above is the example code of Android mobile phone connected to Jiabo printer through Bluetooth introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>