Share my Android Bluetooth open source work HBluetooth

Hello, everyone. I haven't blogged for a long time. This article will share with you an open source project on github, which is about the encapsulation of Android Bluetooth development, including the search, connection and communication of Bluetooth devices, and supports classic Bluetooth and low-power Bluetooth. The language versions include Java and Kotlin.


In fact, a version of the project was prepared as early as 2018. However, due to the busy work at that time, it was shelved for a long time and ignored later. Until I changed my job this year, I had nothing to do. I looked at the GitHub account. I came across a developer who gave me the first issue. I was very surprised and reminded me that I should pick up the project again. Then I optimized it again, updated it to GitHub, and prepared to introduce the use method here in csdn. But before the introduction, I'd like to talk to you about an extra topic, that is:

Why do you want to encapsulate this framework and share it? What is the positioning of this framework?

As we all know, there are many open source frameworks for Android Bluetooth development on Github, which are mature and stable. Like the FastBle framework, many people may feel that it is not necessary to encapsulate such a framework. But what I want to say is, I'm sorry. My original intention of writing this project was not to apply it to the work project for the majority of developers, but to provide a learning entrance for more learners or Android beginners to help them better and faster understand and master the knowledge of Android Bluetooth development. Up to now, this original intention has not changed. Therefore, when designing at that time, the framework modules were divided according to the communication process of Bluetooth development. They all used Android native code and were not over encapsulated. The purpose was to make it clear when you read the source code.
Like many open source frameworks on Github, they are deeply encapsulated or complex, and can be used directly, but they are not suitable for beginners to learn the basic Api of Bluetooth. On the other hand, I think my personal ability is limited, so my positioning is to learn open source works. Although the positioning is not so high, I will always maintain it. In fact, most of my work experience is related to Bluetooth development. In the process of packaging this framework, I have deepened this knowledge and benefited.

OK, now that the digression is over, let's talk about the use of this open source project.

This project has two language versions, one is Java and the other is Kotlin. The Kotlin language version has only been modified recently. It is actually no different from the Java version. The framework process module design and API are actually the same, but there are only grammatical differences, so you can choose according to your needs.


Java language version address: https://github.com/g-HJY/HBluetooth
Kotlin language version address: https://github.com/g-HJY/Kotlin-HBluetooth

Taking the Java language version as an example, tell us how to use this project:
1, Integration mode
The first is integration. Of course, you can download the whole project directly on github or integrate it into your project in the form of dependency. The specific steps are as follows:
1. Add the following configuration to the build.gradle file in the project root directory:

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

2. Add dependency in build.gradle of app module:

dependencies {
     implementation 'com.github.g-HJY:HBluetooth:V1.0.3'
}

After you see that the dependency library has successfully entered, you can start calling.

2, Introduction to use

1. Step 1: instantiate HBluetooth (Global singleton) before use, and call enableBluetooth() method to enable Bluetooth function:

           HBluetooth.getInstance(this).enableBluetooth();

2. After the Bluetooth capability is enabled, you can start to scan Bluetooth devices, where type is the Bluetooth device type (classic Bluetooth or low-power Bluetooth):

           HBluetooth.getInstance(this)
                .scanner()
                .scan(type, new ScanCallBack() {
            @Override
            public void onScanStart() {
                Log.i(TAG, "Start scanning");
            }

            @Override
            public void onScanning() {
                Log.i(TAG, "Scanning");
            }

            @Override
            public void onError(int errorType, String errorMsg) {

            }

            @Override
            public void onScanFinished(List<BluetoothDevice> bluetoothDevices) {
                Log.i(TAG, "End of scan");
                if (bluetoothDevices != null && bluetoothDevices.size() > 0) {
                    list.clear();
                    list.addAll(bluetoothDevices);
                    adapter.notifyDataSetChanged();
                }
            }
        });
        
        
Or, if you want to scan directly after the first step, you can call:
        HBluetooth.getInstance(this)
                .enableBluetooth()
                .scan(type, new ScanCallBack() {
            @Override
            public void onScanStart() {
                Log.i(TAG, "Start scanning");
            }

            @Override
            public void onScanning() {
                Log.i(TAG, "Scanning");
            }

            @Override
            public void onError(int errorType, String errorMsg) {

            }

            @Override
            public void onScanFinished(List<BluetoothDevice> bluetoothDevices) {
                Log.i(TAG, "End of scan");
                if (bluetoothDevices != null && bluetoothDevices.size() > 0) {
                    list.clear();
                    list.addAll(bluetoothDevices);
                    adapter.notifyDataSetChanged();
                }
            }
        });

3. Once the device is scanned, you can find the target device and connect:

        HBluetooth.getInstance(this)
            .connector()
            .connect(device, new ConnectCallBack() {

                @Override
                public void onConnecting() {
                    Log.i(TAG, "Connecting...");
                }

                @Override
                public void onConnected() {
                    Log.i(TAG, "Connection succeeded");
                }

                @Override
                public void onDisConnecting() {
                    Log.i(TAG, "Disconnecting...");
                }

                @Override
                public void onDisConnected() {
                    Log.i(TAG, "Disconnected");
                }

                @Override
                public void onError(int errorType, String errorMsg) {
                    Log.i(TAG, "Error type:" + errorType + " Error reason:" + errorMsg);
                }
            });

4. After the device is successfully connected, you can start to communicate with the device:

           HBluetooth.getInstance(this)
                            .sender()
                            .send(new byte[]{0x01, 0x02}, new SendCallBack() {
                        @Override
                        public void onSending() {
                            Log.i(TAG, "Command sending...");
                        }

                        @Override
                        public void onReceived(DataInputStream dataInputStream, byte[] bleValue) {
                            Log.i(TAG, "onReceived->" + dataInputStream + "---" + bleValue);
                        }
                    });

5. finally, call the following methods to take the initiative to disconnect and release resources:

            HBluetooth.getInstance(this).release();

This is the end of the usage method. The source code within the framework is also very simple, because it has been mentioned at the beginning of this article. The purpose of this open source project is to facilitate everyone to understand and learn this knowledge. If you have seen some theoretical knowledge introduction or scattered API of Android Bluetooth development before, I believe you can understand and run through all the knowledge content very soon.
Well, that's all for this article. Thank you!

Tags: Android bluetooth

Posted on Sun, 17 Oct 2021 21:36:40 -0400 by lupus2k5