当前位置: 首页 > 知识库问答 >
问题:

在ICS中实现FindMe配置文件

邹誉
2023-03-14

我正在android上开发蓝牙应用程序。我正在ICS上编写一个FindMe服务器应用程序。我们在研究低能时,已将Gatt方法纳入ICS。我目前没有FindMe配置文件API,因此尝试使用GATT方法来完成应用程序。我的应用程序正在向GATT注册,它也在进行服务发现,但在进行服务发现时,蓝牙设备突然与我的手机断开连接。你能告诉我哪里出了问题吗??

我已经在这里复制了代码。。。。所有代码都来自一个文件

public class FindMEService extends Service {

private static final String TAG = "FindMEService";

public static final int MSG_REG_GATT_SERVER_CONFIG = 300;
public static final int MSG_UNREG_GATT_SERVER_CONFIG = 301;

public static final int MSG_REG_GATT_SERVER_SUCCESS = 400;
public static final int MSG_REG_GATT_SERVER_FAILURE = 401;
public static final int MSG_UNREG_GATT_SERVER_SUCCESS = 500;
public static final int MSG_UNREG_GATT_SERVER_FAILURE = 501;

BluetoothGatt gattProfile;
private BluetoothGattAppConfiguration serverConfiguration = null;

InputStream raw = null;

public static ArrayList<Attribute> FMPHandleToAttributes;

public static int serverMinHandle = 0;

public static int serverMaxHandle = -1;

public static HashMap<String, List<Integer>> AttribTypeToHandle =
    new HashMap<String, List<Integer>>();

final Messenger mMessenger = new Messenger(new handler());


@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

public void onCreate() {
    super.onCreate();
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if(mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        stopSelf();
        return;
    }

    if (!mBluetoothAdapter.getProfileProxy(this, mBluetoothServiceListener,
            BluetoothProfile.GATT)) {
                stopSelf();
                return;
            }

    populateFMPAttribTypeMap();
    ReadXML readxml= new ReadXML();
            raw = getResources().openRawResource(R.raw.fmpservice);

           if (raw != null) {
              readxml.parse(raw);        
           }

           sendMessage(MSG_REG_GATT_SERVER_CONFIG,0);
       }

public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent,flags, startId);
    return START_STICKY;
}

public void onDestroy() {
    super.onDestroy();
    sendMessage(MSG_UNREG_GATT_SERVER_CONFIG,0);
}

 private final BluetoothProfile.ServiceListener mBluetoothServiceListener =
     new BluetoothProfile.ServiceListener() {
 public void onServiceConnected(int profile, BluetoothProfile proxy) {
     if (profile == BluetoothProfile.GATT) {
         gattProfile = (BluetoothGatt) proxy;
     }
 }

 public void onServiceDisconnected(int profile) {
     if (profile == BluetoothProfile.GATT) {
         gattProfile = null;
     }
 }
};

private class handler extends Handler {

    public void handleMessage(Message msg) {
        switch(msg.what) {
        case MSG_REG_GATT_SERVER_CONFIG :
            registertoGATT();
            break;
        case MSG_UNREG_GATT_SERVER_CONFIG:
            unregistertoGATT();
            break;
        case MSG_REG_GATT_SERVER_SUCCESS:
                break;
        case MSG_REG_GATT_SERVER_FAILURE:
                break;
        case MSG_UNREG_GATT_SERVER_SUCCESS:
                break;
        case MSG_UNREG_GATT_SERVER_FAILURE:
                break;
        }
    }


}

/**
 * Sending Messages to the Handler
 * @param what
 * @param value
 */
private void sendMessage(int what, int value) {

    if (mMessenger == null) {
        return;
    }

    try {
            mMessenger.send(Message.obtain(null, what, value, 0));
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

/**
 * Register to GATT
 */
private void registertoGATT() {
         gattProfile.registerServerConfiguration("FMP",0xffff,bluetoothGattCallBack));
}

 private void populateFMPAttribTypeMap() {
        AttribTypeToHandle.put("00002800-0000-1000-8000-00805F9B34FB", new ArrayList<Integer>());
        AttribTypeToHandle.put("00002803-0000-1000-8000-00805F9B34FB", new ArrayList<Integer>());
 }


    /**
     * Callback to handle application registration, unregistration events and other
     * API requests coming from the client device.
    */
    private final BluetoothGattCallback bluetoothGattCallBack = new BluetoothGattCallback() {
        public void onGattAppConfigurationStatusChange(BluetoothGattAppConfiguration config,
                int status) {
            serverConfiguration = config;

            switch(status) {
                case BluetoothGatt.GATT_CONFIG_REGISTRATION_SUCCESS:
                        sendMessage(MSG_REG_GATT_SERVER_SUCCESS, 0);
                        break;
                case BluetoothGatt.GATT_CONFIG_REGISTRATION_FAILURE:
                        sendMessage(MSG_REG_GATT_SERVER_FAILURE, 0);
                        break;
                case BluetoothGatt.GATT_CONFIG_UNREGISTRATION_SUCCESS:
                        sendMessage(MSG_UNREG_GATT_SERVER_SUCCESS, 0);
                        break;
                case BluetoothGatt.GATT_CONFIG_UNREGISTRATION_FAILURE:
                        sendMessage(MSG_UNREG_GATT_SERVER_FAILURE, 0);
                        break;
            }
        }

        public void onGattActionComplete(String action, int status) {
            Log.d(TAG, "FindMEService :  onGattActionComplete: " + action + "Status: " + status);
        }


        /**
         * Processes the Discover Primary Services Request from client and sends the response
         * to the client.
        */
        public void onGattDiscoverPrimaryServiceRequest(BluetoothGattAppConfiguration config,
                        int startHandle, int endHandle, int requestHandle) {
            System.out.println("FindMEService :  onGattDiscoverPrimaryServiceRequest");


        }


        /**
         * Processes the Discover Primary Services by UUID Request from client and sends the
         * response to the client.
        */
        public void onGattDiscoverPrimaryServiceByUuidRequest(BluetoothGattAppConfiguration config,
                        int startHandle, int endHandle, ParcelUuid uuid, int requestHandle) {
             int j, k, hdlFoundStatus =0;
             int startAttrHdl = 0, endAttrHdl = 0;
             int status = BluetoothGatt.ATT_ECODE_ATTR_NOT_FOUND;
             boolean retVal;
             List<Integer> hndlList = null;
             if(AttribTypeToHandle != null) {
                 for(Map.Entry<String, List<Integer>> entry : AttribTypeToHandle.entrySet()) {
                     if("00002800-0000-1000-8000-00805F9B34FB".
                                 equalsIgnoreCase(entry.getKey().toString())) {
                         //List of primary service handles
                         hndlList = entry.getValue();
                     }
                 }
             }
             if(hndlList != null) {
                 for(j=0; j< hndlList.size(); j++) {
                     int handle = hndlList.get(j);
                     if(handle >= 0) {
                         if((handle >= startHandle) && (handle <= endHandle)){
                             if(FMPHandleToAttributes != null) {
                                 for(k=0; k<FMPHandleToAttributes.size(); k++) {
                                     if(handle ==FMPHandleToAttributes.get(k).handle) {
                                         Attribute attr = FMPHandleToAttributes.get(k);
                                         startAttrHdl = attr.startHandle;
                                         endAttrHdl = attr.endHandle;
                                         if(attr.uuid != null &&
                                                         attr.uuid.equalsIgnoreCase(uuid.toString())) {
                                             Log.d(TAG, "Primary Handle with UUID available ::");
                                             hdlFoundStatus = 1;
                                             status = BluetoothGatt.GATT_SUCCESS;
                                             break;
                                         }

                                     }
                                 }
                             }
                         }
                     }
                     if(hdlFoundStatus == 1) {
                         Log.d(TAG, "Primary Handle found, success ::");
                         status = BluetoothGatt.GATT_SUCCESS;
                         break;
                     }
                     if(j == (hndlList.size()-1)) {
                         Log.d(TAG, "Primary Handle not found, failure ::");
                         status = BluetoothGatt.ATT_ECODE_ATTR_NOT_FOUND;
                         break;
                     }
                 }
             }
             retVal = gattProfile.discoverPrimaryServiceByUuidResponse(config, requestHandle, status,
                         startAttrHdl, endAttrHdl, uuid);

        }

        /**
         * Processes the Find Included Services Request from client and sends the response
         * to the client.
        */
        public void onGattFindIncludedServiceRequest(BluetoothGattAppConfiguration config,
                        int startHandle, int endHandle, int requestHandle) {
            System.out.println("FindMEService :  onGattFindIncludedServiceRequest");
        }

        /**
         * Processes the Discover Characteristic Descriptors Request from client and sends the
         * response to the client.
        */
        public void onGattDiscoverCharacteristicDescriptorRequest(BluetoothGattAppConfiguration
                        config, int startHandle, int endHandle, int requestHandle) {
            System.out.println("FindMEService :  onGattDiscoverCharacteristicDescriptorRequest");

        }

        /**
         * Processes the Discover Characteristics Request from client and sends the response
         * to the client.
        */
        public void onGattDiscoverCharacteristicRequest(BluetoothGattAppConfiguration config,
                        int startHandle, int endHandle, int requestHandle) {
            System.out.println("FindMEService :  onGattDiscoverCharacteristicRequest");
              }

        /**
         * Processes the Read By Attribute Type Request from client and sends the response
         * to the client.
        */
        public void onGattReadByTypeRequest(BluetoothGattAppConfiguration config, ParcelUuid uuid,
                int startHandle, int endHandle, String authentication, int requestHandle) {
            System.out.println("FindMEService :  onGattReadByTypeRequest");
        }

        /**
         * Processes the Read Request from client and sends the response
         * to the client.
        */
        public void onGattReadRequest(BluetoothGattAppConfiguration config, int handle,
                        String authentication, int requestHandle) {
            System.out.println("FindMEService :  onGattReadRequest");

        }

        /**
         * Processes the Write Request from client and sends the response
         * to the client.
        */
        public void onGattReliableWriteRequest(BluetoothGattAppConfiguration config, int handle,
                        byte value[], String authentication, int sessionHandle,
                        int requestHandle) {
            System.out.println("FindMEService :  onGattReliableWriteRequest");

        }

        /**
         * Processes the Write Request from client and sends the response
         * to the client.
        */
        public void onGattWriteRequest(BluetoothGattAppConfiguration config, int handle,
                        byte value[], String authentication) {
            System.out.println("FindMEService :  onGattWriteRequest");

        }

        public void onGattSetClientConfigDescriptor(BluetoothGattAppConfiguration config,
                        int handle, byte[] value, int sessionHandle) {
            System.out.println("FindMEService :  onGattSetClientConfigDescriptor");
        }


    };

 // Unregister Gatt server application through Bluetooth Gatt API.
    private void unregistertoGATT() {
        Log.d(TAG, "FindMEService :  Unregister Server config called::");
        gattProfile.unregisterServerConfiguration(serverConfiguration);
    }

}

共有1个答案

薛寒
2023-03-14

你能找到根本原因吗?因为你还没有发布,所以我假设可能是你没有。从共享的源代码中,很明显,您的应用程序使用了来自高通、博通和CSR等许多供应商之一的BT-低能耗堆栈。通过实际获取该场景的Air Sniffed日志,了解断开连接的真正原因将很有帮助。您的FindMeService在我看来是正确的。然而,我个人希望看到空气嗅探日志。

谢谢,干杯!!

 类似资料:
  • Seata 可以支持多个第三方配置中心,那么 Seata 是如何同时兼容那么多个配置中心的呢?下面我给大家详细介绍下 Seata 配置中心的实现原理。 配置中心属性加载 在 Seata 配置中心,有两个默认的配置文件: file.conf 是默认的配置属性,registry.conf 主要存储第三方注册中心与配置中心的信息,主要有两大块: registry { # file 、nacos 、e

  • 这个问题是关于以可移植的方式读取REST服务中的配置的正确方法,例如应该运行在Thornail 2.4.0和Wildfly 15上。 这是索恩泰尔建议的原始实现 这在WildFly 15中不起作用,因此我们用以下方式更改了此代码: 只要设置了系统属性,它就能很好地工作。 然而,回到Thorntail,它会生成以下异常: org.jboss.weld.exceptions.部署异常:WELD-001

  • 本文向大家介绍C#为配置文件加密的实现方法,包括了C#为配置文件加密的实现方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了C#为配置文件加密的实现方法,分享给大家供大家参考。具体实现方法如下: 一般来说,在web.config或app.config文件里我们经常会存储一些敏感信息,比如connectionStrings或者appSettings,比如像下面的文件。 一、加密文件可以使用

  • 本文向大家介绍Python3实现配置文件差异对比脚本,包括了Python3实现配置文件差异对比脚本的使用技巧和注意事项,需要的朋友参考一下 应用场景:配置文件由于升级改动了,我们想看看升级后的配置文件相对于之前的改动了哪些配置项 注意:这个脚本只能检测的配置文件是键值对的形式,就是key=value的形式 我在网上找了好久没找到这一块的案例,大部分都是用一些difflib库做的可视化对比,所以自己

  • 本文向大家介绍Android Studio 配置忽略文件的方法实现,包括了Android Studio 配置忽略文件的方法实现的使用技巧和注意事项,需要的朋友参考一下 简介 当我们在进行上传代码到Git、SVN仓库时,通常需要先配置忽略文件,这样主要是方便上传的代码下载的时候不会与编译器和Gradle的版本发生冲突,能够保证下载的代码能正常运行。 操作步骤 打开Android Studio中的Fi

  • 问题内容: 我正在尝试使用Maven 3 在Spring Boot应用程序中设置活动配置文件。在我的pom.xml中,将默认的活动配置文件和属性spring.profiles.active设置 为development: 但是每次我运行应用程序时,都会在日志中收到以下消息: 并且将SpringBoot配置文件设置为默认值(读取application.properties而不是application