Using paypal/gatt to discover nearby Bluetooth-enabled phone

I’m trying to use paypal/gatt to discover nearby Bluetooth devices. To start, I’m just using their discoverer example. It works well in that I can detect my headphones, but it doesn’t see phones (either iPhone or Nexus 5X). I’m a total newbie to Bluetooth programming. Does anyone know how I’d have to change the example to see phones?

package main

import (
	"fmt"
	"log"

	"github.com/paypal/gatt"
	"github.com/paypal/gatt/examples/option"
)

func onStateChanged(d gatt.Device, s gatt.State) {
	fmt.Println("State:", s)
	switch s {
	case gatt.StatePoweredOn:
		fmt.Println("scanning...")
		d.Scan([]gatt.UUID{}, false)
		return
	default:
		d.StopScanning()
	}
}

func onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
	fmt.Printf("\nPeripheral ID:%s, NAME:(%s)\n", p.ID(), p.Name())
	fmt.Println("  Local Name        =", a.LocalName)
	fmt.Println("  TX Power Level    =", a.TxPowerLevel)
	fmt.Println("  Manufacturer Data =", a.ManufacturerData)
	fmt.Println("  Service Data      =", a.ServiceData)
}

func main() {
	d, err := gatt.NewDevice(option.DefaultClientOptions...)
	if err != nil {
		log.Fatalf("Failed to open device, err: %s\n", err)
		return
	}

	// Register handlers.
	d.Handle(gatt.PeripheralDiscovered(onPeriphDiscovered))
	d.Init(onStateChanged)
	select {}
}

This might sound like a dumb question, but have you made sure to expose your phone to other bluetooth devices? I believe in the newest version of android you just have to go to bluetooth settings and you see a message at the bottom about it being exposed to other devices.

I believe that by default phones are not exposed so it is possible your code is fine but the phone isn’t exposed.

Edit: I’m not sure how to do this on older versions of android of iphones, but if you go to the bluetooth settings it will likely be obvious.

Yeah, thanks for double checking. Definitely in the Bluetooth settings so the phone is discoverable. :slight_smile: Confirmed that it is discoverable by other devices.

1 Like

I can imagine two possible causes:

  • Your computer might have either BT hardware or a BT stack that is incompatible with the gatt packages.

  • The gatt project looks quite outdated and not maintained anymore. (Last commit was made in October 2015, and 30 issues are open and do not look very active).

Maybe it’s both, with the former being a result of the latter.

There seem a couple of other BT packages available - maybe you can find an alternative that works.

2 Likes

I’ll try one of those then. Thanks!

If anyone else does happen to know more about Bluetooth, I’d still welcome that feedback in the meantime. :slight_smile:

For now, I had to give in and use python’s pybluez library, but I’m hurting without Go. So I’m still trying to get this package to work: https://github.com/muka/go-bluetooth - but am just working with the author to help me debug a certain issue…

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.