Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Battery % wrong (bluetooth device) #551

Closed
dg0ojo opened this issue Jul 19, 2021 · 20 comments
Closed

Battery % wrong (bluetooth device) #551

dg0ojo opened this issue Jul 19, 2021 · 20 comments
Labels
bug Something isn't working
Projects

Comments

@dg0ojo
Copy link

dg0ojo commented Jul 19, 2021

Bildschirmfoto 2021-07-19 um 13 53 59

Stats show 1% and Apple 100% :-)

@exelban exelban changed the title Battery % wrong Battery % wrong (bluetooth device) Jul 19, 2021
@exelban
Copy link
Owner

exelban commented Jul 19, 2021

Hi. Thanks for reporting. I will check this.
It could be fixed at all. Because there are devices that are available only throw macOS cache (/Library/Preferences/com.apple.Bluetooth). So the level will be synchronized only when macOS will update them in this file.

Could you please try to restart the app, and check if the battery value will be more correct?

@dg0ojo
Copy link
Author

dg0ojo commented Jul 19, 2021 via email

@exelban
Copy link
Owner

exelban commented Jul 19, 2021

Emm, sorry but I think I do not understand.
So the app shows the correct battery level?

@dg0ojo
Copy link
Author

dg0ojo commented Jul 19, 2021 via email

@exelban
Copy link
Owner

exelban commented Jul 19, 2021

Could you help to understand why it's happening?

@jack24254029
Copy link
Sponsor Contributor

jack24254029 commented Jul 20, 2021

I have a same issue.
I try to reconnect my Magic Trackpad2, but Stats still show the wrong battery level.
When I restart the Stats, it can show the correct battery level.

This is system UI
image

This is Stats UI before restart Stats
image

@exelban exelban added the bug Something isn't working label Jul 20, 2021
@exelban exelban added this to To do in Roadmap Jul 20, 2021
@exelban exelban moved this from To do to In progress in Roadmap Jul 20, 2021
@exelban exelban moved this from In progress to Waiting for the next release in Roadmap Jul 20, 2021
@exelban exelban closed this as completed Jul 25, 2021
@exelban exelban moved this from Waiting for the next release to Done in Roadmap Jul 25, 2021
@jack24254029
Copy link
Sponsor Contributor

v2.6.2 still can not show correct battery level of Magic Trackpad 2.
I try to restart the Stats, but not work.

I checked the file (/Library/Preferences/com.apple.Bluetooth), I found my Magic Trackpad 2 in Root -> CoreBluetoothCache, and the BatteryPercent was 0.65, but it can not match the battery level on Stats.

image

image
image

@jack24254029
Copy link
Sponsor Contributor

I found this.
https://stackoverflow.com/a/59730707/5134328

Maybe can help you.

@exelban exelban reopened this Jul 28, 2021
@exelban
Copy link
Owner

exelban commented Jul 28, 2021

Hi. It's strange. Ok, I will recheck it.

@exelban exelban moved this from Done to To do in Roadmap Jul 28, 2021
@exelban
Copy link
Owner

exelban commented Jul 28, 2021

Could I ask you to check this build?
Stats.dmg.zip

@exelban exelban moved this from To do to In progress in Roadmap Jul 28, 2021
@exelban exelban moved this from In progress to Waiting for the next release in Roadmap Jul 28, 2021
@jack24254029
Copy link
Sponsor Contributor

jack24254029 commented Jul 29, 2021

No, still show error battery level, but when I reconnect my Magic Trackpad 2, the battery level is updated.

The value diff 1 % between system and Stats, maybe the problem that is carry of the decimal point.

image
image

I can keep tracing this version which you given will be updated battery level or not.

@jack24254029
Copy link
Sponsor Contributor

jack24254029 commented Jul 29, 2021

I make a situation that charing my Magic Trackpad 2, when I stop charing it, the system UI updated the battery level, but Stats not updated it.
image
image

I try below solutions:

  1. Restart Stats -> NOT UPDATED
  2. Reconnect Magic Trackpad2 -> UPDATED

If you need other informations, let me know please.
Thank you.

@jack24254029
Copy link
Sponsor Contributor

I charing my Magic Trackpad 2 to 100%, Stats show 1%.

image
image

This is my device cache
image

@exelban
Copy link
Owner

exelban commented Jul 29, 2021

Hi. Thanks for such a nice debug. According to the second message, the problem is not in the Stats. But in the process of how cache in macOS is updating.
I suppose the value in the cache is updated only on some trigger. In the case of the trackpad is reconnect.

I notice the same bug with my AirPods. It updates the battery level on reconnect. I will try to fetch the battery level from ioreg. Maybe it contains more "live" data.

@exelban
Copy link
Owner

exelban commented Jul 29, 2021

And the 1% when cache contains 1.00 could be related to the float/int conversation. Because the same value in some cases is Int and in some Float. I will fix it.

exelban added a commit that referenced this issue Jul 30, 2021
feat: change UUID to address in the BLE device toolTip
@exelban
Copy link
Owner

exelban commented Jul 30, 2021

Hi. I take a look at the ioreg. It's not possible to fetch a BLE device better level from it. Maybe, it's acceptable for a few devices ( don't have any of them). But it's not a universal solution. I don't find a better way than com.apple.Bluetooth.

@exelban exelban closed this as completed Aug 1, 2021
@exelban exelban moved this from Waiting for the next release to Done in Roadmap Aug 1, 2021
@jack24254029
Copy link
Sponsor Contributor

jack24254029 commented Aug 2, 2021

Hi @exelban
I search the method to get the battery level of Magic Trackpad2.
I wrote the sample code on Playground.
Hope can help you.

import Foundation
import IOKit

var serialPortIterator = io_iterator_t()
var object: io_object_t
let masterPort: mach_port_t = kIOMasterPortDefault
let matchingDict: CFDictionary = IOServiceMatching("AppleDeviceManagementHIDEventService")
let kernResult = IOServiceGetMatchingServices(masterPort, matchingDict, &serialPortIterator)
if KERN_SUCCESS == kernResult {
    object = IOIteratorNext(serialPortIterator)
    while object != 0 {
        if let product: Unmanaged<AnyObject> = IORegistryEntryCreateCFProperty(object, "Product" as CFString, kCFAllocatorDefault, 0) {
            let productValue: String = product.takeRetainedValue() as! String
            print("Product: \(productValue)")
        }
        if let batteryPercent: Unmanaged<AnyObject> = IORegistryEntryCreateCFProperty(object, "BatteryPercent" as CFString, kCFAllocatorDefault, 0) {
            let batteryPercentValue: Int = batteryPercent.takeRetainedValue() as! Int
            print("Battery Level: \(batteryPercentValue)")
        }
        object = IOIteratorNext(serialPortIterator)
    }
    IOObjectRelease(object)
}
IOObjectRelease(serialPortIterator)

Result
image

I don't have a bluetooth earphone, so I can't test it.

P.S You can download the IORegistryExplorer and search AppleDeviceManagementHIDEventService
https://developer.apple.com/download/all/?q=IORegistryExplorer

@exelban
Copy link
Owner

exelban commented Aug 2, 2021

@jack24254029 Thanks. As I said previously, it will work only with some devices. This way works only because Magic Trackpad is adding to the system as an input device (and only Apple devices, because my BLE keyboard does not visible in the AppleDeviceManagementHIDEventService). So it could be fetched via ioreg. Another device will not work with this way of obtaining the battery level. So it could not be used to get BLE battery levels.

I propose you continue this discussion in #557.
Because maybe it will be the only way to obtain Magic mouse/keyboard levels. And I will add this method for specific devices.

@jack24254029
Copy link
Sponsor Contributor

@jack24254029 Thanks. As I said previously, it will work only with some devices. This way works only because Magic Trackpad is adding to the system as an input device (and only Apple devices, because my BLE keyboard does not visible in the AppleDeviceManagementHIDEventService). So it could be fetched via ioreg. Another device will not work with this way of obtaining the battery level. So it could not be used to get BLE battery levels.

I propose you continue this discussion in #557.
Because maybe it will be the only way to obtain Magic mouse/keyboard levels. And I will add this method for specific devices.

OK, thank you.

gmcinalli pushed a commit to gmcinalli/stats that referenced this issue Feb 28, 2022
gmcinalli pushed a commit to gmcinalli/stats that referenced this issue Feb 28, 2022
feat: change UUID to address in the BLE device toolTip
@leobrafl
Copy link

leobrafl commented Jun 20, 2022

Not sure if this is still here, but I'm not being able to see my Bluetooth mouse (Apple Magic Mouse) on the dashboard there
However, when I check iStats, which I was previously using, it shows the battery level of my Bluetooth mouse normally. I dropped some images to show that, even though it's recognizing the mouse, it's not showing the battery level

Screen Shot 2022-06-20 at 18 46 13
Screen Shot 2022-06-20 at 18 46 23

You can see in the first image there's a blank space there, which is where the battery should be displaying

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
No open projects
Roadmap
  
Done
Development

No branches or pull requests

4 participants