question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android app crashes because don't found characteristic

See original GitHub issue
  • bleak version: 0.14.3
  • Python version: 3.9.12
  • Operating System: Android 6.0.1, API 23

I am trying to get this android app to work which is supposed to connect to a Mi Smart Scale 2 scale via low energy Bluetooth. However, even if the app crashes from the logs I see that it connects to the phone.

The app once connected to the phone should read the feature with the start_notify operation but this does not happen.

This is the error that I get, from logcat in android studio:

022-06-13 17:34:33.657 12975-12986/org.test.demo12 D/BluetoothGatt: onClientConnParamsChanged() - Device=70:87:9E:0F:F8:7D interval=39 status=0
2022-06-13 17:34:33.847 12975-13001/org.test.demo12 I/python: Connected: True
2022-06-13 17:34:33.847 12975-13001/org.test.demo12 D/BluetoothGatt: cancelOpen() - device: 70:87:9E:0F:F8:7D
2022-06-13 17:34:33.847 1944-1978/? D/BtGatt.GattService: clientDisconnect() - address=70:87:9E:0F:F8:7D, connId=10
2022-06-13 17:34:33.847 1944-2370/? E/bt_btm: btm_ble_get_search_if search_if=4
2022-06-13 17:34:33.847 1944-2370/? W/bt_btif: bta_gattc_conn_cback() - cif=4 connected=0 conn_id=4 reason=0x0016
2022-06-13 17:34:33.847 1944-2370/? W/bt_btif: bta_gattc_conn_cback() - cif=5 connected=0 conn_id=5 reason=0x0016
2022-06-13 17:34:33.847 1944-2370/? W/bt_btif: bta_gattc_conn_cback() - cif=6 connected=0 conn_id=6 reason=0x0016
2022-06-13 17:34:33.847 1944-2370/? W/bt_btif: bta_gattc_conn_cback() - cif=7 connected=0 conn_id=7 reason=0x0016
2022-06-13 17:34:33.847 1944-2370/? W/bt_btif: bta_gattc_conn_cback() - cif=8 connected=0 conn_id=8 reason=0x0016
2022-06-13 17:34:33.847 1944-2370/? W/bt_btif: bta_gattc_conn_cback() - cif=9 connected=0 conn_id=9 reason=0x0016
2022-06-13 17:34:33.847 1944-2370/? W/bt_btif: bta_gattc_conn_cback() - cif=10 connected=0 conn_id=10 reason=0x0016
2022-06-13 17:34:33.857 1944-2267/? D/bt_vendor: op for 7
2022-06-13 17:34:33.857 1944-2267/? D/bt_upio: upio_set : pio 0 action 2, polarity 1
2022-06-13 17:34:33.857 1944-2267/? D/bt_upio: BT_WAKE is asserted already
2022-06-13 17:34:33.857 1944-2370/? E/bt_btif: bta_gattc_mark_bg_conn unable to find the bg connection mask for: 70:87:9e:0f:f8:7d
2022-06-13 17:34:33.857 1944-2113/? D/BtGatt.GattService: onDisconnected() - clientIf=10, connId=10, address=70:87:9E:0F:F8:7D
2022-06-13 17:34:33.857 12975-12987/org.test.demo12 D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=10 device=70:87:9E:0F:F8:7D
2022-06-13 17:34:33.857 844-2748/? V/AlarmManager:  remove PendingIntent] PendingIntent{c6946a9: PendingIntentRecord{96e10 com.android.bluetooth broadcastIntent}}
2022-06-13 17:34:33.857 844-1752/? V/AlarmManager:  remove PendingIntent] PendingIntent{c10c42e: PendingIntentRecord{96e10 com.android.bluetooth broadcastIntent}}
2022-06-13 17:34:33.867 12975-13001/org.test.demo12 D/BluetoothGatt: close()
2022-06-13 17:34:33.867 12975-13001/org.test.demo12 D/BluetoothGatt: unregisterApp() - mClientIf=10
2022-06-13 17:34:33.867 1944-7183/? D/BtGatt.GattService: unregisterClient() - clientIf=10
2022-06-13 17:34:33.867 12975-13001/org.test.demo12 I/python:  Traceback (most recent call last):
2022-06-13 17:34:33.867 12975-13001/org.test.demo12 I/python:    File "/content/.buildozer/android/app/main.py", line 225, in <module>
2022-06-13 17:34:33.867 12975-13001/org.test.demo12 I/python:    File "/content/.buildozer/android/platform/build-armeabi-v7a/build/other_builds/python3/armeabi-v7a__ndk_target_21/python3/Lib/asyncio/base_events.py", line 616, in run_until_complete
2022-06-13 17:34:33.867 12975-13001/org.test.demo12 I/python:    File "/content/.buildozer/android/app/main.py", line 218, in get_the_weight_from_the_mi_smart_scale_2
2022-06-13 17:34:33.867 12975-13001/org.test.demo12 I/python:    File "/content/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/demo12/armeabi-v7a/bleak/backends/p4android/client.py", line 486, in start_notify
2022-06-13 17:34:33.867 12975-13001/org.test.demo12 I/python:  bleak.exc.BleakError: Characteristic with UUID 00002a9d-0000-1000-8000-00805f9b34fb could not be found!
2022-06-13 17:34:33.867 12975-13001/org.test.demo12 I/python: Python for android ended.
2022-06-13 17:34:33.897 1944-2370/? I/bt_btm_sec: btm_sec_disconnected clearing pending flag handle:64 reason:22

This is the code I converted with Colab:

import asyncio

from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty

from bleak import BleakClient


kv = '''
WindowManager:
    
    MainWindow:
    BluetoothWindow:
    LoginWindow:
    
        
     
    
<LoginWindow>:
    name: "login"

    email: email
    password: password

    FloatLayout:

        Label:
            text:"Email: "
            font_size: (root.width**2 + root.height**2) / 13**4
            pos_hint: {"x":0.1, "top":0.9}
            size_hint: 0.35, 0.15

        TextInput:
            id: email
            font_size: (root.width**2 + root.height**2) / 13**4
            multiline: False
            pos_hint: {"x": 0.45 , "top":0.9}
            size_hint: 0.4, 0.15

        Label:
            text:"Password: "
            font_size: (root.width**2 + root.height**2) / 13**4
            pos_hint: {"x":0.1, "top":0.7}
            size_hint: 0.35, 0.15

        TextInput:
            id: password
            font_size: (root.width**2 + root.height**2) / 13**4
            multiline: False
            password: True
            pos_hint: {"x": 0.45, "top":0.7}
            size_hint: 0.4, 0.15

        Button:
            pos_hint:{"x":0.2,"y":0.05}
            size_hint: 0.6, 0.2
            font_size: (root.width**2 + root.height**2) / 13**4
            text: "Login"
            on_release:
                app.root.current = "main"
                root.manager.transition.direction = "left"

        Button:
            pos_hint:{"x":0.2,"y":0.3}
            size_hint: 0.6, 0.1
            font_size: (root.width**2 + root.height**2) / 17**4
            text: "Non hai un account? Creane uno"  
            on_release:
     
                
     
        
     
<MainWindow>:
    name: "main"
    BoxLayout:
		orientation: "vertical"
		size: root.width, root.height
        
    FloatLayout:
        n: n
        email: email
        created:created

        FloatLayout:
            Label:
                id: n
                pos_hint:{"x": 0.1, "top":0.9}
                size_hint:0.8, 0.2
                text: "Nome account: "

            Label:
                id: email
                pos_hint:{"x": 0.1, "top":0.7}
                size_hint:0.8, 0.2
                text: "Email: "

            Label:
                id: created
                pos_hint:{"x": 0.1, "top":0.5}
                size_hint:0.8, 0.2
                text: "Creato il: "

            Button:
                pos_hint:{"x":0.35, "y": 0.2}
                size_hint:0.3,0.1
                text: "Log Out"
                on_release:
                    app.root.current = "login"
                    root.manager.transition.direction = "right"
                

            Button:
                pos_hint:{"x":0.35, "y": 0.1}
                size_hint:0.3,0.1
                text: "Log In"
                on_release:
                    app.root.current = "ble"
                    root.manager.transition.direction = "left"
                
<BluetoothWindow>:
    name: "ble"
    peso: peso

    
    BoxLayout:
		orientation: "vertical"
		size: root.width, root.height

    FloatLayout:
        Label:
            id: peso
            pos_hint:{"x": 0.1, "top":0.7}
            size_hint:0.8, 0.2
            text: "peso: "
        
        Button:
            pos_hint:{"x":0.2, "y": 0.1}
            size_hint:0.6,0.2
            text: "Log Out"
            on_release:
                app.root.current = "main"
                root.manager.transition.direction = "right"
'''

class LoginWindow(Screen):
    pass

class MainWindow(Screen):
    pass

class BluetoothWindow(Screen):
    peso = ObjectProperty(None)
    
    def on_enter(self):

        self.peso.text = "peso: " + str(data2)


class WindowManager(ScreenManager):
    pass

sm = WindowManager()

screens = [MainWindow(name="main"), 
           BluetoothWindow(name="ble"),
           LoginWindow(name="login")]

for screen in screens:
    sm.add_widget(screen)


sm.current = "main"

class AsyncApp(App):

    other_task = None

    def build(self):
        return Builder.load_string(kv)
    
    def build2(self):
        return sm

    def app_func(self):
        '''This will run both methods asynchronously and then block until they
        are finished
        '''
        self.other_task = asyncio.ensure_future(self.get_the_weight_from_the_mi_smart_scale_2())

        async def run_wrapper():
            # we don't actually need to set asyncio as the lib because it is
            # the default, but it doesn't hurt to be explicit
            await self.async_run(async_lib='asyncio')
            print('App done')
            self.other_task.cancel()

        return asyncio.gather(run_wrapper(), self.other_task)
   
    global notification_handler
    def notification_handler(sender, data):
        """Simple notification handler which prints the data received."""
        data1 = list(data)
        global data2
        data2 = ((data1[1]+data1[2]*256)*0.005)
        print("{0}: {1}".format(sender, data2))

    async def get_the_weight_from_the_mi_smart_scale_2(self):
        #da modificare
        '''This method is also run by the asyncio loop and periodically prints
        something.
        '''
        async with BleakClient("70:87:9E:0F:F8:7D") as client:
            print(f"Connected: {client.is_connected}")
            
            await client.start_notify("00002a9d-0000-1000-8000-00805f9b34fb", notification_handler)
            await asyncio.sleep(30.0)
            await client.stop_notify("00002a9d-0000-1000-8000-00805f9b34fb")   


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(AsyncApp().app_func())
    loop.close()

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
dlechcommented, Jun 13, 2022

Was the output in https://github.com/hbldh/bleak/issues/846#issuecomment-1154151293 generated on Android or another OS?

0reactions
dlechcommented, Jul 13, 2022

Duplicate of #883

Read more comments on GitHub >

github_iconTop Results From Across the Web

Detect and diagnose crashes - Android Developers
An Android app crashes whenever there's an unexpected exit caused by an unhandled exception or signal. An app that is written using Java...
Read more >
Android App Bundle introduces Resource Not found crash in ...
I have found that other apps are having the same problem of Resource Not Found , which was not existing before using Android's...
Read more >
How to Diagnose App Issues Using Crash Logs - Papertrail
Getting the Crash Log · 1. Enable USB Debugging on Device · 2. Install the App via Android Studio and ADB · 3....
Read more >
11 common reasons for mobile app crashing | DECODE
Mobile apps crash for many different reasons. Sometimes, they are the result of inadequate app testing, poor memory management, or even device ...
Read more >
Fix an installed Android app that isn't working - Google Support
Try the following steps if an app installed on your phone has any of these problems: Crashing. Won't open. Won't respond. Isn't working...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found