无法连接采用Android BluetoothProfile蓝牙保健器佛拉 [英] Unable to connect to bluetooth Health Device Fora using Android BluetoothProfile

查看:269
本文介绍了无法连接采用Android BluetoothProfile蓝牙保健器佛拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Android的BluetoothPRofile连接到佛拉温度计和获得的读数。
以下是我的approoach: -

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


  1. 在OnCreate()中我写了这片code的: -

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


  • 这之后附近的蓝牙设备的code搜索并显示在列表中。
    该列表项的onclicklistener调用下面code: -

  • 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\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 {
    
            }
        }
    }
        };
    


  • 在上述情况下,我得到的BluetoothHealthCallback precisely 2倍: -

    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

    即。在尝试连接到该设备,但它不是succesfull。
    此外,在这两个回调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

    *注:该设备已经配对

    *Note : The device is already paired

    推荐答案

    你觉得你下面的if语句是正确的?

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

    如果(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)

    如果(prevState == BluetoothHealth.STATE_CHANNEL_CONNECTING&放大器;&安培;
                  newState == BluetoothHealth.STATE_CHANNEL_CONNECTED){

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

    当你连接,然后再连接

    或者我可能只是

    如果(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天全站免登陆