简体   繁体   English

BlueZ:如何从命令行设置 GATT 服务器

[英]BlueZ: How to set up a GATT server from the command line

I would like to know if there is a way to set up a gatt server from the Linux command line.我想知道是否有办法从 Linux 命令行设置 gatt 服务器。 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.我知道 BlueZ gatttool 命令允许您充当 gatt 客户端并询问远程 gatt 服务器,但是,我认为此工具不能用于设置服务器。

What I want to achieve is a gatt server, created from the command line , and can be interrogated by any central device (eg iOS or Android device) to connect to the GATT server, discover the services and characteristics, and manipulate the data in the characteristics.我想要实现的是一个 gatt 服务器,从命令行创建,可以被任何中央设备(例如 iOS 或 Android 设备)询问以连接到 GATT 服务器,发现服务和特征,并操纵其中的数据特征。

Example:例子:

Gatt Server with 1 service which contains 3 characteristics.具有 1 个服务的 Gatt 服务器,其中包含 3 个特征。

  • Service uuid = 0xFFFF服务 uuid = 0xFFFF
  • Char 1 uuid = 0xAAAA, value = 01, properties = readable字符 1 uuid = 0xAAAA,值 = 01,属性 = 可读
  • Char 2 uuid = 0xBBBB, value = 00, properties = readable & writable字符 2 uuid = 0xBBBB,值 = 00,属性 = 可读可写
  • Char 3 uuid = 0xCCCC, value = 02, properties = notifiable字符 3 uuid = 0xCCCC,值 = 02,属性 = 可通知

I am using kernel version 3.11.0 and BlueZ 5.19我正在使用内核版本 3.11.0 和 BlueZ 5.19

So this is now handled with the new bluetoothctl tool.因此,现在使用新的bluetoothctl工具处理此问题。 A gatt table can be set up using this tool as follows:-可以使用此工具设置 gatt 表,如下所示:-

#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. GAP (0x1800) 和 GATT (0x1801) 服务在默认情况下可用,并且在您发布广告时将成为 GATT 表的一部分。 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

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.我也遇到了同样的问题,但可以找到任何合适的解决方案,在 Ubuntu 机器上使用 bluez 堆栈最好的方法是使用一些 hci 命令来通告 LE 数据包。 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.如果它是 LE 服务器,这些数据包将不断公布,如果您使用 GATT 客户端进行扫描,您将在扫描列表中获得您的 bluez 设备的名称。

Use the following commands below:使用以下命令:

Set the LE advertisement packets by the following command:通过以下命令设置 LE 广告数据包:

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: · 现在通过以下命令通告 LE 数据包:

sudo hciconfig hcix leadv

I believe it is not possible to setup GattServer from CLI.我相信从 CLI 设置 GattServer 是不可能的。 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.但是您可以使用模拟 bluez 使用 dbus 创建服务的方式。

We needed a GattService with two characteristics (R,W,N)我们需要一个具有两个特征(R、W、N)的 GattService

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. 使用 libgdbus(来自 bluez 源)它具有向 bluez 注册服务的所有 dbus 包装器。

  1. Created a translator (socket IPC) to separate the licensing issue (GPL)创建了一个翻译器(socket IPC)来分离许可问题(GPL)

  2. Send command to the service registrar to create a service e,g - op_code = create_service, uuid = 'service_uuid'向服务注册器发送命令以创建服务 e,g - op_code = create_service, uuid = 'service_uuid'

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

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM