0

我正在开发 android 中的蓝牙应用程序。我正在 ICS 上编写 FindMe Server 应用程序。我们在 ICS 中加入了 Gatt 方法,因为我们正在研究低能量。我目前没有 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);
    }

}

4

1 回答 1

0

你能找到根本原因吗。由于您尚未发布,所以我假设您可能没有发布。从共享源代码中,很明显您的应用程序使用来自众多供应商之一的 BT-Low-energy 堆栈,例如 qualcomm、broadcomm 和 CSR。通过实际获取场景的 Air Sniffed 日志,有助于您了解断开连接的真正原因。您的 FindMeService 对我来说看起来是正确的。但是我个人希望看到 Air Sniff 日志。

谢谢和干杯!

于 2012-07-19T03:38:58.437 回答