层次是
红色是小弟 是在里面干活的 底层
黄色外面2个 或者说1个 是大哥 是封装红色的 是高层
小弟用法1 在weixin
package main
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/muka/go-bluetooth/hw/linux/hci"
log "github.com/sirupsen/logrus"
)
//HciUpDownExample hciconfig up / down
func Run(rawAdapterID string) error {
log.Info("Turn down")
adapterID, err := strconv.Atoi(strings.Replace(rawAdapterID, "hci", "", 1))
if err != nil {
return err
}
err = hci.Down(adapterID)
if err != nil {
return fmt.Errorf("Failed to stop device hci%d: %s", adapterID, err.Error())
}
time.Sleep(time.Second * 5) //´ËʱÖÕ¶ËhciconfigÈ¥¿´hci0µÄ״̬ÏÔʾÊÇdown
log.Info("Turn on")
err = hci.Up(adapterID)
if err != nil {
return fmt.Errorf("Failed to start device hci%d: %s", adapterID, err.Error())
}
log.Info("Done.")
return nil
}
func main() {
Run("hci0")
for {
}
}
如法炮制一个
小弟用法2
package main
import (
"time"
"github.com/muka/go-bluetooth/hw/linux/hciconfig"
log "github.com/sirupsen/logrus"
)
//HciUpDownExample hciconfig up / down
func Run(adapterID string) error {
log.Info("Turn down")
h := hciconfig.NewHCIConfig(adapterID)
h.Down()
time.Sleep(time.Second * 5) //´ËʱÖÕ¶ËhciconfigÈ¥¿´hci0µÄ״̬ÏÔʾÊÇdown
log.Info("Turn on")
h.Up()
log.Info("Done.")
return nil
}
func main() {
Run("hci1")
for {
}
}
他们用的工具不同
一个自己C一样的 用socket去干活
一个是使用linux的命令hciconfig去干活
选择那个方式可以linux.go选择
但是MUKA没有给接口 默认是一个HCICONFIG
用一下大哥吧
每个复位一下
package main
import (
"fmt"
"time"
"github.com/muka/go-bluetooth/hw/linux"
log "github.com/sirupsen/logrus"
)
//HciUpDownExample hciconfig up / down
func Run(adapterID string) error {
log.Info("Turn down")
err := linux.Down(adapterID)
if err != nil {
log.Warnf("Down failed: %s", err)
}
time.Sleep(time.Second)
return linux.Up(adapterID)
}
func main() {
Run("hci1")
Adapters, _ := linux.GetAdapters()
fmt.Println(Adapters)
fmt.Println(len(Adapters))
for i:=0;i
其他的 没有意思 就是一个cmd的拼接
package main
import (
"fmt"
"regexp"
"strings"
"github.com/muka/go-bluetooth/hw/linux/cmd"
)
type HcitoolDev struct {
ID string
Address string
}
// GetAdapter Return an adapter using hcitool as backend
func GetAdapter(adapterID string) (*HcitoolDev, error) {
list, err := GetAdapters()
if err != nil {
return nil, err
}
for _, a := range list {
if a.ID == adapterID {
return a, nil
}
}
return nil, nil
}
// GetAdapters Return a list of adapters using hcitool as backend
func GetAdapters() ([]*HcitoolDev, error) {
list := make([]*HcitoolDev, 0)
raw, err := cmd.Exec("hcitool", "dev")
if err != nil {
return nil, err
}
// raw:
// Devices:
// hci1 70:C9:4E:58:AA:7E
lines := strings.Split(raw, "\n")
lines = lines[1:]
// hci1 70:C9:4E:58:AA:7E
re1 := regexp.MustCompile("^[ \t]*([a-zA-Z0-9]+)[ \t]*([a-zA-Z0-9:]+)$")
for i := 0; i < len(lines); i++ {
if !re1.MatchString(lines[i]) {
continue
}
el := new(HcitoolDev)
res := re1.FindStringSubmatch(lines[i])
if len(res) > 1 {
el.ID = res[1]
el.Address = res[2]
list = append(list, el)
}
}
return list, nil
}
func main() {
list, err := GetAdapters()
if err != nil {
fmt.Println(err)
}
for j := 0; j < len(list); j++ {
fmt.Println(list[j])
}
size := len(list)
devID := fmt.Sprintf("hci%d", (size + 1))
fmt.Println(devID)
a, err := GetAdapter(devID)
if err != nil {
fmt.Println(err)
}
fmt.Println(a)
if a != nil {
fmt.Println("%s should not be avail", devID)
}
for {
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)