Skip to main content
shopping_basket Basket 0
Login

Remote Flying Fish Project Part 15: Bluetooth in Flutter

lokit
0

In this chapter, we will learn how to use a plugin - flutter_blue to connect our Bluetooth devices.

Firstly, install this plugin into Android Studio.

1. Include flutter_blue into your dependency(package) list (pubspec.yaml file):

Include flutter_blue in your dependencies

2. Type "flutter pub get" in the terminal tab of Android Studio in order to update the package available for use in this project.

type in code to update software package

Or click "packages get" on the right upper area.

Or click on "packages get" of the screen

3. Import the following packages.

List of imported packages

4. Change the minimum SDK version from "16" to "19" in "airship/android/app/build.gradle".

Change the minimum SDK version from "16" to "19"

Now you are ready to start.

The flow of the code is the same as in App Inventor. Start with scanning the Bluetooth device, select our device, connect the device and send messages. For simplification, we will automatically select our device by the name. (HMSoft is our device's name.)

Firstly, create and initialize the flutter_Blue. Also with the service UUID, characteristic UUID , the name of the device and a string that for output. (Remark: UUID is in lower case letters)

create and initialize the flutter_Blue

Similar to the the default template in Flutter, here we create a new stateful widget MyApp() after main().

Hint: Hou can type stful for the shortcut

create a new stateful widget MyApp() after main().

Then, override the initState() method. The uses of these two setup classes are to force the program to switch to landscape orientation and perform Bluetooth scanning at the very first place.

override the initState() method - to change to landscape

Next, we need to create startScan() method. It will keep scanning until it found a device that matches the out device name. Therefore, please be make sure you have the right name of your Bluetooth device. You can check it by using AT commands.

Once it is connected to the device, the program will stop scanning.

create startScan() - It will keep scanning until it found a device

The following are the connectToDevice() and disconnectFromDevice() methods.

Code to connectToDevice() and disconnectFromDevice()

And the last step for setup is to locate the right characteristic. The idea is that there are several services in the Bluetooth device. However, only one of them is useful for us to send data. Also the same, there are several characteristics of the service but only one of them is useful for us.

Therefore, we can simply use "forEach()" to match the right service and characteristic by the UUIDs. Save the characteristic in "targetCharacteristic" for future use.

locate the right characteristic

The last step is to send the data. create a global method of writeData() outside any class.

Code to writeData() outside any class

Insert the writeData(messages) inside the class HorizontalTouchControlPainter and the class VerticalTouchControlPainter.

Insert the writeData(messages) inside the class HorizontalTouchControlPainterInsert the writeData(messages) inside the the class VerticalTouchControlPainter.

Build the APK in the Build/Flutter/Build APK.Build the APK in the Build/Flutter/Build APK

You can find the apk file in the directory "airship/android/build/app/outputs/apk/debug/".

apk file shown in the directory

Upload the apk and install it on your mobile device. You can use USB TTL or Arduino to test the results. (Remarks: now you can control both joysticks at the same time.)

==

Here you are, you are now ready to test it with the airship.

============================= iOS User Remark ======================================

You will need to comment out (by adding a #) the line of use_frameworks! in ios/Podfile in order to run/build successfully.

target 'Runner' do
# use_frameworks!

Parts in this series

Downloads

lokit has not written a bio yet…
DesignSpark Electrical Logolinkedin