我正在尝试创建一个自定义BLE广告和扫描响应。广告是iBeacon。我还希望它能够广播服务的可用性。我不能把服务uid放在广告数据中,因为它太长了(据我了解,Bluegiga的BGScript只允许广告数据中有32个八位字节)。我将服务uid放在扫描响应中,希望这将允许移动设备看到服务,以便搜索并连接到该设备。出于某种原因,我无法让它工作。iOS不再将BLE设备作为iBeacon接收,也无法识别该服务是否可用。我将为服务uid使用哪种AD类型,以及如何格式化该数据(包括服务uid)?
到目前为止我写的是:
广告数据02 01 06 // flags
1a ff 4c 00 02 15 // manufacturer data, apple company id, iBeacon preamble
aa aa aa aa - aa aa - aa aa - aa aa - aa aa aa aa aa aa // iBeacon UUID
00 01 00 01 // iBeacon major, minor
c6 // measured tx power
扫描响应数据
11 // length
16 // ad type (I am not sure what this should be)
a5 b7 67 a0 - 74 9b - 11 e3 - 98 1f - 08 00 20 0c 9a 66 // service UUID
为了更好的上下文,下面是BGScript中关于BLE112的部分:
event system_boot( major, minor, patch, build, ll_version, protocol_version, hw )
call gap_set_adv_parameters( 20, 100, 7 )
call gap_set_mode( gap_general_discoverable, gap_undirected_connectable )
# Flags
data( 0:1) = $02
data( 1:1) = $01
data( 2:1) = $06
# Manufacturer data
data( 3:1) = $1a
data( 4:1) = $ff
# Preamble
data( 5:1) = $4c
data( 6:1) = $00
data( 7:1) = $02
data( 8:1) = $15
# UUID
data( 9:1) = $aa
data(10:1) = $aa
data(11:1) = $aa
data(12:1) = $aa
data(13:1) = $aa
data(14:1) = $aa
data(15:1) = $aa
data(16:1) = $aa
data(17:1) = $aa
data(18:1) = $aa
data(19:1) = $aa
data(20:1) = $aa
data(21:1) = $aa
data(22:1) = $aa
data(23:1) = $aa
data(24:1) = $aa
# Major
data(25:1) = $00
data(26:1) = $01
# Minor
data(27:1) = $00
data(28:1) = $01
# Measured power (specified in 2's complement, so 0xC6 is -58)
data(29:1) = $c6
# Scan Response Data
sr_data( 0:1) = $11
sr_data( 1:1) = $21
# Service Uuid
sr_data( 2:1) = $a5
sr_data( 3:1) = $b7
sr_data( 4:1) = $67
sr_data( 5:1) = $a0
sr_data( 6:1) = $74
sr_data( 7:1) = $9b
sr_data( 8:1) = $11
sr_data( 9:1) = $e3
sr_data(10:1) = $98
sr_data(11:1) = $1f
sr_data(12:1) = $08
sr_data(13:1) = $00
sr_data(14:1) = $20
sr_data(15:1) = $0c
sr_data(16:1) = $9a
sr_data(17:1) = $66
# Set advertisement data
call gap_set_adv_data(0, 30, data(0:30))
call gap_set_adv_data(1, 18, sr_data(0:18))
#set bondable mode
call sm_set_bondable_mode(1)
end
我能够在扫描响应包中广播服务uid。它可以同时与CLLocationManager (iBeacon)和CBCentralManager (BLE)一起工作。我认为还有一些其他的变化,使它的工作,但我不确定他们在哪里。我使用了"128位服务类uuid的不完整列表"AD类型(0x06),并且我发现我将uuid的端序颠倒了。
下面是我使用的扫描响应数据:
# Scan Response Data
sr_data( 0:1) = $11
sr_data( 1:1) = $06
# Service Uuid
sr_data( 2:1) = $66
sr_data( 3:1) = $9a
sr_data( 4:1) = $0c
sr_data( 5:1) = $20
sr_data( 6:1) = $00
sr_data( 7:1) = $08
sr_data( 8:1) = $1f
sr_data( 9:1) = $98
sr_data(10:1) = $e3
sr_data(11:1) = $11
sr_data(12:1) = $9b
sr_data(13:1) = $74
sr_data(14:1) = $a0
sr_data(15:1) = $67
sr_data(16:1) = $b7
sr_data(17:1) = $a5
为什么需要服务UUID ?你应该能够在使用CoreBluetooth扫描后连接到iBeacon。
请看这里的例子。本例旨在证明使用CoreBluetooth读取iBeacon标识符是不可能的。但它也表明,CoreBluetooth api可以让你在扫描后连接到iBeacon,即使你无法读取其标识符。
也可以使用CoreBluetooth在连接完成后查询信标服务