/// <summary> /// A helper method to start the local GATT server. /// </summary> private async Task <bool> StartGattServer() { bool started = false; try { started = await gattServer.StartAsync(); } catch (Exception ex) { Debug.WriteLine("Error starting GATT server with this" + "message: " + ex.Message); } if (!started) { Debug.WriteLine("Could not start the GATT server."); } else { Debug.WriteLine("GATT server started."); } // Get a handle to the local packet write characteristic and its siblings localPacketWriteCharacteristic = gattServer.PacketProcessingService.PacketWriteCharacteristic; localCompressedHeaderLengthCharacteristic = gattServer.PacketProcessingService.CompressedHeaderLengthCharacteristic; localPayloadLengthCharacteristic = gattServer.PacketProcessingService.PayloadLengthCharacteristic; // Subscribe to the characteristic's "packet received" event localPacketWriteCharacteristic.PropertyChanged += WatchForPacketReception; return(started); }
/// <summary> /// Awaits the reception of a packet on the packet write characteristic /// of the packet write service of the local GATT server. /// /// This is so a server can know when a packet has been received and /// deal with it accordingly. /// </summary> /// <param name="sender"></param> /// <param name="eventArgs"></param> private void WatchForPacketReception( IPv6ToBlePacketWriteCharacteristic localPacketWriteCharacteristic, PropertyChangedEventArgs eventArgs ) { if (eventArgs.PropertyName == "Packet") { // Only send it back out if this device is not the destination; // in other words, if this device is a middle router in the // subnet IPAddress destinationAddress = GetDestinationAddressFromPacket( localPacketWriteCharacteristic.Packet ); if (IPAddress.Equals(destinationAddress, localAddress)) { SendPacketOverBluetoothLE(localPacketWriteCharacteristic.Packet, destinationAddress ); } } }
//--------------------------------------------------------------------- // Asynchronous initialization //--------------------------------------------------------------------- public override async Task InitAsync() { await CreateServiceProvider(Constants.IPv6ToBlePacketProcessingServiceUuid); GattLocalCharacteristicResult characteristicResult = null; // // Step 1 // Create the packet write characteristic // GattLocalCharacteristic createdPacketWriteCharacteristic = null; characteristicResult = await ServiceProvider.Service.CreateCharacteristicAsync( Constants.IPv6ToBlePacketWriteCharacteristicUuid, GattHelpers.packetWriteParameters ); // // Step 2 // Assign the created packet write characteristic to this service's internal one // GattHelpers.GetCharacteristicFromResult(characteristicResult, ref createdPacketWriteCharacteristic ); if (createdPacketWriteCharacteristic != null) { PacketWriteCharacteristic = new IPv6ToBlePacketWriteCharacteristic( createdPacketWriteCharacteristic, this ); } characteristicResult = null; // // Step 3 // Create the compressed header length characteristic // GattLocalCharacteristic createdCompressedHeaderLengthCharacteristic = null; characteristicResult = await ServiceProvider.Service.CreateCharacteristicAsync( Constants.IPv6ToBleCompressedHeaderLengthCharacteristicUuid, GattHelpers.packetWriteParameters ); // // Step 4 // Assign the created compressed header length characteristic to this service's internal one // GattHelpers.GetCharacteristicFromResult(characteristicResult, ref createdCompressedHeaderLengthCharacteristic ); if (createdCompressedHeaderLengthCharacteristic != null) { CompressedHeaderLengthCharacteristic = new CompressedHeaderLengthCharacteristic( createdCompressedHeaderLengthCharacteristic, this ); } characteristicResult = null; // // Step 5 // Create the payload length characteristic // GattLocalCharacteristic createdPayloadLengthCharacteristic = null; characteristicResult = await ServiceProvider.Service.CreateCharacteristicAsync( Constants.IPv6ToBlePayloadLengthCharacteristicUuid, GattHelpers.packetWriteParameters ); // // Step 6 // Assign the created payload length characteristic to this service's internal one // GattHelpers.GetCharacteristicFromResult(characteristicResult, ref createdPayloadLengthCharacteristic ); if (createdPayloadLengthCharacteristic != null) { PayloadLengthCharacteristic = new PayloadLengthCharacteristic( createdPayloadLengthCharacteristic, this ); } characteristicResult = null; // // Step 7 // Create the IPv6 address read characteristic // // Generate the device's link-local IPv6 address IPAddress address = await StatelessAddressConfiguration.GenerateLinkLocalAddressFromBlThRadioIdAsync(2); if (address == null) { Debug.WriteLine("Could not generate a link-local IPv6 address" + " from the Bluetooth address." ); return; } // Create the characteristic GattLocalCharacteristic createdIPv6AddressReadCharacteristic = null; characteristicResult = await ServiceProvider.Service.CreateCharacteristicAsync( Constants.IPv6ToBleIPv6AddressCharacteristicUuid, GattHelpers.ipv6AddressReadParameters ); // // Step 8 // Assign the created IPv6 address read characteristic to this service's internal one // GattHelpers.GetCharacteristicFromResult(characteristicResult, ref createdIPv6AddressReadCharacteristic ); if (createdIPv6AddressReadCharacteristic != null) { IPv6AddressCharacteristic = new IPv6ToBleIPv6AddressCharacteristic( createdIPv6AddressReadCharacteristic, this, address ); } }