无法使用 Android BluetoothProfile 连接到蓝牙健康设备论坛 [英] Unable to connect to bluetooth Health Device Fora using Android BluetoothProfile

查看:59
本文介绍了无法使用 Android BluetoothProfile 连接到蓝牙健康设备论坛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to connect to Fora thermometer via Android BluetoothPRofile and get the reading. Following is my approoach :-

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

  1. In OnCreate() i have written this piece of code :-

if (!mBluetoothAdapter.getProfileProxy(this, mBluetoothServiceListener,
        BluetoothProfile.HEALTH)) {
    Toast.makeText(this, "No Health Profile Supported",
            Toast.LENGTH_LONG);
    return;
}

  • 这会触发下面提到的 mBluetoothServiceListener 回调:-

  • This triggers the mBluetoothServiceListener callback which is mentioned below :-

    @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;
        }
    }
    };
    

  • 此后,代码会搜索附近的蓝牙设备并将其显示在列表中.该列表项的 onclicklistener 调用以下代码:-

  • After this the code searches for nearby bluetooth devices and shows it in the list. The onclicklistener of that list item calls the following code :-

    boolean bool = mBluetoothHealth.registerSinkAppConfiguration(TAG, HEALTH_PROFILE_SOURCE_DATA_TYPE, mHealthCallback);
    
    // where HEALTH_PROFILE_SOURCE_DATA_TYPE = 0x1008 (it's a body thermometer)
    

  • 注册过程完成后,我尝试像这样连接设备:-

  • Once the registeration process is done i try to connect the device like this :-

        boolean bool = mBluetoothHealth.connectChannelToSource(mDevice, mHealthAppConfig);
    

  • 完成上述所有步骤后,BluetoothHealthCallback 将在任何时候调用设备连接状态改变

  • Once all the above steps are done then the BluetoothHealthCallback is called whenever the device connection state changes

            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	%d ----------> newState	%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 :-

    In the above case i get the BluetoothHealthCallback precisely 2 times :-

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

    1. First Time i get the prevState = BluetoothHealth.STATE_CHANNEL_DISCONNECTED and newState = BluetoothHealth.STATE_CHANNEL_CONNECTING

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

    While in Second time i get the prevState = BluetoothHealth.STATE_CHANNEL_CONNECTING and newState = BluetoothHealth.STATE_CHANNEL_DISCONNECTED

    即已尝试连接到设备,但未成功.同样在两个回调中 ParcelFileDescriptor 为空

    i.e. the attempt is made to connect to the device but it is not succesfull. Also in both the callbacks the ParcelFileDescriptor is null

    *注意:设备已经配对

    推荐答案

    你觉得你下面的if"语句对吗?

    Do you think yours below "if" statement is right?

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

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

    它应该是(就像经典蓝牙设备发生的那样)

    It should be either (as it happen for classic Bluetooth devices)

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

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

    因为你先连接然后连接

    或者我可能只是

    if (newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {

    if (newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {

    对其他if"语句进行同样的更正.

    And same correction for other "if" statement.

    还有,设备是否支持配对和连接选项?如果是,那么您可以尝试手动连接.

    Also, Does device support paired and connected option? If yes then you may try to connect it manually.

    这篇关于无法使用 Android BluetoothProfile 连接到蓝牙健康设备论坛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆