I'm developing a frame exchange sequence between an nRF52840 and an Android smartphone using the BLE protocol.
The service exposed by nRF52840 has the following 16-bit UUID : EB7A
In my Android application, if I only want to retrieve BLE devices that have this service, I need to initialize the filter like this :
private var scanFilters: List<ScanFilter> = arrayListOf(
ScanFilter.Builder().setServiceUuid(ParcelUuid(UUID.fromString("0000eb7a-0000-1000-8000-00805f9b34fb"))
)
However, if I want to write about a feature of this service, I have to use the following UUID:
0000eb7a-0000-0000-0000-000000000000
I don't understand why I can't use the same UUID for filtering as for read/write operations, can you help me?
I don't know whether you have figured it out. If yes, would be interesting to know what you have discovered. I am quite new to android and nRF dev myself, but I have 2 theories for your question:
If you are using a vendor-specific UUID for the filter, I guess you are not able to modify it, which is why you have to use a different UUID.
Have you tried deriving the UUID as a String for example and then using it in a filter (not sure if this would work thou)
public static String SOME_SERVICE = "0000eb7a-0000-1000-8000-00805f9b34fb"
ScanFilter = newScanFilter.Builder().setServiceUuid(UUID.fromString(SOME_SERVICE);
Related
For weeks I was struggling to create an app that connected to an arduino device over Bluetooth Low Energy (Bluetooth LE) and I finally got it working today. I am sharing the resources I found so that if someone else is struggling they can learn from my experience. The information I am sharing is for bluetooth LE only. If your device is using bluetooth classic this will not work as they are not compatible.
For anyone who is new to this here is some terms you will need to know. You need to use them in your program to communicate with bluetooth LE.
UART: universal asynchronous receiver-transmitter. This is the protocol that is used to send data to a device via bluetooth LE and to receive data.
GATT: Generic Attribute Profile. This defines how data will be transferred back and forth using Services and Characteristics.
This was an excellent tutorial on how to create the app:
https://www.jenx.si/2020/08/13/bluetooth-low-energy-uart-service-with-xamarin-forms/
This app will scan for BLE devices, connect to them, send data to the device, and receive data from the device. My app is for android, but it looks like the app from this tutorial will work for IOS as well.
Once I created the app I searched for the UART UUID's of my device (an Adafruit Feather Bluefruit LE). Here is the page where I found the codes for my device:
https://learn.adafruit.com/adafruit-feather-m0-bluefruit-le/uart-service
I found the Send and Receive UUID's from the Nordic Semiconductor website:
https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v14.0.0%2Fble_sdk_app_nus_eval.html
Then in the GattIdentifiers page I replaced the generic UUID's that were entered with the UUID's for my device. Here is what my GattIdentifiers page looks like with my UUID's.
public class GattIdentifiers
{
public static Guid UartGattServiceId = Guid.Parse("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
public static Guid UartGattCharacteristicReceiveId = Guid.Parse("6E400003-B5A3-F393-E0A9-E50E24DCCA9E");
public static Guid UartGattCharacteristicSendId = Guid.Parse("6E400002-B5A3-F393-E0A9-E50E24DCCA9E");
public static Guid SpecialNotificationDescriptorId = Guid.Parse("00002902-0000-1000-8000-00805f9b34fb");
}
A note on this is to make sure you put the right UUID's with the send and receive characteristics defined in the file. I had them reversed at first which was giving me "null" results. So if they are not working try reversing them.
I hope this helps!
First, the source code for the BLE Peripheral Simulator is a tremendous resource for anyone investigating Web Bluetooth.
There is a an approved list of GATT Services.
When I modified the Android source code to use a custom defined GATT service (easy to do by simply specifying your own UUID in the Android code) pairing from the web page to the Android app running the GATT sever fails.
The Web Bluetooth documentation does state, "If device’s advertised Service UUIDs have a non-empty intersection with the set of Service UUIDs, add device to result and abort these sub-steps [for pairing]."
Does this mean only services in the approved GATT list are supported? If so, what is the rational? It seems that such a restriction would limit innovation.
I am working with web-bluetooth also and i was able to talk with custom services, yet to read data from them you have to have device docs explaining how to talk to that services. I think this approved list is like generally used list which doesn't require unique logic to get data. However there is difference since some characteristics are blacklisted for web-bluetooth yet available to fetch data from using ios/android stuff.
If I had done some more research on Stack Overflow I would have realized the UUID spec requires lowercase a-f.
Type of Character generated by UUID
The following values work for a custom defined service and characteristic:
private static final UUID SERVICE_UUID = UUID.fromString("29143321-ef6c-4761-947c-c858f9a2e8f1");
private static final UUID CHARACTERISTIC_UUID = UUID.fromString("92f3131b-ffa8-4dd1-a12b-641d65a78857");
I have a problem with using my own generated UUID(not using 16 bit UUID reserved by Bluetooth SIG) to communicate in Android BLE.
Target device is two - one is as a peripheral(Samsung galaxy note 4/android 5.1.1) and another one is as a central(Samsung galaxy S5/android 5.0.1). I put my own service's UUID and characteristic's UUID on the peripheral side. On the central side, it could not find characteristic but service.
It seems like it caused by not using one of these Characteristic UUIDs but using custom generated UUID.
Below is my own generated UUIDs.
private static final UUID CUSTOM_SERVICE_UUID = UUID.fromString("abcd1111-0000-1000-8000-00805f9b34fb");
private static final UUID CUSTOM_CHARACTERISTIC_UUID = UUID.fromString("abcd1112-0000-1000-8000-00805f9b34fb");
Service, found well, but characteristic, no.
Do you have any idea?
you are using the official base UUID: xxxxxxxx-0000-1000-8000-00805f9b34fb, from which all upper 32-bit are reserved (some already assigned.)
Instead, you should use your own 128-bit UUID. You can use an online tool to generate one. Then just assign your own internal n-bit short uuid to each one of your characteristic.
For example: using base => 18902a9a-1f4a-44fe-936f-14c8eea418xx
Service => 18902a9a-1f4a-44fe-936f-14c8eea41800
Characteristic => 18902a9a-1f4a-44fe-936f-14c8eea41801
My understanding is that the SDP is a list of UUIDs that other devices can fetch.
According to this PDF from MIT, "A more general way to think of
SDP is as an information database." Does this mean I can add multiple values to SDP? Since Android has BluetoothDevice.fetchUuidsWithSdp(), how do I set the UUIDs of a device?
Also, what does each section of an UUID mean? UUIDs look like 00000000-0000-1000-8000-00805F9B34FB, but what information does this convey?
An UUID identifies a service that is available on a particular device. So if you call BluetoothDevice.fetchUUidsWithSdp() your BroadcastReceiver will receive the relevant Intent ACTION_UUID containing the device and the service UUID.
The bluetooth specification defines some common UUIDs.
If you don't want to connect to one of these well known services but intent to implement your own bluetooth application, then you have to just generate your own UUID (use uuidgen from a unix console or an online generator) that identifies your application/service.
You can create an UUID instance in java like this UUID uuid = UUID.fromString("785da8ea-1220-11e5-9493-1697f925ec7b");.
So if you create the server side for your bluetooth application on Android you typically do this
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
BluetoothServerSocket serverSocket = adapter.listenUsingRfcommWithServiceRecord("YourHumanReadableServiceName", uuid);
And this is where you "set" your UUID. The Android bluetooth API creates the SDP-entry consisting of YOUR application's UUID and name for you. Other devices can now retrieve this entry. Androids bluetooth stack will now associate a bluetooth channel to your BluetoothServerSocket. If you want to connect to this ServerSocket, the connecting side usually connects doing this:
// you will most likely already have this instance from a discovery or paired device list
BluetoothDevice serverDevice = adapter.getRemoteDevice(bluetoothMacAddress);
// connect to your ServerSocket using the uuid
BluetoothSocket socket = serverDevice.createRfcommSocketToServiceRecord(uuid);
socket.connect();
Android will again do the heavy lifting for you: It checks the SDP-Records on the remote device, looks up the bluetooth channel that corresponds to your service's UUID and connects using this information.
There is a common code snippet spooking around here on SO that advices you to use "reflection" to get to a hidden API looking similar to this code:
try {
// this is the way to go
socket = device.createRfcommSocketToServiceRecord(uuid);
socket.connect( );
} catch ( IOException exception ) {
// don't do that! You will bypass SDP and things will go sideways.
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(device, 1);
socket.connect();
}
Most people try this and it "just works" in their dev environment but you should know what you do using this. You actively bypass the SDP lookup that retrieves the right bluetooth channel to be used with your service and you will end up connecting to channel 1. If you have more than one Service running on the device, things WILL go sideways in this cases and you will end up in debugging hell ;-)
I developed a small middleware called Blaubot to create small networks using bluetooth/wifi/nfc and experienced all sorts of problems on the devices I used to test with (12 models). It was often the case that the bluetooth stack was not fully functional anymore in cases where it got some load or after many connects/disconnects (which you usually will have, if you are developing your app). In these cases the device.createRfcommSocketToServiceRecord(uuid) would occasionally fail and only turning the bluetooth adapter off and on again helped to bring the bluetooth adapters back to life (in some cases only after a full power cycle). If this happens and you use the reflection method, you will probably not have much fun with bluetooth.
But if you know this and keep concurrent calls to the BluetoothAdapter within bounds, bluetooth connections and the adapters will be pretty stable.
I'm starting to work on Bluetooth in Android recently.
I want to build an application that can read the data recorded by a sensor through bluetooth.
I have some sample code, but looks like I need another UUID of a different device.It looks like this:
private static final UUID MY_UUID = UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB");
Later on in the code, it uses this UUID to make a connection:
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
I have done some research online, like [here][1]. I think I need a different UUID number for the new device I'm working on. How do I get the UUID number?
On the device, there are two lines of number saying:
SN: 1201L0023
BT: 10:00:E8:C5:16:85
Thanks in advance!
Jake
UUIDs are not tied to particular devices. They identify software services. Some UUIDs for defined profiles are set by BT. The UUIDs used with RFCOMM sockets like your example are arbitrary. You just need both sides to use the same one. In general, devices connect and then use service discovery protocol to find out what services (UUIDs) are supported on the remote device.