无法使用 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 的相关文章

  • Android-如何将 android.net.Uri 对象转换为 java.net.URI 对象?

    我正在尝试获得一个FileInputStream用户从图片库中选择的图像上的对象 这是安卓URI由返回android provider MediaStore Images Media INTERNAL CONTENT URI content
  • Android主线程的IO操作

    我的问题有两个 是否建议在 Android 的主线程上进行 IO 操作 或者它是否有可能导致我的应用程序崩溃 如果在主线程上执行 IO 操作不理想 我可以使用哪些其他框架 以便当我的应用程序加载时它可以执行一些基本的 IO 文件读取并将值存
  • 如何替换 Android 中已弃用的 Bundle/Argument get(key) 调用

    我有以下扩展函数 允许我在应用程序活动和片段之间传递捆绑数据项 inline fun
  • Android 应用程序安装失败:包 com.my.app 在条目 AndroidManifest.xml 处没有证书

    在 Android Studio 中 我生成了带有密钥库等的签名 APK 将 APK 安装到设备上时失败 并显示 无法安装应用程序 并且在 Android Monitor 中我看到以下行 Package com my app has no
  • 如何在 iOS 上更改设备音量 - 而不是音乐音量

    我想更改 iOS iPhone 上的设备音量 我知道我可以使用以下几行更改音乐库的音量 implement at first MediaPlayer framework MPMusicPlayerController musicPlayer
  • socket.io xhr 在连接缓慢时出现错误(3G 移动网络)

    当我在 3G 移动网络 互联网连接速度慢 上测试我的真实聊天应用程序时 Socket io反复断开然后重新连接 我已经记录了原因 它说 xhr post error 这提高了 transport error 然后断开连接 我可以知道什么意思
  • 在 gradle 中,我应该排除分支下的所有依赖项还是只排除根就足够了?

    我已将以下自定义任务添加到我的build gradlefile 为了打印出依赖项的依赖项 This part is useful for finding conflict resolution s between dependencies
  • 为什么反射会减慢Android手机的速度

    我多次读到反射会降低手机性能 这有多真实 例如 在我的例子中 我从 Web 服务获取一些参数 这些参数与我在 Android 应用程序中的类的参数同名 所以我只是使用java字段和反射设置这些参数的值 它似乎并没有降低性能 有人可以向我解释
  • 退出设备上的 system.img

    我正在为我们部署给客户的设备 LG p509 Optimus 1 开发自动应用程序更新解决方案 我们可以控制这些设备 并且目前在它们上安装自定义内核 但不是完整的自定义 ROM 由于我们试图在设备上自动更新我们的应用程序 因此我们需要由我们
  • 如果使用grifika的ContinualCaptureActivity中的预览方式,相机预览的视野会更小

    我们知道 当相机预览比例设置为时 在相同距离下我们会得到更大的预览视野4 3代替16 9 具体如下 Android Camera API 奇怪的缩放效果 https stackoverflow com questions 20664628
  • ADB TCPIP 连接问题

    我有两台 Galaxy S3 其中一个已扎根 另一个则未扎根 因此 当我尝试通过本地网络连接它们时 计算机可以看到已root的计算机 但是正常的就卡在tcpip这一步了 所以 我写 adb tcpip 5555 It says restar
  • 错误类型 3 - 活动类不存在

    我正在尝试运行 webRTC 应用程序 但返回以下错误 启动应用程序 com onlinevoicecallapp com onlinevoicecallapp MainActivity 设备外壳命令 am start n com onli
  • ExpandableListview OnGroupClickListener 未触发

    我正在关注这个 以编程方式折叠 ExpandableListView 中的组 https stackoverflow com questions 4314777 programmatically collapse a group in ex
  • 控制 OverlayItem 大小

    我正在构建一个在单个 ItemizedOverlay 中包含几十个 OverlayItems 的地图 我的地图设计为可以非常近距离地查看 大约缩放级别 18 并且 OverlayItems 彼此非常接近 地图放大时看起来不错 但是 如果用户
  • 是否可以通过 Android 应用程序来录音?

    我是一名开发人员 希望创建一个 Android 应用程序来记录电话 这是出于我个人的需要 为了我自己的目的和记录而记录电话 是否有可能做到这一点 是否可以访问麦克风以及通过扬声器发出的声音 我对 Android 开发有点陌生 所以请耐心等待
  • 如何将 Android 添加到 Phonegap 平台版本 3

    经过大量挖掘 我相信这个问题 https stackoverflow com questions 18423444 phonegap 3 doesnt work with andriod studio与我没有添加任何用于构建phonegap
  • 如何为我的 Android Market APK 创建证书?

    我想将我的第一个 APK 应用程序上传到 Android Market 但我收到了此错误 顺便说一下 在 stackoverflow 中搜索时并没有引导我找到正确的链接 市场不接受使用调试证书签名的 APK 创建有效期至少 50 年的新证书
  • Android模拟器中的网络访问

    我试图通过我的 Android 应用程序访问互联网 但我既成功又失败 我在构建应用程序时启动模拟器 并且应用程序安装得很好 我可以使用浏览器访问互联网 但是 当我尝试这个小代码片段时 InetAddress inet try inet In
  • 离子初始加载时间

    我正在使用 Ionic 构建一个简单的应用程序 但我的应用程序在冷启动时的初始加载时间方面存在性能问题 这是我所做的 collection repeat 代替带有 track by 的 ng repeat 原生滚动 overflow scr
  • Android GetPTLAFormat 上的 Phonegap 错误

    我们正在开发一个使用 jQuery 移动和电话间隙的应用程序 一切似乎都工作正常 但是当在连接的 Android 手机上运行应用程序时 我们在 Eclipse logcat 中看到大量类似这样的错误 0 GetPTLAFormat inva

随机推荐