首页 文章

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

提问于
浏览
12

我想知道是否有办法从Linux命令行设置gatt服务器 . 我知道BlueZ gatttool命令允许你充当gatt客户端并询问远程gatt服务器,但是,我不认为这个工具可以用来设置服务器 .

我想要实现的是一个gatt服务器,并且可以被任何中央设备(例如iOS或Android设备)查询以连接到GATT服务器,发现服务和特征,以及操纵特征中的数据 .

例:

Gatt Server具有1个服务,包含3个特征 .

  • 服务uuid = 0xFFFF

  • 字符1 uuid = 0xAAAA,值= 01,属性=可读

  • 字符2 uuid = 0xBBBB,值= 00,属性=可读写

  • Char 3 uuid = 0xCCCC,value = 02,properties = notifiable

我使用的是内核版本3.11.0和BlueZ 5.19

3 回答

  • 2

    所以现在使用新的 bluetoothctl 工具处理 . 可以使用此工具设置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
    

    我已经尝试了一些服务/特性组合,并能够让它工作 . GAP(0x1800)和GATT(0x1801)服务默认可用,并且在您做广告时将成为GATT表的一部分 . 您还可以使用以下命令查看可用服务: -

    [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
    
  • 1

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

    使用以下命令:

    通过以下命令设置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
    

    ·现在通过以下命令通告LE数据包:

    sudo hciconfig hcix leadv
    
  • 0

    我相信不可能从CLI设置GattServer . 主要是因为它是一个上层功能,因此没有可用的工具(因为大多数工具提供较低层功能) .

    但你可以使用模仿bluez使用dbus创建服务的方式 .

    我们需要一个具有两个特征(R,W,N)的GattService

    我们最终做的是 - 1.使用libgdbus(来自bluez源)它有所有的dbus包装器来向bluez注册服务 .

    • 创建了一个转换器(套接字IPC)来分离许可问题(GPL)

    • 向服务注册商发送命令以创建服务e,g - op_code = create_service,uuid ='service_uuid'

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

    希望这可以帮助 .

相关问题