Created
February 5, 2019 14:01
-
-
Save cuu/a43f0c0c666e4ea61e3dece310803318 to your computer and use it in GitHub Desktop.
simple_agent.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"strings" | |
"github.com/godbus/dbus" | |
// "github.com/godbus/dbus/introspect" | |
"github.com/muka/go-bluetooth/api" | |
"github.com/muka/go-bluetooth/bluez/profile" | |
"github.com/muka/go-bluetooth/linux" | |
// "os" | |
//"encoding/xml" | |
) | |
/* | |
var ErrRejected = dbus.NewError("org.bluez.Error.Rejected", nil) | |
var ErrCanceled = dbus.NewError("org.bluez.Error.Canceled", nil) | |
*/ | |
// ToDo: allow enabling "simple pairing" (sspmode set via hcitool) | |
const ( | |
BUS_NAME = "org.bluez" | |
AGENT_INTERFACE = "org.bluez.Agent1" | |
AGENT_PATH = "/gameshell/bleagentgo" | |
) | |
func set_trusted(path string) { | |
devices ,err := api.GetDevices() | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
for i,v := range devices { | |
fmt.Println(i, v.Path) | |
if strings.Contains(v.Path,path) { | |
fmt.Println("Found device") | |
dev1,_ := v.GetClient() | |
err:=dev1.SetProperty("Trusted",true) | |
if err != nil { | |
fmt.Println(err) | |
} | |
} | |
} | |
} | |
type Agent struct{ | |
BusName string | |
AgentInterface string | |
AgentPath string | |
} | |
func (self *Agent) Release() *dbus.Error { | |
return nil | |
} | |
func (self *Agent) RequestPinCode(device dbus.ObjectPath) (pincode string, err *dbus.Error) { | |
fmt.Println("RequestPinCode",device) | |
set_trusted(string(device)) | |
return "0000",nil | |
} | |
func (self *Agent) DisplayPinCode(device dbus.ObjectPath, pincode string) *dbus.Error { | |
fmt.Println( fmt.Sprintf("DisplayPinCode (%s, %s)" ,device, pincode)) | |
return nil | |
} | |
func (self *Agent) RequestPasskey(device dbus.ObjectPath) (passkey uint32, err *dbus.Error) { | |
set_trusted(string(device)) | |
return 1024,nil | |
} | |
func (self *Agent) DisplayPasskey(device dbus.ObjectPath, passkey uint32, entered uint16) *dbus.Error { | |
fmt.Println(fmt.Sprintf("DisplayPasskey %s, %06u entered %u" ,device, passkey, entered)) | |
return nil | |
} | |
func (self *Agent) RequestConfirmation(device dbus.ObjectPath, passkey uint32) *dbus.Error { | |
fmt.Println(fmt.Sprintf("RequestConfirmation (%s, %06d)", device, passkey)) | |
set_trusted(string(device)) | |
return nil | |
} | |
func (self *Agent) RequestAuthorization(device dbus.ObjectPath) *dbus.Error { | |
fmt.Printf("RequestAuthorization (%s)\n" ,device) | |
return nil | |
} | |
func (self *Agent) AuthorizeService(device dbus.ObjectPath, uuid string) *dbus.Error { | |
fmt.Printf("AuthorizeService (%s, %s)",device, uuid) //directly authrized | |
return nil | |
} | |
func (self *Agent) Cancel() *dbus.Error { | |
fmt.Println("Cancel") | |
return nil | |
} | |
func (self *Agent) RegistrationPath() string { | |
return self.AgentPath | |
} | |
func (self *Agent) InterfacePath() string { | |
return self.AgentInterface | |
} | |
func RegisterAgent(agent profile.Agent1Interface, caps string) (err error) { | |
//agent_path := AgentDefaultRegisterPath // we use the default path | |
agent_path := agent.RegistrationPath() // we use the default path | |
fmt.Println("The Agent Path: ", agent_path) | |
// Register agent | |
am := profile.NewAgentManager1(agent_path) | |
// Export the Go interface to DBus | |
err = am.ExportGoAgentToDBus(agent) | |
if err != nil { return err } | |
// Register the exported interface as application agent via AgenManager API | |
err = am.RegisterAgent(agent_path, caps) | |
if err != nil { return err } | |
// Set the new application agent as Default Agent | |
err = am.RequestDefaultAgent(agent_path) | |
if err != nil { return err } | |
return | |
} | |
const adapterID = "hci0" | |
func InitAgent() { | |
agent := &Agent{} | |
agent.BusName = BUS_NAME | |
agent.AgentInterface = AGENT_INTERFACE | |
agent.AgentPath = AGENT_PATH | |
RegisterAgent(agent, profile.AGENT_CAP_KEYBOARD_DISPLAY) | |
} | |
func main() { | |
defer api.Exit() | |
dev_mac := "34:88:5D:97:FF:26" // Keyboard K480 | |
// dev_mac := "E4:17:D8:AC:08:7A" // 8bit gamepad | |
a := linux.NewBtMgmt(adapterID) | |
err := a.Reset() | |
if err != nil { | |
log.Fatal(err) | |
os.Exit(1) | |
} | |
InitAgent() | |
devices ,err := api.GetDevices() | |
if err != nil { | |
fmt.Println("GetDevices",err) | |
return | |
} | |
for i,v := range devices { | |
dev_mac_path := strings.Replace(dev_mac,":","_",-1) | |
if strings.Contains(v.Path,dev_mac_path) { | |
fmt.Println(i,v.Path) | |
fmt.Println("Pairing...") | |
err := v.Pair() | |
if err == nil { | |
fmt.Println("Pair succeed,Connecting...") | |
set_trusted(dev_mac_path) | |
v.Connect() | |
}else { | |
fmt.Println("Pair failed:",err) | |
} | |
} | |
} | |
fmt.Println("Working...") | |
select {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where should I get
/gameshell/bleagentgo
from?