func main() { uuid := flag.String("uuid", "1BEAC099-BEAC-BEAC-BEAC-BEAC09BEAC09", "iBeacon UUID") major := flag.Int("major", 0, "iBeacon major value (uint16)") minor := flag.Int("minor", 0, "iBeacon minor value (uint16)") power := flag.Int("power", -57, "iBeacon measured power (int8)") d := flag.Duration("duration", 1*time.Minute, "advertising duration") verbose := flag.Bool("verbose", false, "dump all events") flag.Parse() ble := goble.New() ble.SetVerbose(*verbose) ble.Init() var utsname xpc.Utsname xpc.Uname(&utsname) log.Println(utsname.Release) time.Sleep(1 * time.Second) log.Println("Start Advertising", *uuid, *major, *minor, *power) ble.StartAdvertisingIBeacon(xpc.MustUUID(*uuid), uint16(*major), uint16(*minor), int8(*power)) time.Sleep(*d) log.Println("Stop Advertising") ble.StopAdvertising() }
// start advertising as IBeacon (raw data) func (ble *BLE) StartAdvertisingIBeaconData(data []byte) { var utsname xpc.Utsname xpc.Uname(&utsname) if utsname.Release >= "14." { l := len(data) buf := bytes.NewBuffer([]byte{byte(l + 5), 0xFF, 0x4C, 0x00, 0x02, byte(l)}) buf.Write(data) ble.sendCBMsg(8, xpc.Dict{"kCBAdvDataAppleMfgData": buf.Bytes()}) } else { ble.sendCBMsg(8, xpc.Dict{"kCBAdvDataAppleBeaconKey": data}) } }
// AdvertiseIBeaconData advertises iBeacon packet with specified manufacturer data. func (d *Device) AdvertiseIBeaconData(ctx context.Context, md []byte) error { var utsname xpc.Utsname xpc.Uname(&utsname) if utsname.Release >= "14." { l := len(md) b := []byte{byte(l + 5), 0xFF, 0x4C, 0x00, 0x02, byte(l)} return d.AdvertiseMfgData(ctx, append(b, md...)) } if err := d.sendReq(d.pm, 8, xpc.Dict{"kCBAdvDataAppleBeaconKey": md}).err(); err != nil { return err } <-ctx.Done() return d.stopAdvertising() }