rohitsangwan01 / ble_peripheral

A Flutter plugin that allows you to use your device as Bluetooth Low Energy (BLE) peripheral
MIT License
12 stars 13 forks source link

BLE GATT Server ask for pairing #13

Open GurgenHOVH opened 1 month ago

GurgenHOVH commented 1 month ago

When trying to connect to the server hosted on an Android device, it opens a pairing request on both server and client.

The service and advertising looks like this:

 await BlePeripheral.addService(
        BleService(
          uuid: serviceTicketing,
          primary: true,
          characteristics: [
            BleCharacteristic(
              uuid: characteristicTicketing,
              properties: [
                CharacteristicProperties.write.index,
              ],
              value: null,
              permissions: [AttributePermissions.writeable.index],
            ),
          ],
        ),
      );

      /// set callback for advertising state
      BlePeripheral.setAdvertisingStatusUpdateCallback(
          (bool advertising, String? error) {
        print("AdvertisingStatus: $advertising Error $error");
      });

      BlePeripheral.setWriteRequestCallback(
          (deviceId, characteristicId, offset, value) {
        print(
            "WriteRequest: $deviceId ${String.fromCharCodes(value?.toList() ?? [])} : $offset : $value ");

        final userId = String.fromCharCodes(value?.toList() ?? []);

        if (userId.isNotEmpty) {
          addUserId(userId);
        }

        return null;
      });

      // Start advertising
      await BlePeripheral.startAdvertising(
        services: [serviceTicketing],
        localName: "ticketing",
      );
rohitsangwan01 commented 1 month ago

@GurgenHOVH yes android will try to pair if any device will try to connect, if you think your use case does not requires pairing on android, you can try to replace this code, and check if it still works for you without bonding

BluetoothProfile.STATE_CONNECTED -> {
                            handler?.post {
                                gattServer?.connect(device, true)
                            }
                            synchronized(bluetoothDevicesMap) {
                                bluetoothDevicesMap.put(
                                    device.address,
                                    device
                                )
                            }
                        onConnectionUpdate(device, status, newState)
                    }
GurgenHOVH commented 1 month ago

Thanks @rohitsangwan01 for the quick reply. Will try and update here tomorrow.

GurgenHOVH commented 1 month ago

Hi @rohitsangwan01 . The code you have provided works. Would be great to turn this into a setting and update the package. Thanks!

rohitsangwan01 commented 1 month ago

@gurgenAya not sure if you are using any encrypted Char which causes Pairing request, or maybe thats the default android behaviour, but now that you have removed the pairing code from the native part, that program should not be responsible for making pair requests, you can cross check with Nrf Connect app maybe, in NrfConnect you can create a GattServer, and then try to see if that also makes any pairing request

rohitsangwan01 commented 1 month ago

Hi @rohitsangwan01 . The code you have provided works. Would be great to turn this into a setting and update the package. Thanks!

Sure, maybe will add this as an optional setting to StartScan in next update