Skip to content

蓝牙初始化profile未使用关键字参数而是位置参数,导致调用使用关键字参数的调用位置全部报错 #2

Open

Description

@plumnew
Image

可以告诉我怎么自己刷入修改后的python库文件么?

Activity

  1. plumnew commented on Aug 27, 2025

    @plumnew
    Author
    import os
    
    # 定义文件内容为字符串列表
    file_content = [
        "# The MIT License (MIT)\n",
        "\n",
        "# Copyright (c) 2020, Tangliufeng for labplus Industries\n",
        "\n",
        "# Permission is hereby granted, free of charge, to any person obtaining a copy\n",
        "# of this software and associated documentation files (the \"Software\"), to deal\n",
        "# in the Software without restriction, including without limitation the rights\n",
        "# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n",
        "# copies of the Software, and to permit persons to whom the Software is\n",
        "# furnished to do so, subject to the following conditions:\n",
        "\n",
        "# The above copyright notice and this permission notice shall be included in\n",
        "# all copies or substantial portions of the Software.\n",
        "\n",
        "# THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n",
        "# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n",
        "# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n",
        "# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n",
        "# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n",
        "# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n",
        "# THE SOFTWARE.\n",
        "\n",
        "import bluetooth\n",
        "from ..advertising import advertising_payload\n",
        "from ..const import IRQ\n",
        "\n",
        "\n",
        "class Peripheral(object):\n",
        "    \"\"\" 外围设备 \"\"\"\n",
        "\n",
        "    def __init__(self, profile=None, name=b'mpy_ble', appearance=None, adv_services=None, resp_services=None, interval_us=500000, connectable=True):\n",
        "        self.interval_us = interval_us\n",
        "        self.connectable = connectable\n",
        "        self.ble = bluetooth.BLE()\n",
        "        self.ble.active(True)\n",
        "        self.ble.config(gap_name=name)\n",
        "        print(\"BLE: activated!\")\n",
        "        self.ble.irq(self._irq)\n",
        "        self.connections = set()\n",
        "        profile.handles = self.ble.gatts_register_services(profile.definition)\n",
        "        self._adv_payload = advertising_payload(name=name, appearance=appearance, services=adv_services)\n",
        "        self._resp_payload = advertising_payload(services=resp_services)\n",
        "        self._write_cb = None\n",
        "        self._connection_cb = None\n",
        "        # self.advertise(True)\n",
        "        self._debug = False\n",
        "\n",
        "    @property\n",
        "    def mac(self):\n",
        "        return self.ble.config('mac')\n",
        "\n",
        "    def write_callback(self, callback):\n",
        "        self._write_cb = callback\n",
        "\n",
        "    def connection_callback(self, callback):\n",
        "        self._connection_cb = callback\n",
        "\n",
        "    def _irq(self, event, data):\n",
        "        # Track connections so we can send notifications.\n",
        "        if self._debug:\n",
        "            print(\"Event: {}, Data: {}\".format(event, data))\n",
        "        if event == IRQ.IRQ_CENTRAL_CONNECT:\n",
        "            conn_handle, addr_type, addr = data\n",
        "            self.connections.add(conn_handle)\n",
        "            print(\"BLE: connect successful!\")\n",
        "            if self._connection_cb:\n",
        "                self._connection_cb(conn_handle, addr_type, addr)\n",
        "        elif event == IRQ.IRQ_CENTRAL_DISCONNECT:\n",
        "            conn_handle, addr_type, addr = data\n",
        "            if conn_handle in self.connections:\n",
        "                print(\"BLE: disconnected!\")\n",
        "                self.connections.remove(conn_handle)\n",
        "            # Start advertising again to allow a new connection.\n",
        "            self.advertise(True)\n",
        "        elif event == IRQ.IRQ_GATTS_WRITE:\n",
        "            conn_handle, attr_handle = data\n",
        "            if conn_handle in self.connections:\n",
        "                if self._write_cb:\n",
        "                    self._write_cb(conn_handle, attr_handle, self.attrubute_read(attr_handle))\n",
        "\n",
        "    def advertise(self, toggle=True):\n",
        "        if toggle:\n",
        "            self.ble.gap_advertise(self.interval_us, adv_data=self._adv_payload,\n",
        "                                   resp_data=self._resp_payload, connectable=self.connectable)\n",
        "        else:\n",
        "            self.ble.gap_advertise(interval_us=None)\n",
        "\n",
        "    def attrubute_write(self, value_handle, data, notify=False):\n",
        "        self.ble.gatts_write(value_handle, data)\n",
        "        if notify:\n",
        "            for conn_handle in self.connections:\n",
        "                # Notify connected centrals to issue a read.\n",
        "                self.ble.gatts_notify(conn_handle, value_handle)\n",
        "\n",
        "    def attrubute_read(self, value_handle):\n",
        "        return self.ble.gatts_read(value_handle)\n",
        "\n",
        "    def disconnect(self):\n",
        "        for conn_handle in self.connections:\n",
        "            self.ble.gap_disconnect(conn_handle)\n",
        "        self.connections.clear()\n"
    ]
    
    # 写入文件
    file_path = 'mpython_ble/application/peripheral.py'
    
    # 确保目录存在
    os.makedirs('mpython_ble/application', exist_ok=True)
    
    # 写入文件
    with open(file_path, 'w') as f:
        f.writelines(file_content)
    
    print(f"文件 {file_path} 写入完成")
    
    

    在掌控板中切换到python模式,然后使用micropython固件自带的文件系统更新存在bug的python文件,可以修改这个bug,以上为修复的代码。 修复构造函数的内容可以自行对比。


    建议官方IDE开放板端的库文件读写接口的界面,这样bug不会影响别人的使用。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions