Ejemplo n.º 1
0
        public int GetServicePtrPos(ushort uuid16, ref CSR_BLE_DATABASE_DISCOVERY_RESULT dbRes)
        {
            int serviceCount = dbRes.nServices;

            CSR_BLE_SERVICE[] services = new CSR_BLE_SERVICE[serviceCount];
            for (int j = 0; j < serviceCount; j++)
            {
                services[j] =
                    (CSR_BLE_SERVICE)
                    Marshal.PtrToStructure(dbRes.services + j * Marshal.SizeOf(typeof(CSR_BLE_SERVICE)),
                                           typeof(CSR_BLE_SERVICE));
                Debug(string.Format("Service -Uuid:{0} -nCharacters:{1}", services[j].uuid, services[j].nCharacteristics));
            }

            int serviceId = 0;

            while (serviceId < serviceCount)
            {
                if (services[serviceId].uuid.uuid16 == uuid16)
                {
                    return(serviceId);
                }
                serviceId++;
            }
            return(0xFF);
        }
Ejemplo n.º 2
0
        private void OnDatabaseDiscoveryResult(CSR_BLE_DATABASE_DISCOVERY_RESULT database)
        {
            int servicePtrPos = GetServicePtrPos((ushort)_curServiceUuid, ref database);

            if (servicePtrPos != 0xFF)
            {
                CSR_BLE_SERVICE service =
                    (CSR_BLE_SERVICE)Marshal.PtrToStructure(
                        database.services + servicePtrPos * Marshal.SizeOf(typeof(CSR_BLE_SERVICE)),
                        typeof(CSR_BLE_SERVICE));

                //if (_curServiceUuid != XIM_BATTERY_SERVICE)
                {
                    var nCharacter = service.nCharacteristics;
                    CSR_BLE_CHARACTERISTIC readCharcter =
                        (CSR_BLE_CHARACTERISTIC)
                        Marshal.PtrToStructure(service.characteristics, typeof(CSR_BLE_CHARACTERISTIC));

                    CSR_BLE_CHARACTERISTIC writeCharcter =
                        (CSR_BLE_CHARACTERISTIC)
                        Marshal.PtrToStructure(
                            service.characteristics + 1 * Marshal.SizeOf(typeof(CSR_BLE_CHARACTERISTIC)),
                            typeof(CSR_BLE_CHARACTERISTIC));

                    CurDevInfo.ReadHandle  = readCharcter.handle;
                    CurDevInfo.WriteHandle = writeCharcter.handle;

                    if (readCharcter.nDescriptors > 0)
                    {
                        var descHandle =
                            (CSR_BLE_CHARACTERISTIC_DSC)Marshal.PtrToStructure(readCharcter.descriptors,
                                                                               typeof(CSR_BLE_CHARACTERISTIC_DSC));
                        CsrBleDll.CsrBleClientWriteConfiguration(CurDevInfo.ConnHandle, descHandle.handle,
                                                                 readCharcter.handle, 0x01);
                    }
                }
                _dbDiscoverEvent.Set();
            }
        }