BlueZ: How to set up a GATT server from the comman

2020-01-29 06:04发布

I would like to know if there is a way to set up a gatt server from the Linux command line. I know that the BlueZ gatttool command allows you to act as a gatt client and interrogate a remote gatt server, however, I do not think that this tool can be used to set up a server.

What I want to achieve is a gatt server, created from the command line, and can be interrogated by any central device (e.g. iOS or Android device) to connect to the GATT server, discover the services and characteristics, and manipulate the data in the characteristics.

Example:

Gatt Server with 1 service which contains 3 characteristics.

  • Service uuid = 0xFFFF
  • Char 1 uuid = 0xAAAA, value = 01, properties = readable
  • Char 2 uuid = 0xBBBB, value = 00, properties = readable & writable
  • Char 3 uuid = 0xCCCC, value = 02, properties = notifiable

I am using kernel version 3.11.0 and BlueZ 5.19

3条回答
倾城 Initia
2楼-- · 2020-01-29 06:26

I believe it is not possible to setup GattServer from CLI. Mainly because it is a upper layer functionality so there is no tool available to do it (as most of the tools provide lower layer functionalities).

But you can use mimic the way bluez creates service using dbus.

We needed a GattService with two characteristics (R,W,N)

What we ended up doing was following - 1. use the libgdbus (from bluez source) It has all the dbus wrapper to register services to bluez.

  1. Created a translator (socket IPC) to separate the licensing issue (GPL)

  2. Send command to the service registrar to create a service e,g - op_code = create_service, uuid = 'service_uuid'

op_code = create_charac, uuid='charac_uuid' flags='rwn'

Hope this helps.

查看更多
三岁会撩人
3楼-- · 2020-01-29 06:34

So this is now handled with the new bluetoothctl tool. A gatt table can be set up using this tool as follows:-

#bluetoothctl
[bluetoothctl] menu gatt
[bluetoothctl] register-service 0xFFFF # (Choose yes when asked if primary service)
[bluetoothctl] register-characteristic 0xAAAA read       # (Select a value of 1 when prompted)
[bluetoothctl] register-characteristic 0xBBBB read,write # (Select a value of 0 when prompted)
[bluetoothctl] register-characteristic 0xCCCC read       # (Select a value of 2 when prompted)
[bluetoothctl] register-application # (This commits the services/characteristics and registers the profile)
[bluetoothctl] back
[bluetoothctl] advertise on

I've tried this with a few service/characteristic combinations and was able to get it to work. The GAP (0x1800) and GATT (0x1801) services are available by default and will be part of the GATT table when you advertise. You can also use the following command to see the available services:-

[bluetoothctl] show
Controller 00:AA:BB:CC:DD:EE (public)
    Name: MyMachine
    Alias: MyMachine
    Class: 0x000c0000
    Powered: yes
    Discoverable: no
    Pairable: yes
    UUID: Headset AG                (00001112-0000-1000-8000-00805f9b34fb)
    UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
    UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)
    UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
    UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)
    UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
    UUID: Audio Source              (0000110a-0000-1000-8000-00805f9b34fb)
    UUID: Audio Sink                (0000110b-0000-1000-8000-00805f9b34fb)
    **UUID: Unknown                   (0000ffff-0000-1000-8000-00805f9b34fb)**
    UUID: Headset                   (00001108-0000-1000-8000-00805f9b34fb)
    Modalias: usb:v1D6Bp0246d0532
    Discovering: no
查看更多
The star\"
4楼-- · 2020-01-29 06:40

I have also faced the same issue, but could find any proper solution, what you can best do using a bluez stack on an Ubuntu machine is use some hci commands to advertise LE packets. These packets will be constantly advertised as the this is if it is an LE server, If you go for scan using an GATT Client you will get the name of your bluez device on the scan list.

Use the following commands below:

Set the LE advertisement packets by the following command:

sudo hcitool -i hcix cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 00 00 00 00 C8 00

· Now advertise the LE packets by the following command:

sudo hciconfig hcix leadv
查看更多
登录 后发表回答