递归火山软件开发平台

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 火山 源码 类库
查看: 1942|回复: 2
打印 上一主题 下一主题

[安卓] 官方例程蓝牙搜索失败

[复制链接]

3

主题

11

帖子

63

积分

注册会员

Rank: 2

积分
63
跳转到指定楼层
楼主
发表于 2023-10-3 22:28:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 yefeng712 于 2023-10-3 22:35 编辑

用官方例程搜索蓝牙提示搜索失败,只换了UI,把官方源代码考过来也提示启动搜索失败!!不知道原因,请大神帮看看,谢谢!!




上传不了附件!!!
<火山程序 类型 = "通常" 版本 = 1 />

包 MWBT.XX <注释 = "在编译发布版本时请填写自己的包名">

类 启动类 <公开 基础类 = 窗口 注释 = "适配全面屏手机" 注释 = "开启硬件加速" 注释 = "防止软件启动弹出输入法" 注释 = "使用无标题白色窗口,安卓5.0才能运行."
        注释 = "因为设置了无标题清单,所以此处需要限制系统需求,21=安卓5.0." 注释 = "此处设置了常用的附加清单属性,如果不需要可删除." 隐藏值属性 = "0"
        @安卓.窗口.布局 = "baselineAligned = false\np_horz_align = left\np_layout_height_type = p_specified\np_layout_height_value = 950\np_layout_width_type = p_specified\np_layout_width_value = 395\np_vert_align = top\nweightSum = 0"
        @安卓.附加清单 = 常用窗口附加清单.全面屏 @安卓.附加清单 = 常用附加清单.开启硬件加速 @安卓.附加清单 = 常用窗口附加清单.不改变软键盘状态
        @安卓.附加清单 = 常用窗口附加清单.安卓5无标题白色窗口 @安卓.系统需求>
{
    变量 线性布局器1 <类型 = 线性布局器 隐藏值属性 = "0"
            @安卓.窗口.布局 = "baselineAligned = false\nctrl_id = 1\nlayout_weight = 0\norientation = horizontal\np_horz_align = left\np_layout_height_value = 70\np_layout_width_type = p_specified\np_layout_width_value = 380\nweightSum = 80">
    变量 编辑框2 <类型 = 编辑框 隐藏值属性 = "0"
            @安卓.窗口.布局 = "enabled = true\nlayout_marginLeft = 10\nlayout_weight = 0\np_horz_align = center_horizontal\np_layout_height_type = p_specified\np_layout_height_value = 60\np_layout_width_type = p_specified\np_layout_width_value = 220\np_vert_align = center_vertical\nparent_id = 1\ntext = \"34:FF:AD:4f:2A:5E\"\ntextSize = 16">
    变量 按钮1 <类型 = 按钮 注释 = "" 注释 = "" 隐藏值属性 = "0"
            @安卓.窗口.布局 = "background = \"E:\\\\E4A\\\\yem-server\\\\素材\\\\bt2.png\"\nctrl_order = 1\nenabled = true\nlayout_marginLeft = 10\nlayout_marginTop = 15\nlayout_weight = 0\np_horz_align = center_horizontal\np_layout_height_type = p_specified\np_layout_height_value = 50\np_layout_width_type = p_specified\np_layout_width_value = 100\np_vert_align = center_vertical\nparent_id = 1\ntext = 搜索设备\ntextSize = 18"
            "">
    变量 线性布局器13 <类型 = 线性布局器 隐藏值属性 = "0"
            @安卓.窗口.布局 = "ctrl_id = 2\nctrl_order = 1\np_layout_height_type = p_specified\np_layout_height_value = 25\np_layout_width_type = match_parent\np_layout_width_value = 380">
    变量 编辑框7 <类型 = 编辑框 隐藏值属性 = "0"
            @安卓.窗口.布局 = "enabled = false\np_layout_height_type = match_parent\np_layout_height_value = 10\np_layout_width_type = p_specified\np_layout_width_value = 360\nparent_id = 2">
    变量 线性布局器2 <类型 = 线性布局器 隐藏值属性 = "0"
            @安卓.窗口.布局 = "ctrl_id = 3\nctrl_order = 2\np_layout_height_type = p_specified\np_layout_height_value = 250\np_layout_width_type = p_specified\np_layout_width_value = 380\nweightSum = 80">
    变量 设备列表框1 <类型 = 自定义列表框 隐藏值属性 = "0"
            @安卓.窗口.布局 = "layout_weight = 50\np_layout_height_type = match_parent\np_layout_width_type = match_parent\nparent_id = 3">
    变量 线性布局器14 <类型 = 线性布局器 隐藏值属性 = "0"
            @安卓.窗口.布局 = "ctrl_id = 4\nctrl_order = 3\norientation = horizontal\np_layout_height_type = p_specified\np_layout_height_value = 25\np_layout_width_type = match_parent">
    变量 编辑框8 <类型 = 编辑框 隐藏值属性 = "0"
            @安卓.窗口.布局 = "enabled = false\np_layout_height_type = match_parent\np_layout_height_value = 10\np_layout_width_type = p_specified\np_layout_width_value = 360\np_vert_align = bottom\nparent_id = 4">
    变量 线性布局器3 <类型 = 线性布局器 隐藏值属性 = "0"
            @安卓.窗口.布局 = "ctrl_id = 5\nctrl_order = 4\norientation = horizontal\np_horz_align = left\np_layout_height_type = p_specified\np_layout_height_value = 50\np_layout_width_type = p_specified\np_layout_width_value = 380\nweightSum = 80">
    变量 文本框3 <类型 = 文本框 隐藏值属性 = "0"
            @安卓.窗口.布局 = "layout_margin = 0\nlayout_marginLeft = 10\np_horz_align = center_horizontal\np_layout_height_type = match_parent\np_layout_height_value = 40\np_layout_width_type = wrap_content\np_layout_width_value = 80\np_vert_align = center_vertical\nparent_id = 5\ntext = \"楼宇号:\"\ntextSize = 18">
    变量 编辑框1 <类型 = 编辑框 隐藏值属性 = "0"
            @安卓.窗口.布局 = "ctrl_order = 1\nlayout_gravity = left\np_horz_align = center_horizontal\np_layout_height_type = match_parent\np_layout_height_value = 40\np_layout_width_type = p_specified\np_layout_width_value = 40\np_vert_align = bottom\nparent_id = 5\ntextSize = 18">
    变量 文本框2 <类型 = 文本框 隐藏值属性 = "0"
            @安卓.窗口.布局 = "ctrl_order = 2\nlayout_marginLeft = 5\np_horz_align = center_horizontal\np_layout_height_type = match_parent\np_layout_height_value = 40\np_layout_width_type = wrap_content\np_layout_width_value = 80\np_vert_align = center_vertical\nparent_id = 5\ntext = \"楼层号:\"\ntextSize = 18">
    变量 编辑框3 <类型 = 编辑框 隐藏值属性 = "0"
            @安卓.窗口.布局 = "ctrl_order = 3\nlayout_gravity = left\np_horz_align = center_horizontal\np_layout_height_type = match_parent\np_layout_height_value = 40\np_layout_width_type = p_specified\np_layout_width_value = 40\np_vert_align = bottom\nparent_id = 5\ntextSize = 18">
    变量 文本框1 <类型 = 文本框 隐藏值属性 = "0"
            @安卓.窗口.布局 = "ctrl_order = 4\nlayout_marginLeft = 5\np_horz_align = center_horizontal\np_layout_height_type = match_parent\np_layout_height_value = 40\np_layout_width_type = wrap_content\np_layout_width_value = 80\np_vert_align = center_vertical\nparent_id = 5\ntext = \"房间号:\"\ntextSize = 18">
    变量 编辑框4 <类型 = 编辑框 隐藏值属性 = "0"
            @安卓.窗口.布局 = "ctrl_order = 5\nlayout_gravity = left\nlayout_marginLeft = -5\np_horz_align = center_horizontal\np_layout_height_type = match_parent\np_layout_height_value = 40\np_layout_width_type = p_specified\np_layout_width_value = 40\np_vert_align = bottom\nparent_id = 5\ntextSize = 18">
    变量 线性布局器9 <类型 = 线性布局器 隐藏值属性 = "0"
            @安卓.窗口.布局 = "ctrl_id = 6\nctrl_order = 5\norientation = horizontal\np_layout_height_type = p_specified\np_layout_height_value = 25\np_layout_width_type = match_parent">
    变量 编辑框9 <类型 = 编辑框 隐藏值属性 = "0"
            @安卓.窗口.布局 = "enabled = false\np_layout_height_type = match_parent\np_layout_width_type = p_specified\np_layout_width_value = 360\np_vert_align = bottom\nparent_id = 6">
    变量 线性布局器4 <类型 = 线性布局器 隐藏值属性 = "0"
            @安卓.窗口.布局 = "ctrl_id = 7\nctrl_order = 6\norientation = horizontal\np_horz_align = left\np_layout_height_type = p_specified\np_layout_height_value = 50\np_layout_width_type = p_specified\np_layout_width_value = 380\np_vert_align = center_vertical\nweightSum = 0">
    变量 按钮2 <类型 = 按钮 隐藏值属性 = "0"
            @安卓.窗口.布局 = "background = \"E:\\\\E4A\\\\yem-server\\\\素材\\\\bt2.png\"\nlayout_marginLeft = 10\np_horz_align = center_horizontal\np_layout_height_type = match_parent\np_layout_height_value = 40\np_layout_width_type = p_specified\np_layout_width_value = 160\np_vert_align = center_vertical\nparent_id = 7\ntext = 更改设备名称\ntextSize = 18"
            "">
    变量 按钮3 <类型 = 按钮 隐藏值属性 = "0"
            @安卓.窗口.布局 = "background = \"E:\\\\E4A\\\\yem-server\\\\素材\\\\bt2.png\"\nctrl_order = 1\nlayout_marginLeft = 20\np_horz_align = center_horizontal\np_layout_height_type = match_parent\np_layout_height_value = 40\np_layout_width_type = p_specified\np_layout_width_value = 160\np_vert_align = center_vertical\nparent_id = 7\ntext = 打印设备名称\ntextSize = 18"
            "">
    变量 线性布局器10 <类型 = 线性布局器 隐藏值属性 = "0"
            @安卓.窗口.布局 = "ctrl_id = 8\nctrl_order = 7\norientation = horizontal\np_layout_height_type = p_specified\np_layout_height_value = 25\np_layout_width_type = match_parent">
    变量 编辑框10 <类型 = 编辑框 隐藏值属性 = "0"
            @安卓.窗口.布局 = "enabled = false\np_layout_height_type = match_parent\np_layout_width_type = p_specified\np_layout_width_value = 360\np_vert_align = bottom\nparent_id = 8">
    变量 线性布局器5 <类型 = 线性布局器 隐藏值属性 = "0"
            @安卓.窗口.布局 = "ctrl_id = 9\nctrl_order = 8\norientation = horizontal\np_layout_height_type = p_specified\np_layout_height_value = 50\np_layout_width_type = p_specified\np_layout_width_value = 380">
    变量 按钮4 <类型 = 按钮 隐藏值属性 = "0"
            @安卓.窗口.布局 = "background = \"E:\\\\E4A\\\\yem-server\\\\素材\\\\bt2.png\"\nlayout_marginLeft = 10\np_horz_align = center_horizontal\np_layout_height_type = match_parent\np_layout_height_value = 40\np_layout_width_type = p_specified\np_layout_width_value = 160\np_vert_align = center_vertical\nparent_id = 9\ntext = 锁定设备\ntextSize = 18"
            "">
    变量 按钮5 <类型 = 按钮 隐藏值属性 = "0"
            @安卓.窗口.布局 = "background = \"E:\\\\E4A\\\\yem-server\\\\素材\\\\bt2.png\"\nctrl_order = 1\nlayout_marginLeft = 20\nlayout_weight = 0\np_horz_align = center_horizontal\np_layout_height_type = match_parent\np_layout_height_value = 40\np_layout_width_type = p_specified\np_layout_width_value = 160\np_vert_align = center_vertical\nparent_id = 9\ntext = 解锁设备\ntextSize = 18"
            "">
    变量 线性布局器11 <类型 = 线性布局器 隐藏值属性 = "0"
            @安卓.窗口.布局 = "ctrl_id = 10\nctrl_order = 9\norientation = horizontal\np_layout_height_type = p_specified\np_layout_height_value = 25\np_layout_width_type = match_parent">
    变量 编辑框12 <类型 = 编辑框 隐藏值属性 = "0"
            @安卓.窗口.布局 = "enabled = false\np_layout_height_type = match_parent\np_layout_width_type = p_specified\np_layout_width_value = 360\np_vert_align = bottom\nparent_id = 10">
    变量 线性布局器6 <类型 = 线性布局器 隐藏值属性 = "0"
            @安卓.窗口.布局 = "ctrl_id = 11\nctrl_order = 10\norientation = horizontal\np_layout_height_type = p_specified\np_layout_height_value = 50\np_layout_width_type = p_specified\np_layout_width_value = 380">
    变量 按钮6 <类型 = 按钮 隐藏值属性 = "0"
            @安卓.窗口.布局 = "background = \"E:\\\\E4A\\\\yem-server\\\\素材\\\\bt2.png\"\nlayout_marginLeft = 10\np_horz_align = center_horizontal\np_layout_height_type = match_parent\np_layout_height_value = 40\np_layout_width_type = p_specified\np_layout_width_value = 110\np_vert_align = center_vertical\nparent_id = 11\ntext = 购买天数\ntextSize = 18"
            "">
    变量 编辑框5 <类型 = 编辑框 隐藏值属性 = "0"
            @安卓.窗口.布局 = "contentDescription = 最大255天\nctrl_order = 1\nlayout_marginLeft = 5\np_horz_align = center_horizontal\np_layout_height_type = match_parent\np_layout_height_value = 40\np_layout_width_type = p_specified\np_layout_width_value = 58\np_vert_align = bottom\nparent_id = 11\ntextSize = 18">
    变量 按钮7 <类型 = 按钮 隐藏值属性 = "0"
            @安卓.窗口.布局 = "background = \"E:\\\\E4A\\\\yem-server\\\\素材\\\\bt2.png\"\nctrl_order = 2\nlayout_marginLeft = 5\np_horz_align = center_horizontal\np_layout_height_type = match_parent\np_layout_height_value = 40\np_layout_width_type = p_specified\np_layout_width_value = 100\np_vert_align = center_vertical\nparent_id = 11\ntext = 剩余天数\ntextSize = 18"
            "">
    变量 编辑框6 <类型 = 编辑框 隐藏值属性 = "0"
            @安卓.窗口.布局 = "ctrl_order = 3\nenabled = false\nlayout_marginLeft = 5\np_horz_align = center_horizontal\np_layout_height_type = match_parent\np_layout_height_value = 40\np_layout_width_type = p_specified\np_layout_width_value = 57\np_vert_align = bottom\nparent_id = 11\ntextSize = 18">
    变量 线性布局器12 <类型 = 线性布局器 隐藏值属性 = "0"
            @安卓.窗口.布局 = "ctrl_id = 12\nctrl_order = 11\norientation = horizontal\np_layout_height_type = p_specified\np_layout_height_value = 25\np_layout_width_type = match_parent">
    变量 编辑框11 <类型 = 编辑框 隐藏值属性 = "0"
            @安卓.窗口.布局 = "enabled = false\np_layout_height_type = match_parent\np_layout_width_type = p_specified\np_layout_width_value = 360\np_vert_align = bottom\nparent_id = 12">
    变量 线性布局器7 <类型 = 线性布局器 隐藏值属性 = "0"
            @安卓.窗口.布局 = "ctrl_id = 13\nctrl_order = 12\norientation = horizontal\np_layout_height_type = p_specified\np_layout_height_value = 50\np_layout_width_type = p_specified\np_layout_width_value = 380">
    变量 按钮8 <类型 = 按钮 隐藏值属性 = "0"
            @安卓.窗口.布局 = "background = \"E:\\\\E4A\\\\yem-server\\\\素材\\\\bt2.png\"\nlayout_marginLeft = 10\np_horz_align = center_horizontal\np_layout_height_type = match_parent\np_layout_height_value = 40\np_layout_width_type = p_specified\np_layout_width_value = 160\np_vert_align = center_vertical\nparent_id = 13\ntext = 解除到期锁屏\ntextSize = 18">
    变量 按钮9 <类型 = 按钮 隐藏值属性 = "0"
            @安卓.窗口.布局 = "background = \"E:\\\\E4A\\\\yem-server\\\\素材\\\\bt2.png\"\nctrl_order = 1\nlayout_marginLeft = 20\np_horz_align = center_horizontal\np_layout_height_type = match_parent\np_layout_height_value = 40\np_layout_width_type = p_specified\np_layout_width_value = 160\np_vert_align = center_vertical\nparent_id = 13\ntext = 退出系统\ntextSize = 18">
    变量 蓝牙适配器1 <参考 类型 = 蓝牙适配器类>
    变量 蓝牙开关状态 <类型 = 逻辑型 值 = 真>
    变量 蓝牙设备搜索状态 <类型 = 逻辑型>

    # //定义广播接收器,以获取蓝牙的搜索结果

    变量 蓝牙搜索结果广播 <类型 = 全局广播动态接收器>

    # //定义一个用于储存搜索到新设备的蓝牙设备列表

    变量 搜索到的蓝牙设备列表 <参考 类型 = 蓝牙设备列表类>
    变量 蓝牙开关检测线程 <公开 类型 = 线程类 注释 = "用于实时检测当前设备的蓝牙各种状态">

    方法 通知_被启动 <公开 @虚拟方法 = 可覆盖>
    {
        父对象.通知_被启动 ()
        // 初始化蓝牙适配器对象
        蓝牙适配器1 = 蓝牙适配器类.取默认适配器 ()
        如果 (蓝牙适配器1 == 空对象)  // 判断是否初始化成功了,如果为空对象,表示设备不支持蓝牙.
        {
            提示信息框 ("当前设备不支持蓝牙功能!")
            返回
        }

        // 蓝牙开关检测线程.启动 ()  // 启动一个状态检测线程,以实时获取蓝牙当前的各种状态.
        如果 (蓝牙适配器1.可用)
        {
            载入已配对设备列表 ()  // 加载已配对设备列表

        }
    }

    方法 按钮_被单击 <接收事件 类型 = 整数 注释 = "用户在组件上单击事件" 注释 = "注意:"
            注释 = "  1. \"按钮\"类别及\"图片按钮\"组件会自动初始设置\"支持单击\"属性为真,不需要单独设置;"
            注释 = "  2. 除开上述类别组件外,必须将组件的\"支持单击\"属性设置为真才会发送本事件.">
    参数 来源对象 <类型 = 按钮 注释 = "提供事件产生的具体来源对象">
    参数 标记值 <类型 = 整数 注释 = "用户调用\"挂接事件\"命令时所提供的\"标记值\"参数值,非此方式挂接事件则本参数值固定为0.">
    {
        变量 背景图像 <类型 = 状态到图像表 被按下 = "E:\\E4A\\yem-server\\素材\\bt1.png" 通常 = "E:\\E4A\\yem-server\\素材\\bt2.png">


        如果 (来源对象 == 按钮1)
        {
            按钮1.置状态背景图 (背景图像)
            蓝牙适配器类.请求打开蓝牙 (本对象)
            设备列表框1.适配器.删除所有项目 ()

            搜索新设备 ()
            载入已配对设备列表 ()
            返回 (0)

        }
        如果 (来源对象 == 按钮2)
        {
            按钮2.置状态背景图 (背景图像)
            设备列表框1.适配器.删除所有项目 ()
            搜索新设备 ()
            返回 (0)

        }
        如果 (来源对象 == 按钮3)
        {
            按钮3.置状态背景图 (背景图像)
            设备列表框1.适配器.删除所有项目 ()
            载入已配对设备列表 ()
            返回 (0)
        }

        如果 (来源对象 == 按钮4)
        {
            按钮4.置状态背景图 (背景图像)
            返回 (0)
        }

        如果 (来源对象 == 按钮5)
        {
            按钮5.置状态背景图 (背景图像)
            返回 (0)
        }

        如果 (来源对象 == 按钮6)
        {
            按钮6.置状态背景图 (背景图像)
            返回 (0)
        }

        如果 (来源对象 == 按钮7)
        {
            按钮7.置状态背景图 (背景图像)
            返回 (0)
        }

        如果 (来源对象 == 按钮8)
        {
            按钮8.置状态背景图 (背景图像)
            返回 (0)
        }


        如果 (来源对象 == 按钮9)
        {
            按钮9.置状态背景图 (背景图像)
            退出程序 ()
            返回 (0)
        }
        返回 (0)

    }

    方法 搜索新设备
    {
        如果 (蓝牙开关状态)
        {
            蓝牙搜索结果广播.登记 (, 蓝牙设备类.寻找设备, 蓝牙设备类.绑定状态变化)  // 注册广播以接收搜索回调.
            变量 局_新建数组列表 <类型 = 蓝牙设备数组类>
            搜索到的蓝牙设备列表 = 局_新建数组列表  // 初始化列表对象,用于储存搜索接收到的蓝牙设备象.
            如果 (蓝牙适配器1.可用 == 假)
            {
                蓝牙适配器1.可用 = 真
            }
            // 开始搜索 搜索完成后会在一个广播中回调结果
            如果 (蓝牙适配器1.开始搜索设备 ())
            {
                提示框 ("已开始扫描设备!")
            }
            否则
            {
                提示框 ("启动扫描失败!")
            }
        }
        否则
        {
            提示框 ("蓝牙当前处于关闭状态!")
        }

    }

    方法 线程类_线程启动 <接收事件 类型 = 整数>
    参数 来源对象 <类型 = 线程类>
    参数 标记值 <类型 = 整数>
    参数 用户对象1 <类型 = 对象类>
    参数 用户对象2 <类型 = 对象类>
    {
        判断循环 (真)
        {
            如果 (蓝牙适配器1.可用)
            {
                蓝牙开关状态 = 真
                按钮1.内容 = "关闭搜索"
                如果 (蓝牙适配器1.搜索设备状态)
                {
                    蓝牙设备搜索状态 = 真
                    如果 (蓝牙适配器1.扫描模式 == 蓝牙适配器扫描模式.启用所有扫描cha询)
                    {
                        编辑框2.内容 = "蓝牙:开启|搜索状态:真"
                        按钮1.内容 = "关闭搜索"
                    }
                    否则 (蓝牙适配器1.扫描模式 == 蓝牙适配器扫描模式.禁用cha询扫描)
                    {
                        编辑框2.内容 = "蓝牙:开启|搜索状态:真"
                        按钮1.内容 = "关闭搜索"
                    }
                    否则
                    {
                        编辑框2.内容 = "蓝牙:开启|搜索状态:真"
                        按钮1.内容 = "关闭搜索"
                    }
                }
                否则
                {
                    蓝牙设备搜索状态 = 假
                    如果 (蓝牙适配器1.扫描模式 == 蓝牙适配器扫描模式.启用所有扫描cha询)
                    {
                        编辑框2.内容 = "蓝牙:开启|搜索状态:假"
                        按钮1.内容 = "开始搜索"
                    }
                    否则 (蓝牙适配器1.扫描模式 == 蓝牙适配器扫描模式.禁用cha询扫描)
                    {
                        编辑框2.内容 = "蓝牙:开启|搜索状态:假"
                        按钮1.内容 = "开始搜索"
                    }
                    否则
                    {
                        编辑框2.内容 = "蓝牙:开启|搜索状态:假"
                        按钮1.内容 = "开始搜索"
                    }
                }

            }
            否则
            {
                编辑框2.内容 = "当前蓝牙状态:关闭"
                按钮1.内容 = "开始搜索"
                蓝牙开关状态 = 假
            }
            睡眠当前线程 (1000)  // 1秒检测一次状态
        }

    }

    方法 载入已配对设备列表 <注释 = "把已配对设备做一个列表出来">
    {
        变量 蓝牙设备枚举 <参考 类型 = 蓝牙设备枚举器类>
        // 先判断下蓝牙的开关状态,处于关闭状态就无法获取配对设备.
        如果 (蓝牙开关状态 != 真)
        {
            提示框 ("蓝牙当前处于关闭状态!")
            返回
        }

        // 先取出已配对设备集合,创建枚举器对象
        蓝牙设备枚举 = 蓝牙适配器1.配对设备集合.创建枚举器 ()
        // 设备列表框1.适配器.删除所有项目 ()
        判断循环 (蓝牙设备枚举.是否有下一成员 ())  // 用枚举器遍历设备集合对象,取出单个设备对象
        {
            变量 列表数据 <类型 = 蓝牙设备列表数据类>
            变量 局_设备对象 <参考 类型 = 蓝牙设备类>
            局_设备对象 = 蓝牙设备枚举.到下一成员 ()
            列表数据.MAC地址 = 局_设备对象.地址
            列表数据.设备名称 = 局_设备对象.名称
            列表数据.设备对象 = 局_设备对象
            设备列表框1.适配器.插入项目 (列表数据)
        }
        如果 (设备列表框1.适配器.取项目数 () == 0)  // 判断空集合
        {
            提示框 ("暂无已配对设备!")
        }

    }

    方法 载入查找新设备列表 <注释 = "把搜寻到的新设备做成列表">
    {
        如果 (搜索到的蓝牙设备列表 == 空对象)
        {
            提示框 ("还没有开始查找")
            返回
        }
        变量 i <类型 = 整数>
        // 设备列表框1.适配器.删除所有项目 ()
        循环 (, 搜索到的蓝牙设备列表.取成员数 (), i)
        {
            变量 列表数据 <类型 = 蓝牙设备列表数据类>
            列表数据.MAC地址 = 搜索到的蓝牙设备列表.取成员 (i).地址
            列表数据.设备名称 = 搜索到的蓝牙设备列表.取成员 (i).名称
            列表数据.设备对象 = 搜索到的蓝牙设备列表.取成员 (i)
            设备列表框1.适配器.插入项目 (列表数据)
        }
        如果 (设备列表框1.适配器.取项目数 () == 0)  // 判断空数据
        {
            提示框 ("没有查找到新设备!")
        }

    }

    方法 自定义列表框_取对象项目视图 <接收事件 类型 = 整数>
    参数 来源对象 <类型 = 自定义列表框>
    参数 标记值 <类型 = 整数>
    参数 项目信息 <类型 = 对象数据适配信息>
    {
        变量 布局器组件1 <类型 = 线性布局器>
        变量 布局器组件2 <类型 = 线性布局器>
        变量 设备名 <类型 = 文本框>
        变量 地址 <类型 = 文本框>
        变量 图标 <类型 = 图片框>
        // 将设备信息显示在列表中
        如果 (来源对象 == 设备列表框1)
        {
            设备名.内容 = ((蓝牙设备列表数据类)设备列表框1.适配器.取项目 (项目信息.项目索引位置)).设备名称 + "MAC地址:" + ((蓝牙设备列表数据类)设备列表框1.适配器.取项目 (项目信息.项目索引位置)).MAC地址
            地址.内容 = "MAC地址:" + ((蓝牙设备列表数据类)设备列表框1.适配器.取项目 (项目信息.项目索引位置)).MAC地址
            图标.图片 = ((蓝牙设备列表数据类)设备列表框1.适配器.取项目 (项目信息.项目索引位置)).设备图标
        }

        如果 (来源对象 == 设备列表框1)
        {
            设备名.内容 = ((蓝牙设备列表数据类)设备列表框1.适配器.取项目 (项目信息.项目索引位置)).设备名称
            地址.内容 = "MAC地址:" + ((蓝牙设备列表数据类)设备列表框1.适配器.取项目 (项目信息.项目索引位置)).MAC地址
            图标.图片 = ((蓝牙设备列表数据类)设备列表框1.适配器.取项目 (项目信息.项目索引位置)).设备图标
        }
        布局器组件1.布局方向 = 线性布局方向.横向
        布局器组件2.布局方向 = 线性布局方向.纵向
        布局器组件2.添加子组件 (设备名)
        布局器组件2.添加子组件 (地址)
        布局器组件2.左内边距 = (整数)DP到像素 (10)
        布局器组件1.添加子组件 (图标)
        布局器组件1.添加子组件 (布局器组件2)
        项目信息.所返回组件 = 布局器组件1
        返回 (0)
    }

    方法 全局广播动态接收器_接收到全局广播 <接收事件 类型 = 整数>
    参数 来源对象 <类型 = 全局广播动态接收器>
    参数 标记值 <类型 = 整数>
    参数 接收器对象 <类型 = 安卓广播接收器>
    参数 启动信息 <类型 = 启动信息类>
    {
        变量 动作文本 <类型 = 文本型>
        变量 局_设备对象 <参考 类型 = 蓝牙设备类>
        动作文本 = 启动信息.取动作文本 ()

        如果 (文本相等 (动作文本, 蓝牙设备类.寻找设备))  // 判断是不是蓝牙搜索的广播接收
        {
            局_设备对象 = (蓝牙设备类)启动信息.取分包附带数据 (蓝牙设备类.额外字段_蓝牙设备对象)  // 取回搜索到的设备对象.
            // 此处还可以取更多的额外字段信息, 例如要取该设备的信号强度:启动信息.取附带数据包().读短整数字段(蓝牙设备类.信号值)

            // 判断一下是不是已经配对绑定过的设备,如果是已配对的设备则不添加到搜索列表中.
            // 如果 (局_设备对象.绑定状态 != 蓝牙设备类.已配对)
            {
                搜索到的蓝牙设备列表.添加成员 (局_设备对象)  // 将未绑定过的设备对象添加到列表对象中储存.
            }
            载入查找新设备列表 ()
        }
        否则 (文本相等 (动作文本, 蓝牙设备类.绑定状态变化))
        {
            搜索新设备 ()
        }

        返回 (0)
    }

    # 把设备列表用到的数据定义一个类===
    # //定义一个蓝牙适配器对象并初始化它,很多蓝牙操作都是由该对象执行的.
}

类 蓝牙设备列表数据类 <公开>
{
    变量 设备图标 <公开 类型 = 可绘制资源 值 = "E:\\E4A\\yem-server\\素材\\ly1.png" 编辑时信息 = "0, 51550, 0, 0">
    变量 设备名称 <公开 类型 = 文本型 编辑时信息 = "0, 51550, 0, 0">
    变量 MAC地址 <公开 类型 = 文本型 编辑时信息 = "0, 51550, 0, 0">
    变量 设备对象 <公开 参考 类型 = 蓝牙设备类 编辑时信息 = "0, 51550, 0, 0">
}

回复

使用道具 举报

444

主题

1万

帖子

3万

积分

超级版主

Rank: 8Rank: 8

积分
39356
沙发
发表于 2023-10-4 07:53:31 | 只看该作者
例子直接运行也扫描失败?前些天我还看过蓝牙例子,可以扫描成功的。。
安卓无障碍实战课:点击查看
交流群:641526939
回复

使用道具 举报

3

主题

11

帖子

63

积分

注册会员

Rank: 2

积分
63
板凳
 楼主| 发表于 2023-10-4 17:09:33 | 只看该作者
创世魂 发表于 2023-10-4 07:53
例子直接运行也扫描失败?前些天我还看过蓝牙例子,可以扫描成功的。。

也搜索蓝牙失败,直接运行例程可以,我把代码考过来就不行
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|递归火山软件开发平台 ( 鄂ICP备18029190号 )

GMT+8, 2024-9-25 06:45 , Processed in 0.093799 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表