yanshouwang / bluetooth_low_energy

A Flutter plugin for controlling the bluetooth low energy.
https://pub.dev/packages/bluetooth_low_energy
MIT License
35 stars 12 forks source link

BLE Advertisement changes device Bluetooth name "permanently" #81

Open pdalfarr opened 2 weeks ago

pdalfarr commented 2 weeks ago

Hi,

Thanks a lot for this great Flutter library: amazing work here 👍

I tested the sample app on both my Windows PC and my Android device: this is working fine.

I just noticed that my phone's "Bluetooth name" has changed to "BLE-12138" event after I closed the android app.

Looking at the code, I found 3 places where this constant is used:

https://github.com/yanshouwang/bluetooth_low_energy/blob/c9f0e7e5ee4cd27a8c459cc24a577d7d4834b068/bluetooth_low_energy/example/lib/view_models/peripheral_manager_view_model.dart#L137

https://github.com/yanshouwang/bluetooth_low_energy/blob/c9f0e7e5ee4cd27a8c459cc24a577d7d4834b068/bluetooth_low_energy_darwin/example/lib/view_models/peripheral_manager_view_model.dart#L132

https://github.com/yanshouwang/bluetooth_low_energy/blob/c9f0e7e5ee4cd27a8c459cc24a577d7d4834b068/bluetooth_low_energy_android/example/lib/view_models/peripheral_manager_view_model.dart#L135

Would it be possible to preseve and use the current device's "Bluetooth name" ? And, by the way, is it possible to obtain the current "Bluetooth name" of the device?

Thanks

Using bluetooth_low_energy: ^6.0.0 (I'll update to 6.0.1 ASAP)

yanshouwang commented 2 weeks ago

I think this can be improved but need to consider about other platforms.

On Android, if we want to include local name in advertisement, We can only use the bluetoothAdapter's name, the adapter's name is a getter/setter property, in some devices, set the adapter's name seems will change the name in the system settings.

On iOS and macOS, we can set the name without influnce the system settings.

On Windows, the name is unable to set, the system always use the PC's name as the local name in the advertisement.

I think maybe I can provide a getName method to the CentralManager and PeripheralManager API. Then you can set the name from this method.

Maybe there is a better way with this, I need to consider this for a while.

pdalfarr commented 2 weeks ago

Thanks a lot for your reply.

"in some devices, set the adapter's name seems will change the name in the system settings" I think this is what happened to me.

I hope you can find a way to solve this.

Thanks a lot

yanshouwang commented 2 weeks ago

I have added the includeDeviceName argument in PeripheralManager.startAdvertising method,you can use this argument to include the adapter's name without change it by accident. And you can change the adapter's name by using the setName method. Notice that these is only available on Android, and you can't set the name argument in the Advertisemnt anymore.

You can test this feature with the 7.0.0-dev.0 version.

pdalfarr commented 2 weeks ago

Great! I'll give a try ASAP. So, changing the name by accident was only an issue on Android, right? On IOS, the device name is not changed, is that correct? Thanks a lot for this quick fix 🙏

pdalfarr commented 2 weeks ago

Hi,

I just tested on Android and here is the outcome:

Using the sample app, I observe that "Central Manager" does work, but "Peripheral Manager" doesn't 😞

When I tap on the "begin" button, its label does NOT change to "end".

Logcat says this:

BluetoothGattServer     com.android.ble.sample               D  clearServices()
bt_stack                com.android.bluetooth                E  [ERROR:gatt_api.cc(341)] Active Service Found: 00000064-0000-1000-8000-00805f9b34fb
bt_stack                com.android.bluetooth                I  [INFO:gatt_api.cc(409)] GATTS_StopService: service = 0x0086
bt_stack                com.android.bluetooth                I  [INFO:gatt_sr_hash.cc(127)] gatts_calculate_database_hash: hash=8991C462EBE9D90B9EA0DA3E16D0F513
BluetoothGattServer     com.android.ble.sample               D  addService() - service: 00000064-0000-1000-8000-00805f9b34fb
bt_stack                com.android.bluetooth                I  [INFO:bta_gatts_api.cc(129)] bta_gatts_add_service_impl: rcb_idx=0
bt_stack                com.android.bluetooth                I  [INFO:gatt_api.cc(172)] GATTS_AddService
bt_stack                com.android.bluetooth                I  [INFO:gatt_api.cc(287)] GATTS_AddService: service parsed correctly, now starting
bt_stack                com.android.bluetooth                I  [INFO:gatt_sr_hash.cc(127)] gatts_calculate_database_hash: hash=04043D3E4AF8BCE480E98C9DFE870F29
BluetoothGattServer     com.android.ble.sample               D  onServiceAdded() - handle=134 uuid=00000064-0000-1000-8000-00805f9b34fb status=0

So there is an error: E [ERROR:gatt_api.cc(341)] Active Service Found: 00000064-0000-1000-8000-00805f9b34fb

Does this help you?

Pascal

yanshouwang commented 2 weeks ago

Can you provide the logs of the Flutter side?

I can't reproduce this with my Android device.

[MyPeripheralManager] initialize
[MyPeripheralManager] getState
[MyPeripheralManager] onStateChanged: MyBluetoothLowEnergyStateArgs.on
[MyPeripheralManager] openGATTServer
D/BluetoothGattServer(10092): registerCallback()
D/BluetoothGattServer(10092): registerCallback() - UUID=a2e516c7-110f-46cf-ace2-89e5b3188d53
D/BluetoothGattServer(10092): onServerRegistered() - status=0 serverIf=11
D/BluetoothGattServer(10092): clearServices()
D/BluetoothGattServer(10092): addService() - service: 00000064-0000-1000-8000-00805f9b34fb
D/BluetoothGattServer(10092): onServiceAdded() - handle=47 uuid=00000064-0000-1000-8000-00805f9b34fb status=0
[MyPeripheralManager] removeAllServices
[MyPeripheralManager] addService: Instance of 'MyMutableGATTServiceArgs'
2
D/BluetoothAdapter(10092): isLeEnabled(): ON
[MyPeripheralManager] startAdvertising: Instance of 'MyAdvertiseSettingsArgs', Instance of 'MyAdvertiseDataArgs', Instance of 'MyAdvertiseDataArgs'
pdalfarr commented 2 weeks ago

OK, now it works.

The thing is that I first only changed version from 6.0.0 to 7.0.0-dev.0 and nothing else.

Now I have checked your repo and noticed that the sample app code also changed. cfr https://github.com/yanshouwang/bluetooth_low_energy/commit/5cf5b4a5d9812e61f9144eeb75a24bc70df70c29 (changes in _manager.startAdvertising( ... and final advertisement = Advertisement( ...)

The thing is that I already have my own app based on your sample code... from version6.0.0. So updating from 6.0.0 to 7.0.0-dev.0 broke my app because, I did not changes "my sample code"

Maybe this is something to consider in the future. Some code of the sample app should be part of the library itself ? More precisely code with Platform.isAndroid, Platform.windows, etc.??

Thanks

yanshouwang commented 2 weeks ago

I upgraded the version from 6.x to 7.x, because the API have breaking changes. Notice this is a pre release and the API is not stable. When 7.0.0 released. I will add the migration docs. And the platform specific codes is necessary because these APIs are only available on particular platforms

pdalfarr commented 2 weeks ago

On my side, I confirm 7.0.0-dev.0 does fix this issue ;-)

yanshouwang commented 2 weeks ago

On my side, I confirm 7.0.0-dev.0 does fix this issue ;-)

Thanks for the report. You can use the pre release now but notice that the API maybe changed in the next pre release version