react-native-ble-advertiser
react-native-ble-advertiser copied to clipboard
Begging you to provide a minimal working example source code
Hello, sorry for mass mentioning (tags), I really want to have an example code of this BLE advertiser package. I just could not setup it myself, I tried millions of times, but there are errors and errors and errors. I read many solved issues/conversations here, but still could not get it working.
@vitorpamplona, @ayoubphy, @JuliaGmP, @bentofortes, @pavankumarupai37, @hern12, @fhllnd, @Philip-Smyth,
Please, please provide a minimal working example source code of React Native project with this BLE package.
It can be super minimal.
I will be happy with anything, but, preferably: 1] to be Expo project. 2] to have "Broadcast" button which starts broadcasting simple string (up to 12 characters) like: "ThisIsLeo". 3] to have "Scan" button which starts scanning and shows list of scanned items: "ThisIsOlivia", "ThisIsBob", "ThisIsTom". 4] to be project with the latest React Native version. 5] to be omni-potent: I mean to work with any direction: Android/iPhone to Android/iPhone. Yeah, I understand the Bluetooth version should 5.0 or higher.
Initially, I thought Bluetooth broadcasting/scanning would be an easy thing, but now it's my big dream to somehow get it working and do some experiments with it.
Hi Leo,
I'll try and cover the points as best I can since I haven't touched the project I used this in in a while, so I might misremember some points.
First point I'm not sure if it's possible to use this at all in expo, I originally enjoyed using expo as it was quick to test out packages, however, as you find quickly majority of packages (and production apps) use bare workflow so it's good to get to know it (assuming you don't/haven't used it)
2 & 3) these buttons are in there :)
- there are some interesting rules when it comes to broadcasting with BLE, since it's not the same as Bluetooth the packets have to be smaller than a certain size and I don't believe this is a possibility when it comes to a custom text. If you find a way around please let me know.
My workaround was to convert the userId (or an identifying value name) into a uuid string with string-to-uuid then you can get the user details by matching the uuid that is scanned in the db
-
latest react version I can see if it can be updated but it's on 0.70.1 which is stable and should work with 0.7x.x
-
this is implemented
I'll have a look tomorrow at getting the example to work, you do have to run npm pack
in the root folder and change example/package.json
the ble-advertiser...0.0.16
to 0.0.17
The other thing that may need to be updated is the gradle version but this is more of a maintenance than necessity as it should still work in its own project
Oh, thanks for the quick reply.
I'll have a look tomorrow at getting the example to work
Thanks so much, I will be super happy. (No need to hurry, I can wait many days/weeks).
One more question:
Can the react-native-ble-plx
package do (theoretically) all the things react-native-ble-advertiser
can?
And here is one interesting video: This person somehow probably manages to use Bluetooth (react-native-ble-plx) with Expo project.
Title: "React-Native with Expo and Bluetooth Low Energy for Beginners" Channel: "Dan's React Native Lab" Uploaded: 2023-02-13 https://www.youtube.com/watch?v=UuHLPsjp6fM
But, anyway, as I said: I prefer Expo project but I also will be happy with bare workflow project.
I don't believe this is a possibility when it comes to a custom text
At least on Android it seems quite possible. For example, I tested this app and it works very well. It has broadcast and scan features. We can type custom text (up to probably 16 characters) and broadcast it and another phone scans/reads it quickly and pretty well.
https://play.google.com/store/apps/details?id=net.alea.beaconsimulator
https://github.com/vhiribarren/beacon-simulator-android
Well, yeah, it's not a React Native project, but I guess we can do the same with RN.
At the time I wrote this library (2020), I could not get Expo to work with Bluetooth at all. Even after ejecting it, Expo would interfere and make BLE debugging impossible to make sense of. The same was true for any emulator of a real phone.
Maybe things have changed, but Bluetooth hardware is so messy that you might want to avoid any source of uncertainty in your dev environment.
@leodevbro I've made this fork for the time being git clone https://github.com/fhllnd/react-native-ble-advertiser.git
(working on android) I'll PR it tomorrow when I can test it on iOS apologies if this is what you need.
I stand corrected, it is possible to transmit data like a name however I'm not sure if this package was written with that in mind, its more just a broadcast and find.
You're welcome to change it up and add that feature in but I don't think the strings will be able to be very long anyway. Perhaps ChatGPT can help and I might have a look in the far future but personally I don't have the need as the string I need is longer than 16 characters anyway.
It should work between iOS to Android, and iOS to iOS, and Android to Android this was a feature I added for my use a while back. Most likely it will also work on Bluetooth 4 and 5, (I used a 5s for testing and it was fine)
Check out the fork and don't forget to run npm pack && cd example && yarn
first. Let me know if there's any issues, I re-cloned it and ran from scratch all fine but you never know with different machines and java...
@fhllnd, thanks so much. I'll try it.
it is possible to transmit data like a name however I'm not sure if this package was written with that in mind, its more just a broadcast and find.
Yeah, all I want is to just broadcast and find, nothing more. I was just thinking to create an app which would assign a unique id to each phone (saved in centralized backend database), and then the app would broadcast this id (from all phones) and these phones would discover each other (without pairing/connecting), so the owners of these phones will know that specific people are near them. So, I was thinking that some kind of id-like string would be a normal thing to identify phones. I don't need more than 16 characters. If not a string, then I don't know, some kind of binary number is considered to be an id?
Yep that's exactly what I've used this for before.
Say you have a users table with userId and broadcastId you can generate this using the uuid-by-string package and using the userId as the argument to make it unique and not random
const getUuid = require('uuid-by-string')
broadcastId = getUuid(userId.toString())
And one step further to add a filter suffix broadcastId = getUuid(userId.toString()).slice(0, -6) + '0abcde'
this will allow you to use a query similar to select * from users where broadcastId = broadcastId
to get the user details of the scanned UUID.
Of course the suffix isn't needed but it is helpful to only search for UUIDs you're expecting
One thing to note is this method relies on you knowing the userId, therefore the broadcastId field has to be populated after the user is created (two writes to db). You could do this with the email instead and then pass the broadcastId to the createUser method along with all the other user details which would remove this step and likely be just as safe with uniqueness I just didn't think of that at the time haha.
Hopefully that makes sense :D
@leodevbro you can now use the PR #55 in order to test, working on ios and android (you may have to change org in xcode) enjoy :D