Bluetooth Low Energy (BLE) In Flutter - BLE SCANNER

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys so welcome to protocol as point so in this tutorial we are going to learn how to implement BL scanner into our flutter application so in this tutorial we are going to learn how to detect a nearby Bluetooth devices and show the list of Bluetooth devices into our flutter application so as you can see over here so this is my mobile device that has been connected to a USB and you can see my Mobile screen over here and these are the Bluetooth devices nearby my mobile device okay so if I press on this scan button you can see I get the list of dat devices that is Bluetooth devices near my mobile okay so here I just showing the MAC address of the Bluetooth device and the RSSI that is the distance of that device particular device okay so let's understand how to implement BL scanner in our flutter application so let's get started okay so here I have created a new flutter project in my Android Studio IDE and this is the default code that is the counter code given by flut while creating new flutter project so I will just remove this and create my own State stateful widget and name it as my home page so now this stateful widget simply has a empty placeholder you can see so first of all we must add some dependencies and we need to add BL scanner uh permissions to be used in my mobile device so let's do that so to add dependencies just go to PO specification. yamile and under dependency section over here we need to add three dependencies one is flutter okay so this has been used to scan Bluetooth devices and we also need get package and uh to ask permission from the user about the using the Bluetooth devices we need to add permission Handler so let's add that okay so these are the three dependencies that is required for this project okay so after adding that just hit this pubget button to to download all these dependencies in your external library of your product project it's already been done so now let's go to my homepage main. page okay so now let's uh start the coding part so here in lip directory let me create a new file that file and I will just name it as blle controller okay so this file will be responsible for handling all the requests uh that are coming uh like scanning the devices and showing it on the screen okay so here first of all let's create a class and I will just name it as BL controller extend uh getex controller so get x controller okay so this will import our get Library okay and now let's make use of flutter blue to detect our Bluetooth devices so we have already imported the library that is uh flutter blue okay so here flutter blue okay so you can see it got imported automatically so I will just name it as BL scan B okay and just create an instance of it so flutter blue. instance okay so this is an instance so by making use of this instance we can just scan and show our devices that is nearby our device okay so here let me create a function future function and let name it name it as scan blue devices okay so this function should be a sync nature so let's create this function as a sync okay so now I need to check if a user has given a permission to access Bluetooth device of of his device so we we are going to make use of permission Library so let's do that so if await permission okay so by using this you can see the permission Handler class got imported permission dobl scan do request dot is granted okay and we need another if condition await permission. BL connect okay so these are the two permissions that we need to ask from the user to Grant it to use BL Bluetooth device of his device okay so what this will do is it will just prompt the user to accept the permission uh that is Android permission okay so now let's use this instance and start the Bluetooth scanning uh of the nearby devices so B dot start scan there is a function start scan okay so here we need need to make use of a property called as uh timer so time out yeah so duration for how much time the device should scan for the nearby devices so I will just keep it as 10 seconds okay and after 10 seconds uh BL will stop the it scanning so ble do stop scan okay so now once the blle has scan it scanned all the devices will be listed in this instance so now we need to make use of it and show it in our app in our main. page so what I will do is I will just create a streamer that will give a list of BL devices so here I will just create a stream okay so this stream will be of kind that is data type list and it will just return scan devices so scan result so it will just return return scan result of list okay and this will be a get method scan result okay so ble do scan result okay so our streaming has been created okay so we can make use of it to show our device in our list will okay so now our blle controller has been created so now let's go to main do. page and uh uh create our UI and a button to scan the devices okay so now let's uh add our controller in our UI so let me remove this and instead of this let me make use of scaffold widget so we have a app bar so let's add an AB bar okay so in body tag we will attach our uh gex Builder with the ble controller that we have created so gex Builder and we are going to connect our ble controller ble controller over here like this okay so here we okay so BL controller okay so it is giving me an error let me remove this and be get Builder B controller so now here we must make use of this required property that is Builder what we need to build in this vet we need to Define okay so here in Intel we must pass our controller class so ble controller class so here written uh we can just return a center vigit so Center visit we can just return return over here okay so Center wiget will have a child so in Center wiget I will just add a child as column viget okay so that I can integrate more than one widget in this column so main access alignment and I will just keep a main access alignment s Center so here we must Define children's so let me create a button so elevated button so elevated button so when it has been pressed what should happen will be defined over here and then here I will just create a text widget and I will just name it as scan okay so when this button is been pressed I need to call this class to scan my uh nearby devices so this function I need to call okay so let's do it afterwards so let's build our UI and down of it I need to show all the list of Bluetooth devices as I have shown on over here okay so above it I need to do so here over here okay so size box I will just give and with height of 10 so that there is a gap between button and the list of Bluetooth device nearby so now so here as you saw I have a Stream So I must make use of stream Builder to show all the list of this devices so let's do that so here uh stream Builder Okay so these are the properties that we must make use of so here this stream Builder will uh get the list of Bluetooth devices so here I will just Define the data type so scan result okay so now here in stream property we must pass the data that we received uh over here that is scan result okay so to access this uh data type from this BL scanner we have already attached or linked our controller so we can make use of this controller to access this uh data type okay that that is get property so here stream Builder uh we can just make use of controller controller. scan result okay and here in Builder we get the list of datas of this scanner okay so here uh context so first parameter is context and second parameter is data so I will just name this data as snapshot okay now here if snapshot do has data if it has data then we need to show the data that we have else we are we can we are going to just show a message that is that there is no nearby device so here return center Viet uh child property no device found and now here I need to show all the devices if there are some devices Bluetooth devices nearby so let's do that so here written uh list Builder do Builder okay so here we are going to just show all the list that is been available in this snapshot okay so item Builder we have making use of so context and second second parameter is index final data is equal to snapshot. data null safety and then we are going to access each index data so first index we get and we are going to just show that data in our card so written card okay so let's give a elevation for this card child list t Okay so as you saw uh we are going to show our Mac ID of the device the name so here I'm uh all the devices which are nearby don't have an Bluetooth device name so it is not getting printed else it will get printed and the RSSI so here we need to make use of three properties that is title subtitle and trailing okay so let's use that so here text widget data do device name and subtitle text data. device. id. ID okay so this will be our Mac ID of the Bluetooth device that is been scanned and then trailing we are going to show a RSSI so text widget data. RSSI do2 string okay so let's end this card and you want this uh list will Builder okay so we have already completed uh Al completed the UI part that is scanning the Bluetooth devices okay so now only thing is we need to call this function uh by when the button is been pressed okay so that we can do by making use of the same controller so here when this button has been pressed controller dot scan devices like this we can do okay so let me run this in my device okay so I just forgot one main thing that is Bluetooth device access uh permission to be added in our android model of our flutter project so just open this manifest and in Android manifest we need to add the Bluetooth access permission okay so let me add that so these are the five permissions that you need to add to make use of Bluetooth devices okay so this source code of this video tutorial will be on my website protoc codesp point.com so you can just get all the source code from there and Implement in your flutter application so this is the thing that you need to add so I have just forgot it to introduce you with this at the beginning of this tutorial okay so let me run this project again okay so the application got installed on my device as you can see so when I press on this uh start scanning so you can see it is asking me a permission to access your location so I will just accept it and with thatth it also accepted the Bluetooth permission to use it and you can see I got the list of Bluetooth devices nearby my devices so this is how you can Implement BL scanner into your flutter application okay so that's all for this video tutorial so if you want a tutorial on how to connect to a particular Bluetooth device you can just comment it out below and I will just create a video tutorial on how to connect to a specific uh Bluetooth device from your F application okay so thanks for watching
Info
Channel: Proto Coders Point
Views: 18,802
Rating: undefined out of 5
Keywords: flutter, flutter tutorial, android, android tutorial, Bluetooth Low Energy, BLE, BLE Scanner, Bluetooth Connectivity, Bluetooth Technology, BLE Integration, IoT, BLE SCANNER, ble scanner, bluetooth scanner, bluetooth scanner inin flutter
Id: 0edSNTjAaRg
Channel Id: undefined
Length: 14min 42sec (882 seconds)
Published: Mon Oct 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.