无法使用 Android BluetoothProfile 连接到蓝牙 Health Device Fora

2023-11-30

我想通过 Android BluetoothPROfile 连接到 Fora 温度计并获取读数。 以下是我的方法:-

  1. 在 OnCreate() 中我写了这段代码:-

    if (!mBluetoothAdapter.getProfileProxy(this, mBluetoothServiceListener,
            BluetoothProfile.HEALTH)) {
        Toast.makeText(this, "No Health Profile Supported",
                Toast.LENGTH_LONG);
        return;
    }
    
  2. 这会触发下面提到的 mBluetoothServiceListener 回调:-

    @SuppressLint("NewApi")
    private final BluetoothProfile.ServiceListener mBluetoothServiceListener =
    new BluetoothProfile.ServiceListener() {
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
    
        Log.d(TAG, "onServiceConnected to profile: " + profile + " while health is " + BluetoothProfile.HEALTH);
        if (profile == BluetoothProfile.HEALTH) {
            mBluetoothHealth = (BluetoothHealth) proxy;
            if (Log.isLoggable(TAG, Log.DEBUG))
                Log.d(TAG, "onServiceConnected to profile: " + profile);
        }
        else if(profile == BluetoothProfile.HEADSET)
        {
            Log.d(TAG, "onServiceConnected to profile: " + profile);
        }
    }
    
    public void onServiceDisconnected(int profile) {
        if (profile == BluetoothProfile.HEALTH) {
            mBluetoothHealth = null;
        }
    }
    };
    
  3. 此后,代码搜索附近的蓝牙设备并将其显示在列表中。 该列表项的 onclicklistener 调用以下代码:-

    boolean bool = mBluetoothHealth.registerSinkAppConfiguration(TAG, HEALTH_PROFILE_SOURCE_DATA_TYPE, mHealthCallback);
    
    // where HEALTH_PROFILE_SOURCE_DATA_TYPE = 0x1008 (it's a body thermometer)
    
  4. 注册过程完成后,我尝试像这样连接设备:-

        boolean bool = mBluetoothHealth.connectChannelToSource(mDevice, mHealthAppConfig);
    
  5. 完成上述所有步骤后,每当 设备连接状态改变

            private final BluetoothHealthCallback mHealthCallback = new BluetoothHealthCallback() {
    // Callback to handle application registration and unregistration events.  The service
    // passes the status back to the UI client.
    public void onHealthAppConfigurationStatusChange(BluetoothHealthAppConfiguration config,
            int status) {
        if (status == BluetoothHealth.APP_CONFIG_REGISTRATION_FAILURE) {
            mHealthAppConfig = null;
        } else if (status == BluetoothHealth.APP_CONFIG_REGISTRATION_SUCCESS) {
            mHealthAppConfig = config;
        } else if (status == BluetoothHealth.APP_CONFIG_UNREGISTRATION_FAILURE ||
                status == BluetoothHealth.APP_CONFIG_UNREGISTRATION_SUCCESS) {
        }
    }
    
    // Callback to handle channel connection state changes.
    // Note that the logic of the state machine may need to be modified based on the HDP device.
    // When the HDP device is connected, the received file descriptor is passed to the
    // ReadThread to read the content.
    public void onHealthChannelStateChange(BluetoothHealthAppConfiguration config,
            BluetoothDevice device, int prevState, int newState, ParcelFileDescriptor fd,
            int channelId) {
        if (Log.isLoggable(TAG, Log.DEBUG))
            Log.d(TAG, String.format("prevState\t%d ----------> newState\t%d",
                    prevState, newState));
        if (prevState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED &&
                newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {
            if (config.equals(mHealthAppConfig)) {
                mChannelId = channelId;
    
                (new ReadThread(fd)).start();
            } else {
    
            }
        } else if (prevState == BluetoothHealth.STATE_CHANNEL_CONNECTING &&
                newState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED) {
    
        } else if (newState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED) {
            if (config.equals(mHealthAppConfig)) {
    
            } else {
    
            }
        }
    }
        };
    

在上述情况下,我精确地收到了 2 次 BluetoothHealthCallback :-

  1. 我第一次得到 prevState = BluetoothHealth.STATE_CHANNEL_DISCONNECTED 和 newState = BluetoothHealth.STATE_CHANNEL_CONNECTING

  2. 在第二次时,我得到 prevState = BluetoothHealth.STATE_CHANNEL_CONNECTING 和 newState = BluetoothHealth.STATE_CHANNEL_DISCONNECTED

即尝试连接到设备但未成功。 此外,在这两个回调中,ParcelFileDescriptor 均为 null

*注意:设备已配对


您认为下面的“如果”陈述正确吗?

if (prevState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED && newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {

它应该是其中之一(就像经典蓝牙设备一样)

if (prevState == BluetoothHealth.STATE_CHANNEL_CONNECTING && newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {

当您先连接然后再连接时

或者我可能只是

if (newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {

对其他“if”语句进行同样的更正。

还, 设备是否支持配对并连接选项?如果是的话你可以尝试connect手动进行。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法使用 Android BluetoothProfile 连接到蓝牙 Health Device Fora 的相关文章

  • ActionBar 下拉微调器项目默认为第一项

    我试图设置默认情况下需要在微调器中选择的项目的索引 但它始终默认为 0 第一项 actionBar setDisplayShowTitleEnabled false actionBar setNavigationMode ActionBar
  • 围绕二维坐标系中的特定点缩放

    Below is an image my coordinate system 我想做的是 我想开始围绕画布中的特定点进行缩放 缩放工作正常 但我的问题是我不知道如何计算缩放时移动画布的量 请注意 我我没有使用canvas scale 我只是
  • 使用AndroidKeyStore身份验证的无限循环

    当我使用需要用户身份验证才能使用密钥的 AndroidKeyStore 时 我的应用程序进入无限循环 setUserAuthenticationRequired true setUserAuthenticationValidityDurat
  • com.android.ddmlib.AdbCommandRejectedException:设备离线(即使设备已连接)

    将 Android Studio 更新到 2 1 2 后 当我进行更改时 我多次收到以下错误 com android ddmlib AdbCommandRejectedException 设备离线 安装 APK 时出错 问题是设备从未连接且
  • Android 通知 - 显示完整消息

    我的 Android 应用程序必须能够向一大群人发送简短的警报 执行此操作的明显位置是在通知中心 完整的通知毫无问题地显示在股票代码中 但在通知中心 用户只能看到前几个单词 然后是省略号 通知并不长 最多也就10 15个字 如何使文本自动换
  • 无法实例化接收器 com.parse.GcmBroadcastReceiver

    我正在编写一个使用 GCM 通知和解析推送的离子应用程序 这个应用程序正在使用这些插件 com ionic keyboard 1 0 3 Keyboard com phonegap plugins PushPlugin 2 4 0 Push
  • 对齐卡片视图中的项目

    我希望我的卡片如下所示 我保持这样的布局
  • 请求超级用户权限编辑文件

    我正在规划一个需要编辑系统文件的应用程序 我只能使用 root 权限编辑该文件 我有一个已 root 且安装了 Superuser apk 的开发手机 其他需要 root 的应用程序会在首次启动时请求 root 访问权限 我想做同样的事情
  • AdMob 插页式广告仅显示一次

    当广告在一个会话内第二次或第三次打开时 LogCat 会显示错误消息 尝试使用不同的广告管理器启动新的 AdActivity 我通过应用程序主屏幕的 on resume 方法中的意图启动插页式广告 Override public void
  • 在 Marshmallow 中获取蓝牙本地 mac 地址

    在 Marshmallow 之前 我的应用程序将通过以下方式获取其设备 MAC 地址BluetoothAdapter getDefaultAdapter getAddress 现在 随着 Marshmallow Android 的回归02
  • 输入连接-如何删除选定的文本?

    我为 Android 制作了一个自定义键盘 当我按下键盘的退格按钮时 我使用 getCurrentInputConnection deleteSurroundingText 1 0 从输入字段中删除一个字母 但是 当我选择一些文本然后按退格
  • 动态选取框文本

    是否可以将列表视图的 java 编码中的文本添加到 Android 中的选取框滚动中 如果可以 请告诉我如何做 如果需要 我将发布使用的代码 这是列表视图使用的 XML 如下
  • Android系统每个应用程序的通知限制

    这可能偏离主题 但我找不到任何相关内容 Android应用程序可以显示的通知数量有限制吗 我在收到 100 条通知后遇到问题 没有文件明确说明这一点 注意 显示 100 条通知并不是一个好主意 但由于某些原因这是必需的 In API23 包
  • 单击输入字段会触发窗口调整大小

    我有一个带有徽标 菜单和搜索的标题 当我在桌面上时 我会按该顺序显示所有元素 但如果我的窗口宽度小于 980 像素 菜单会隐藏 有一个切换按钮 并且徽标会与nav并附在徽标之后 如果宽度更大 则徽标将再次分离并附加到 DOM 中的旧位置 w
  • 从代码动态更改多个文本视图的大小(没有“磁盘上”xml 主题)?

    我有 10 个文本视图在我的代码中 我想更改所有代码的字体大小 在我的布局中我使用了 style定义通用属性 但是我不知道一旦布局出现在屏幕上如何从代码中更改它们 我不想做的是更新 AND 对象 但只写在一处 我知道我可以使用应用主题但这假
  • 如何通过子 POJO 的属性过滤复合 ManyToMany POJO?

    我有两个像这样的房间实体 Entity public class Teacher implements Serializable PrimaryKey autoGenerate true public int id ColumnInfo n
  • 如何在kotlin中使用Coroutine每秒调用一个函数

    我刚刚创建了一个应用程序 其中我的函数 getdata 每秒调用一次以从服务器获取新数据 而 updateui 函数将更新 UI 中的视图 我在我的应用程序中不使用任何异步任务或协程 我想这样做 请告诉我我怎样才能做到这一点 这是我的代码
  • mgwt - 以编程方式改变方向

    是否可以在 gwt mgwt 应用程序中更改强制执行特定的屏幕方向 可以说我希望用户始终以横向模式使用应用程序 这取决于 是作为phonegap应用程序 而不是在浏览器内部 如果您作为 Web 应用程序运行 则不需要t get any co
  • Google Cloud Messaging - 立即收到或长时间延迟收到的消息

    我在大学最后一年的项目中使用谷歌云消息传递 一切正常 但我在使用 GCM 时遇到了一些麻烦 通常 消息要么几乎立即传递 要么有很大的延迟 我读过这篇文章 但我真的认为它不适用于这种情况 GCM 通常会在消息发送后立即传送消息 然而 这并不总
  • Android - 保留或删除应用程序卸载时创建的文件

    我创建了一个应用程序 用于创建文件并将其存储到 SD 卡 有没有办法将文件夹与应用程序绑定 以便当用户在 Android 设备上运行卸载时删除所有文件 自 2009 年以来似乎有了一些进展 来自文档 http developer andro

随机推荐