Android Question Bluetooth device detection through service

wexican

Member
Licensed User
Longtime User
Hi All,

I'm trying to detect the presence of a particular Bluetooth device through a service.
I have the code working through an activity, I can detect all available devices into a list, when I select one particular device then I can remember that devices name & mac address, when I execute the next search for all devices my search will stop early if my remembered device is detected. I basically want to search for Bluetooth devices in the area when the phone is sleeping by using a service that runs every 1 minute.
I can get a service to work on a time basis every minute no problem but my Bluetooth does not appear to be searching when asked to do so through the service.

Anybody got any thoughts or comments?
Thanks in advance.

Tom
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hello,
My stupid and quick reply is based on the issue encountered with some manufacturers. Some put the device in a deep sleeping battery economy mode when the screen is locked.

Erel, one day, adviced me to reenable the PhoneWakeState when the screen is locked. Perhaps could you try : when ScreenOff then acquire a PartialLock.

Hope this can help
 
Upvote 0

wexican

Member
Licensed User
Longtime User
Hello Lemonisdead,

Thanks for your reply, I tried the partial lock as you suggested but I think this is not where my problem is, I think the code I'm trying to run from Service_Start is not working in particular the line "Admin.StartDiscovery" as none of the subs "Admin_DevceFound", "Admin_DiscoveryFinished" or "Admin_StateChanged" appear to be getting called. I know my service is running as I've got a counter that counts every minute as expected & my ToastMessage does display but nothing else happens. I should also mention that I got most of the code from an example from the Serial library to use bluetooth admin functions which I tested as normal first & the examples worked well. If I copy most of the code from the service module into an activity & make the necessary changes to use it in an activity it does work perfectly as I want......it just doesn't work in a service module!

Maybe what I'm trying to do cannot be done within a Service Module. I've attached my service module code below for you to see.

Thanks for the suggestion & help, Tom
Forgot to mention, I'm using B4A version 3.8 & a HTC One M8 with Android 4.4.2
 

Attachments

  • TimeCheck.bas
    2.8 KB · Views: 278
Upvote 0

patrick14384

Member
Licensed User
Longtime User
Tom,

Did you ever find a solution to this? Reading your post, I unknowingly have banged my head on the keyboard trying the same thing. Perfectly good code in the activity, and when I moved it to a code module with the plan to call it from the service and the main activity, but I have failed to get it to work.

Thanks,
Patrick
 
Upvote 0

wexican

Member
Licensed User
Longtime User
Hi Patrick,
Apologies for the delay in getting back to you(work travel) I gave up on trying to use Bluetooth & switched to using WiFi which I got to work. I wanted to basically know when my phone was in a particular location, it didn't have to be accurate & I didn't want to use GPS as it would drain the battery. What I wanted to do was write an app that would basically "clock me in" automatically when I was in the office or out on a customer site. I found the best way to do this was to search for all WiFi available in the area, then select one by name & my app would remember it. Then every minute(using a service) it would look at what WiFi my phone was currently connected to & increment a time record for this location. At the end of the day all times would be written to an excel file. It basically automatically keeps a time sheet of my work locations for the day & at the end of the week would email to my work email the excel sheet.
It currently is under test and is working well, some small bugs but nothing major. if you need more information let me know.

Hope this helps

Best Regards

Tom
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…