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); }
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(); } }
private void OnClientDatabaseDiscoveryResult(CSR_BLE_DATABASE_DISCOVERY_RESULT database) { var gattDevice = GattConnectDevices.Find(d => d.Handle == database.connectHandle); if (gattDevice == null || database.result != 0) { Error("[CSR]:Database Discover Error " + gattDevice?.Address); return; } gattDevice.NService = database.nServices; //update GattDevice services and characteristics for (var i = 0; i < database.nServices; i++) { var service = (CSR_BLE_SERVICE) Marshal.PtrToStructure(database.services + i * Marshal.SizeOf(typeof(CSR_BLE_SERVICE)), typeof(CSR_BLE_SERVICE)); Debug($"[CSR]:Service -Uuid:{service.uuid} -nCharacters:{service.nCharacteristics}"); var gattService = new BleGattService { GattDevice = gattDevice, Uuid = service.uuid.uuid16, StartHandle = service.startHandle, EndHandle = service.endHandle, NCharacters = service.nCharacteristics }; gattDevice.AddGattService(gattService); // add gattCharacteristic to gattService for (var j = 0; j < service.nCharacteristics; j++) { var character = (CSR_BLE_CHARACTERISTIC) Marshal.PtrToStructure( service.characteristics + j * Marshal.SizeOf(typeof(CSR_BLE_CHARACTERISTIC)), typeof(CSR_BLE_CHARACTERISTIC)); var gattCharacter = new BleGattCharacteristic { Service = gattService, Uuid = character.uuid.uuid16, Handle = character.handle, DeclarationHandle = character.declHandle, NDescriptors = character.nDescriptors, Properties = character.properties }; if (character.nDescriptors > 0) { var descriptor = (CSR_BLE_CHARACTERISTIC_DSC)Marshal.PtrToStructure(character.descriptors, typeof(CSR_BLE_CHARACTERISTIC_DSC)); gattCharacter.DescriptorHandle = descriptor.handle; } gattService.AddCharacter(gattCharacter); } } _databaseEvent.Set(); }