weliem / bluetooth-server-example

Example that shows how to implement a Bluetooth Peripheral on a phone
MIT License
6 stars 3 forks source link

Pairing/Bonding , receiving data from central, latency #1

Open sensn opened 2 years ago

sensn commented 2 years ago

Hello,

I have used this example to create a service that acts as a BLE MIDI peripheral on my phone. The issue is everytime I want to use it I need to pair the device again. I am running Win 10 and I have to remove my peripheral from the devices list and add it again. Then I need to re-enter the pin on the phone. If I disconnect, I have to repeat the process. On the win 10 setting (known) devices list it is shown as paired but not connected. I don't know if this issue is because of Win 10 or because I am missing something in the implemaentation of my service. Is there a way to tell the peripheral to (auto-)connect to the central (Win10) (and invoke Bonding from the peripheral)?

Another question is about recieving data from BLE Midi device on the peripheral device. I started from the HeartRateService example and changed the UUIDs to MIDI service and got it working but I haven't implemented recieving Midi data from the Central. Where do i get the MIDI characteristics when sending from the central to the peripheral? I had a look at BaseService.java, but as I understand the methods " onCharacteristicRead" and " onCharacteristicRead" are callbacks invoked when the peripheral device sends or reads data,right? But where do i get the Midi Message sent from the Central Device to the peripheral?

My last question is about the connection interval. I get a lot of Jitter. In BluetoothServer.java I changed .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED) , to LOW_LATENCY. But I think it might have something to do with the connection interval. Is there a way I can set the connection interval from the peripheral device?

I would be glad if you could help me, thanks for this library and the examples, I tried some others before but Blessed was the only one where i got it to work!

weliem commented 2 years ago

I added more example code to show how you can deal with incoming writes....have a look at the CurrentTimeServiceHandler

Slion commented 1 year ago

The issue is everytime I want to use it I need to pair the device again.

I have the same issue when trying to implement HID over GATT using a patched version of this repo. It only works after you first pair. If ever it disconnects it won't come back online, you need to delete the devices on both Android and Windows 10 for it to work again. I tried a bunch of stuff to fix it like calling BluetoothGattServer.connect but no joy so far.

Had a quick look in blessed and it seems there is some logic to do a scan to get auto connect to work, so I'm considering using it. Is there a client I could use to test that server example, for Windows or Android? All the server does is display Hello World it seems. I can pair it with my PC and here again it has the same issue has the sample mentioned above. During pairing the phone gets one pairing request notification without a pin and then a second one with a pin. That software manages to do a cleaner pairing with a single notification.

@weliem Any tips to help me implement HID over GATT would be welcome. For instance, which tools do you use on Android and Windows to inspect Bluetooth devices and comms.

weliem commented 1 year ago

This repo contains an example app that makes your phone advertise as a peripheral with a HeartRate Service and CurrentTime service. It does not require bonding at all, so I guess it is Windows that is trying to bond. There is also no issue with the connection being dropped when using iOS or Android.

You can check using the app 'nRF Connect'. That allows you to connect and see the services the peripheral provides.

I have no experience with HID over GATT so can't help you there.

Slion commented 1 year ago

Thanks for pointing me to nRF Connect, turns out they also have a BLE library that seems really popular. I guess I'll try my luck with it.

sensn commented 6 months ago

If I run the example on Android 12 or above, the peripheral device doesn't show up on my laptop (blueman-manager). It does show if run on Android 11. Strangely it does work if using two android devices. I am not sure what causes this issue, is it android 12 or my blueman-manager. Is this a known issue?

sensn commented 6 months ago

There seem to be issues with reconnecting in Android 12+.

https://issuetracker.google.com/issues/242755161

Slion commented 6 months ago

There seem to be issues with reconnecting in Android 12+.

https://issuetracker.google.com/issues/242755161

That one is scary. But your issue is with Android 12 when this one is from Android 13.

sensn commented 6 months ago

It's pretty much the same on 12L and 13 ( I am running lineageos 19.1 and 20.1.

Slion commented 6 months ago

I think I have that same issue with my HONOR Magic V2 and my eBike the only way I can reconnect is by redoing the pairing.