Use BlueZ Stack As A Peripheral (Advertiser)

前端 未结 2 1992
感情败类
感情败类 2020-11-27 09:54
  • Goal: Use BlueZ and an Bluetooth 4LE dongle to create a peripheral that advertises the bluetooth equivalent of \"Hello World\".

相关标签:
2条回答
  • 2020-11-27 10:32

    adding some more information here (ref BlueZ5.x):

    • bluez is not exposing DBUS api for gatt server registeration; you have some profile implementation available but you need to configure and compile it with --enable-experimental option or enable gatt-example under plugin folder tha tregister some gatt server as example

    • when you advertise doing hciconfig hci0 leadv pay attention to advertised data. in the example below for iBeacon

      sudo hcitool -i hci0 cmd 0x08 0x0008 1e **02 01 1a** ff 4c 00 02 15 e2 c5 6d b5 df fb 48 d2 b0 60 d0 f5 a7 10 96 e0 00 00 00 00 c5 00 00 00 00 00 00 00 00 00 00 00 00 00

      the bolded part is for an AD structure that is releated to flags field (check under bluetooth spec for advertisgin data format). With this flags settings the device is advertising itself as simultaneous LE and BR-EDR.

      In my experience when advertising in this way an android device that scan the bluez device acting as BLE peripheral will trigger a connection over Classic bluetooth (due to flag settings) and not over BLE (note that connection procedures are different for LE and Classic). Not sure how Apple central device act in this case. Anyway to avoid it you may set 06 instead of 1A so that BR-EDR support isnot advertised. In this way you will see a connection over BLE.

    If you want to test it using a central Android device there is a free Nordic app for that.

    0 讨论(0)
  • 2020-11-27 10:55

    With your Bluetooth dongle plugged in, running the following command will tell you the device name and give its state:

    $ hciconfig

    The output should look something like this:

    hci0:    Type: BR/EDR  Bus: USB
         BD Address: 00:01:02:aa:bb:cc  ACL MTU: 1021:8  SCO MTU: 64:1
         DOWN
         RX bytes:1000 acl:0 sco:0 events:47 errors:0
         TX bytes:1072 acl:0 sco:0 commands:47 errors:0
    

    This indicates the device is called hci0 is in a down state. Issue the following command to bring it up:

    $ sudo hciconfig hci0 up

    Now it should look like:

    $ hciconfig
    hci0:   Type: BR/EDR  Bus: USB
         BD Address: 00:01:02:aa:bb:cc  ACL MTU: 1021:8  SCO MTU: 64:1
         UP RUNNING
         RX bytes:1000 acl:0 sco:0 events:47 errors:0
         TX bytes:1072 acl:0 sco:0 commands:47 errors:0
    

    Next, execute the following example command to configure the advertising data to be sent.

    $ sudo hcitool -i hci0 cmd 0x08 0x0008 1e 02 01 1a 1a ff 4c 00 02 15 e2 c5 6d b5 df fb 48 d2 b0 60 d0 f5 a7 10 96 e0 00 00 00 00 c5 00 00 00 00 00 00 00 00 00 00 00 00 00
    

    You can change the hex bytes (starting with 1e) to send different byte sequences for your advertisement. One that literally sends the ASCII codes for "HELLO WORLD" would use: 48 45 4c 4c 4f 57 4f 52 4c 44 (EDIT: But you will also have to prefix this message with a valid header, see here.)

    Now, use the following command to activate advertising on the dongle, this will start sending "Helo World" packets.

    $ sudo hciconfig hci0 leadv 0

    EDIT: the above command makes the advertised service connectable. If you don't want to allow connections, change it to $ sudo hciconfig hci0 leadv 3

    You can also disable advertising using the following command:

    $ sudo hciconfig hci0 noleadv

    0 讨论(0)
提交回复
热议问题