bluetooth_page.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. package Bluetooth
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "strings"
  7. //"errors"
  8. "github.com/fatih/structs"
  9. gotime "time"
  10. "github.com/veandco/go-sdl2/ttf"
  11. "github.com/cuu/gogame/color"
  12. "github.com/cuu/gogame/draw"
  13. "github.com/cuu/gogame/event"
  14. "github.com/cuu/gogame/font"
  15. "github.com/cuu/gogame/rect"
  16. "github.com/cuu/gogame/surface"
  17. "github.com/cuu/gogame/time"
  18. //"github.com/godbus/dbus"
  19. bleapi "github.com/muka/go-bluetooth/api"
  20. //"github.com/muka/go-bluetooth/bluez"
  21. // "github.com/muka/go-bluetooth/bluez/profile"
  22. "github.com/muka/go-bluetooth/bluez/profile/adapter"
  23. "github.com/muka/go-bluetooth/bluez/profile/device"
  24. "github.com/clockworkpi/LauncherGoDev/sysgo/UI"
  25. logrus "github.com/sirupsen/logrus"
  26. )
  27. func showDeviceInfo(dev *device.Device1) {
  28. if dev == nil {
  29. return
  30. }
  31. props, err := dev.GetProperties()
  32. if err != nil {
  33. fmt.Printf("%s: Failed to get properties: %s\n", dev.Path, err.Error())
  34. return
  35. }
  36. fmt.Printf("name=%s addr=%s rssi=%d\n", props.Name, props.Address, props.RSSI)
  37. }
  38. type BleForgetConfirmPage struct {
  39. UI.ConfirmPage
  40. }
  41. func NewBleForgetConfirmPage() *BleForgetConfirmPage {
  42. p := &BleForgetConfirmPage{}
  43. p.ListFont = UI.Fonts["veramono20"]
  44. p.FootMsg = [5]string{"Nav", "", "", "Cancel", "Yes"}
  45. p.ConfirmText = "Confirm Forget?"
  46. p.ConfirmPage.ConfirmText = p.ConfirmText
  47. return p
  48. }
  49. func (self *BleForgetConfirmPage) KeyDown(ev *event.Event) {
  50. if ev.Data["Key"] == UI.CurKeys["A"] || ev.Data["Key"] == UI.CurKeys["Menu"] {
  51. self.ReturnToUpLevelPage()
  52. self.Screen.Refresh()
  53. }
  54. if ev.Data["Key"] == UI.CurKeys["B"] {
  55. self.SnapMsg("Deleting")
  56. self.Screen.Refresh()
  57. time.BlockDelay(400)
  58. self.ReturnToUpLevelPage()
  59. self.Screen.Refresh()
  60. }
  61. }
  62. func (self *BleForgetConfirmPage) Draw() {
  63. self.DrawBG()
  64. for _, v := range self.MyList {
  65. v.Draw()
  66. }
  67. }
  68. type BleInfoPageSelector struct {
  69. UI.InfoPageSelector
  70. }
  71. func NewBleInfoPageSelector() *BleInfoPageSelector {
  72. p := &BleInfoPageSelector{}
  73. p.Width = UI.Width
  74. p.BackgroundColor = &color.Color{131, 199, 219, 255} //SkinManager().GiveColor('Front')
  75. return p
  76. }
  77. func (self *BleInfoPageSelector) Draw() {
  78. idx := self.Parent.GetPsIndex()
  79. mylist := self.Parent.GetMyList()
  80. if idx < len(mylist) {
  81. _, y := mylist[idx].Coord()
  82. _, h := mylist[idx].Size()
  83. x := self.PosX + 2
  84. self.PosY = y + 1
  85. self.Height = h - 3
  86. canvas_ := self.Parent.GetCanvasHWND()
  87. rect_ := rect.Rect(x, self.PosY, self.Width-4, self.Height)
  88. draw.AARoundRect(canvas_, &rect_, self.BackgroundColor, 4, 0, self.BackgroundColor)
  89. }
  90. }
  91. type BleInfoPage struct {
  92. UI.Page
  93. ListFontObj *ttf.Font
  94. ListSmFontObj *ttf.Font
  95. ListSm2FontObj *ttf.Font
  96. AList map[string]interface{}
  97. Scroller *UI.ListScroller
  98. ConfirmPage1 *BleForgetConfirmPage
  99. MyDevice *device.Device1 // from NetItem-> from BluetoothPage
  100. Props *device.Device1Properties
  101. Path string
  102. }
  103. func NewBleInfoPage() *BleInfoPage {
  104. p := &BleInfoPage{}
  105. p.FootMsg = [5]string{"Nav", "Disconnect", "Forget", "Back", ""}
  106. p.ListFontObj = UI.Fonts["varela15"]
  107. p.ListSmFontObj = UI.Fonts["varela12"]
  108. p.ListSm2FontObj = UI.Fonts["varela11"]
  109. return p
  110. }
  111. func (self *BleInfoPage) Init() {
  112. if self.Screen != nil {
  113. if self.Screen.CanvasHWND != nil && self.CanvasHWND == nil {
  114. self.CanvasHWND = self.Screen.CanvasHWND
  115. }
  116. }
  117. self.PosX = self.Index * self.Screen.Width
  118. self.Width = self.Screen.Width // equals to screen width
  119. self.Height = self.Screen.Height
  120. ps := NewBleInfoPageSelector()
  121. ps.Parent = self
  122. self.Ps = ps
  123. self.PsIndex = 0
  124. self.Scroller = UI.NewListScroller()
  125. self.Scroller.Parent = self
  126. self.Scroller.PosX = 2
  127. self.Scroller.PosY = 2
  128. self.Scroller.Init()
  129. self.ConfirmPage1 = NewBleForgetConfirmPage()
  130. self.ConfirmPage1.Screen = self.Screen
  131. self.ConfirmPage1.Name = "Confirm Forget"
  132. self.ConfirmPage1.Init()
  133. }
  134. func (self *BleInfoPage) GenList() {
  135. self.AList = structs.Map(self.Props) //map[string]interface{}
  136. self.MyList = nil
  137. self.PsIndex = 0
  138. start_x := 0
  139. start_y := 0
  140. i := 0
  141. skip_arrays := []string{"ManufacturerData", "AdvertisingFlags", "ServiceData"}
  142. for k, v := range self.AList {
  143. skip2 := false
  144. for _, u := range skip_arrays {
  145. if strings.HasPrefix(k, u) {
  146. skip2 = true
  147. break
  148. }
  149. }
  150. if skip2 {
  151. continue
  152. }
  153. li := UI.NewInfoPageListItem()
  154. li.Parent = self
  155. li.PosX = start_x
  156. li.PosY = start_y + i*NetItemDefaultHeight
  157. li.Width = UI.Width
  158. li.Fonts["normal"] = self.ListFontObj
  159. if k == "UUIDs" {
  160. li.Fonts["small"] = self.ListSm2FontObj
  161. } else {
  162. li.Fonts["small"] = self.ListSmFontObj
  163. }
  164. li.Init(k)
  165. li.Flag = k
  166. sm_text := ""
  167. if k == "UUIDs" {
  168. if len(v.([]string)) > 1 {
  169. sm_text = v.([]string)[0]
  170. } else {
  171. sm_text = "<empty>"
  172. }
  173. } else {
  174. sm_text = fmt.Sprintf("%v", v)
  175. }
  176. if len(sm_text) > 20 {
  177. sm_text = sm_text[:20]
  178. }
  179. li.SetSmallText(sm_text)
  180. li.PosX = 2
  181. self.MyList = append(self.MyList, li)
  182. i += 1
  183. }
  184. }
  185. func (self *BleInfoPage) ScrollUp() {
  186. if len(self.MyList) == 0 {
  187. return
  188. }
  189. self.PsIndex -= 1
  190. if self.PsIndex < 0 {
  191. self.PsIndex = 0
  192. }
  193. cur_li := self.MyList[self.PsIndex]
  194. x, y := cur_li.Coord()
  195. if y < 0 {
  196. for i, v := range self.MyList {
  197. x, y = v.Coord()
  198. _, h := v.Size()
  199. self.MyList[i].NewCoord(x, y+h)
  200. }
  201. }
  202. }
  203. func (self *BleInfoPage) ScrollDown() {
  204. if len(self.MyList) == 0 {
  205. return
  206. }
  207. self.PsIndex += 1
  208. if self.PsIndex >= len(self.MyList) {
  209. self.PsIndex = len(self.MyList) - 1
  210. }
  211. cur_li := self.MyList[self.PsIndex]
  212. x, y := cur_li.Coord()
  213. _, h := cur_li.Size()
  214. if y+h > self.Height {
  215. for i, v := range self.MyList {
  216. x, y = v.Coord()
  217. _, h = v.Size()
  218. self.MyList[i].NewCoord(x, y-h)
  219. }
  220. }
  221. }
  222. func (self *BleInfoPage) TryToForget() {
  223. //muka Adapter1 RemoveDevice Path
  224. adapter, err := bleapi.GetAdapter(adapterID)
  225. if err == nil {
  226. self.Screen.MsgBox.SetText("Forgeting")
  227. self.Screen.MsgBox.Draw()
  228. self.Screen.SwapAndShow()
  229. err = adapter.RemoveDevice(self.MyDevice.Path())
  230. if err != nil {
  231. fmt.Println("BleInfoPage TryToForget: ", err)
  232. }
  233. time.BlockDelay(400)
  234. self.ReturnToUpLevelPage()
  235. self.Screen.Refresh()
  236. } else {
  237. fmt.Println("BleInfoPage TryToForget GetAdapter: ", err)
  238. }
  239. }
  240. func (self *BleInfoPage) TryToDisconnect() {
  241. is_connected, _ := self.MyDevice.GetConnected()
  242. if is_connected {
  243. self.Screen.FootBar.UpdateNavText("Disconnecting")
  244. self.Screen.ShowMsg("Disconnecting",0)
  245. self.MyDevice.Disconnect()
  246. time.BlockDelay(350)
  247. self.ReturnToUpLevelPage()
  248. self.Screen.Refresh()
  249. self.Screen.FootBar.ResetNavText()
  250. }
  251. }
  252. func (self *BleInfoPage) Click() {
  253. if self.PsIndex >= len(self.MyList) {
  254. return
  255. }
  256. cur_li := self.MyList[self.PsIndex]
  257. fmt.Println(cur_li.(*UI.InfoPageListItem).Flag)
  258. }
  259. func (self *BleInfoPage) OnLoadCb() {
  260. if self.Props.Connected == true {
  261. self.FootMsg[1] = "Disconnect"
  262. } else {
  263. self.FootMsg[1] = ""
  264. }
  265. self.GenList()
  266. }
  267. func (self *BleInfoPage) KeyDown(ev *event.Event) {
  268. if ev.Data["Key"] == UI.CurKeys["A"] || ev.Data["Key"] == UI.CurKeys["Menu"] {
  269. self.ReturnToUpLevelPage()
  270. self.Screen.Refresh()
  271. }
  272. if ev.Data["Key"] == UI.CurKeys["Up"] {
  273. self.ScrollUp()
  274. self.Screen.Refresh()
  275. }
  276. if ev.Data["Key"] == UI.CurKeys["Down"] {
  277. self.ScrollDown()
  278. self.Screen.Refresh()
  279. }
  280. if ev.Data["Key"] == UI.CurKeys["Enter"] {
  281. self.Click()
  282. }
  283. if ev.Data["Key"] == UI.CurKeys["X"] {
  284. self.TryToDisconnect()
  285. }
  286. if ev.Data["Key"] == UI.CurKeys["Y"] {
  287. self.TryToForget()
  288. }
  289. }
  290. func (self *BleInfoPage) Draw() {
  291. if len(self.MyList) == 0 {
  292. return
  293. }
  294. self.ClearCanvas()
  295. if len(self.MyList)*UI.DefaultInfoPageListItemHeight > self.Height {
  296. self.Ps.(*BleInfoPageSelector).Width = self.Width - 10
  297. self.Ps.(*BleInfoPageSelector).PosX = 9
  298. self.Ps.Draw()
  299. for _, v := range self.MyList {
  300. v.Draw()
  301. }
  302. self.Scroller.UpdateSize(len(self.MyList)*UI.DefaultInfoPageListItemHeight,
  303. self.PsIndex*UI.DefaultInfoPageListItemHeight)
  304. self.Scroller.Draw()
  305. } else {
  306. self.Ps.(*BleInfoPageSelector).Width = self.Width
  307. self.Ps.Draw()
  308. for _, v := range self.MyList {
  309. v.Draw()
  310. }
  311. }
  312. }
  313. type BleListMessageBox struct {
  314. UI.Label
  315. Parent UI.PageInterface
  316. }
  317. func NewBleListMessageBox() *BleListMessageBox {
  318. p := &BleListMessageBox{}
  319. p.Color = &color.Color{83, 83, 83, 255}
  320. return p
  321. }
  322. func (self *BleListMessageBox) Draw() {
  323. my_text := font.Render(self.FontObj, self.Text, true, self.Color, nil)
  324. w := surface.GetWidth(my_text)
  325. h := surface.GetHeight(my_text)
  326. pw, ph := self.Parent.Size()
  327. x := (pw - w) / 2
  328. y := (ph - h) / 2
  329. padding := 10
  330. rect_ := rect.Rect(x-padding, y-padding, w+padding*2, h+padding*2)
  331. draw.Rect(self.CanvasHWND, &color.Color{255, 255, 255, 255}, &rect_, 0)
  332. draw.Rect(self.CanvasHWND, &color.Color{0, 0, 0, 255}, &rect_, 1)
  333. rect2_ := rect.Rect(x, y, w, h)
  334. surface.Blit(self.CanvasHWND, my_text, &rect2_, nil)
  335. my_text.Free()
  336. }
  337. type BluetoothPage struct {
  338. UI.Page
  339. Devices []*device.Device1
  340. BlePassword string
  341. Connecting bool
  342. Scanning bool
  343. ListFontObj *ttf.Font
  344. Scroller *UI.ListScroller
  345. InfoPage *BleInfoPage
  346. PrevState int
  347. ShowingMessageBox bool
  348. MsgBox *BleListMessageBox
  349. ConnectTry int
  350. //BlockCb ??
  351. LastStatusMsg string
  352. ADAPTER_DEV string // == adapterID
  353. Offline bool
  354. Leader *BluetoothPlugin
  355. }
  356. func NewBluetoothPage() *BluetoothPage {
  357. p := &BluetoothPage{}
  358. p.PageIconMargin = 20
  359. p.SelectedIconTopOffset = 20
  360. p.EasingDur = 10
  361. p.Align = UI.ALIGN["SLeft"]
  362. p.ADAPTER_DEV = adapterID
  363. p.FootMsg = [5]string{"Nav", "Scan", "Info", "Back", "TryConnect"}
  364. p.ListFontObj = UI.Fonts["notosanscjk15"]
  365. return p
  366. }
  367. func (self *BluetoothPage) ShowBox(msg string) {
  368. self.MsgBox.Text = msg
  369. self.ShowingMessageBox = true
  370. self.Screen.ShowMsg(msg,0)
  371. }
  372. func (self *BluetoothPage) HideBox() {
  373. self.Draw()
  374. self.ShowingMessageBox = false
  375. self.Screen.SwapAndShow()
  376. }
  377. func (self *BluetoothPage) Init() {
  378. self.PosX = self.Index * self.Screen.Width
  379. self.Width = self.Screen.Width
  380. self.Height = self.Screen.Height
  381. self.CanvasHWND = self.Screen.CanvasHWND
  382. ps := NewBleInfoPageSelector()
  383. ps.Parent = self
  384. ps.Width = UI.Width - 12
  385. self.Ps = ps
  386. self.PsIndex = 0
  387. msgbox := NewBleListMessageBox()
  388. msgbox.CanvasHWND = self.CanvasHWND
  389. msgbox.Init(" ", UI.Fonts["veramono12"], nil)
  390. msgbox.Parent = self
  391. self.MsgBox = msgbox
  392. self.Scroller = UI.NewListScroller()
  393. self.Scroller.Parent = self
  394. self.Scroller.PosX = 2
  395. self.Scroller.PosY = 2
  396. self.Scroller.Init()
  397. self.InfoPage = NewBleInfoPage()
  398. self.InfoPage.Screen = self.Screen
  399. self.InfoPage.Name = "BluetoothInfo"
  400. self.InfoPage.Init()
  401. }
  402. func (self *BluetoothPage) AbortedAndReturnToUpLevel() {
  403. self.HideBox()
  404. self.Screen.FootBar.ResetNavText()
  405. self.ReturnToUpLevelPage()
  406. self.Screen.Refresh()
  407. }
  408. func (self *BluetoothPage) TryConnect() {
  409. if self.PsIndex >= len(self.MyList) {
  410. return
  411. }
  412. cur_li := self.MyList[self.PsIndex]
  413. if cur_li.(*NetItem).Props.Connected {
  414. return
  415. }
  416. self.Screen.FootBar.UpdateNavText("Connecting")
  417. self.ShowBox("Connecting")
  418. self.Leader.PairPage.DevObj = cur_li.(*NetItem).Device
  419. err := cur_li.(*NetItem).Device.Pair()
  420. if err != nil {
  421. fmt.Println(err)
  422. err_msg := ""
  423. s := err.Error()
  424. err_msg = "Pair error"
  425. if strings.Contains(s, "ConnectionAttemptFailed") {
  426. err_msg = "Page Timeout"
  427. }
  428. if strings.Contains(s, "NoReply") {
  429. err_msg = "NoReply,Cancelling"
  430. dev1 := cur_li.(*NetItem).Device
  431. dev1.CancelPairing()
  432. }
  433. if strings.Contains(s, "Exists") {
  434. err_msg = "Already Exists"
  435. adapter, err := bleapi.GetAdapter(adapterID)
  436. if err == nil {
  437. err = adapter.RemoveDevice(cur_li.(*NetItem).Device.Path())
  438. if err != nil {
  439. fmt.Println(err)
  440. }
  441. } else {
  442. fmt.Println(err)
  443. }
  444. }
  445. self.Leader.PairPage.PairErrorCb(err_msg)
  446. self.Leader.PairPage.DevObj = nil
  447. } else {
  448. self.Leader.PairPage.PairOKCb()
  449. dev1 := cur_li.(*NetItem).Device
  450. err = dev1.SetTrusted(true)
  451. if err != nil {
  452. fmt.Println(err)
  453. }
  454. cur_li.(*NetItem).Device.Connect()
  455. }
  456. self.HideBox()
  457. self.Screen.FootBar.ResetNavText()
  458. }
  459. //GetDevices returns a list of bluetooth discovered Devices
  460. func (self *BluetoothPage) GetDevices() ([]*device.Device1, error) {
  461. adapter, err := bleapi.GetAdapter(adapterID)
  462. if err != nil {
  463. return nil, err
  464. }
  465. list, err := adapter.GetDevices()
  466. return list, err
  467. }
  468. func (self *BluetoothPage) RefreshDevices() {
  469. // sync the cached devices
  470. self.Devices = self.Devices[:0]
  471. devices, err := self.GetDevices()
  472. if err != nil {
  473. panic(err)
  474. os.Exit(1)
  475. }
  476. self.Devices = devices
  477. }
  478. func (self *BluetoothPage) GenNetworkList() {
  479. self.MyList = nil
  480. start_x := 0
  481. start_y := 0
  482. for i, v := range self.Devices { // v == bleapi.Device
  483. props, err := v.GetProperties()
  484. if err != nil {
  485. log.Printf("%s: Failed to get properties: %s\n", v.Path, err.Error())
  486. return
  487. }
  488. ni := NewNetItem()
  489. ni.Parent = self
  490. ni.PosX = start_x
  491. ni.PosY = start_y + i*NetItemDefaultHeight
  492. ni.Width = UI.Width
  493. ni.FontObj = self.ListFontObj
  494. ni.Props = props
  495. ni.Parent = self
  496. ni.Device = v
  497. if props.Name != "" {
  498. ni.Init(props.Name)
  499. } else {
  500. ni.Init(props.Address)
  501. }
  502. self.MyList = append(self.MyList, ni)
  503. }
  504. self.PsIndex = 0
  505. }
  506. func (self *BluetoothPage) Rescan() {
  507. if self.Scanning == true {
  508. self.ShowBox("Bluetooth scanning")
  509. self.Screen.FootBar.UpdateNavText("Scanning")
  510. }
  511. a, err := adapter.GetAdapter(adapterID)
  512. if err != nil {
  513. fmt.Println(err)
  514. return
  515. }
  516. discovery, cancel, err := bleapi.Discover(a, nil)
  517. if err != nil {
  518. fmt.Println(err)
  519. }
  520. defer cancel()
  521. wait := make(chan error)
  522. self.Scanning = true
  523. self.ShowBox("Bluetooth scanning")
  524. self.Screen.FootBar.UpdateNavText("Scanning")
  525. go func() {
  526. for dev := range discovery {
  527. if dev == nil {
  528. return
  529. }
  530. wait <- nil
  531. }
  532. }()
  533. go func() {
  534. sleep := 5
  535. gotime.Sleep(gotime.Duration(sleep) * gotime.Second)
  536. logrus.Debugf("Discovery timeout exceeded (%ds)", sleep)
  537. wait <- nil
  538. }()
  539. err = <-wait
  540. if err != nil {
  541. fmt.Println(err)
  542. }
  543. self.Scanning = false
  544. self.HideBox()
  545. self.Screen.FootBar.ResetNavText()
  546. }
  547. func (self *BluetoothPage) OnLoadCb() {
  548. self.Offline = false
  549. if self.Screen.TitleBar.InAirPlaneMode == false {
  550. out := UI.System("hcitool dev | grep hci0 |cut -f3")
  551. if len(out) < 17 {
  552. self.Offline = true
  553. fmt.Println("Bluetooth OnLoadCb ,can not find hci0 alive,try to reboot")
  554. } else {
  555. self.Rescan()
  556. self.RefreshDevices()
  557. self.GenNetworkList()
  558. }
  559. } else {
  560. self.Offline = true
  561. }
  562. }
  563. func (self *BluetoothPage) ScrollUp() {
  564. if len(self.MyList) == 0 {
  565. return
  566. }
  567. self.PsIndex -= 1
  568. if self.PsIndex < 0 {
  569. self.PsIndex = 0
  570. }
  571. cur_ni := self.MyList[self.PsIndex] //*NetItem
  572. if cur_ni.(*NetItem).PosY < 0 {
  573. for i := 0; i < len(self.MyList); i++ {
  574. self.MyList[i].(*NetItem).PosY += self.MyList[i].(*NetItem).Height
  575. }
  576. }
  577. }
  578. func (self *BluetoothPage) ScrollDown() {
  579. if len(self.MyList) == 0 {
  580. return
  581. }
  582. self.PsIndex += 1
  583. if self.PsIndex >= len(self.MyList) {
  584. self.PsIndex = len(self.MyList) - 1
  585. }
  586. cur_ni := self.MyList[self.PsIndex]
  587. if cur_ni.(*NetItem).PosY+cur_ni.(*NetItem).Height > self.Height {
  588. for i := 0; i < len(self.MyList); i++ {
  589. self.MyList[i].(*NetItem).PosY -= self.MyList[i].(*NetItem).Height
  590. }
  591. }
  592. }
  593. func (self *BluetoothPage) KeyDown(ev *event.Event) {
  594. if ev.Data["Key"] == UI.CurKeys["A"] || ev.Data["Key"] == UI.CurKeys["Menu"] {
  595. if self.Offline == true {
  596. self.AbortedAndReturnToUpLevel()
  597. return
  598. }
  599. a, nil := bleapi.GetAdapter(adapterID)
  600. err := a.StopDiscovery()
  601. if err != nil {
  602. fmt.Println(err)
  603. }
  604. self.HideBox()
  605. self.ReturnToUpLevelPage()
  606. self.Screen.Refresh()
  607. self.Screen.FootBar.ResetNavText()
  608. }
  609. if ev.Data["Key"] == UI.CurKeys["Up"] {
  610. self.ScrollUp()
  611. self.Screen.Refresh()
  612. }
  613. if ev.Data["Key"] == UI.CurKeys["Down"] {
  614. self.ScrollDown()
  615. self.Screen.Refresh()
  616. }
  617. if ev.Data["Key"] == UI.CurKeys["X"] {
  618. if self.Offline == false {
  619. self.Rescan()
  620. }
  621. }
  622. if ev.Data["Key"] == UI.CurKeys["Y"] {
  623. if len(self.MyList) == 0 {
  624. return
  625. }
  626. if self.Offline == true {
  627. return
  628. }
  629. self.InfoPage.Props = self.MyList[self.PsIndex].(*NetItem).Props
  630. self.InfoPage.Path = self.MyList[self.PsIndex].(*NetItem).Path
  631. self.InfoPage.MyDevice = self.MyList[self.PsIndex].(*NetItem).Device
  632. self.Screen.PushPage(self.InfoPage)
  633. self.Screen.Refresh()
  634. }
  635. if ev.Data["Key"] == UI.CurKeys["B"] {
  636. if self.Offline == false {
  637. self.TryConnect()
  638. }
  639. }
  640. }
  641. func (self *BluetoothPage) Draw() {
  642. self.ClearCanvas()
  643. if len(self.MyList) == 0 {
  644. return
  645. }
  646. if len(self.MyList)*NetItemDefaultHeight > self.Height {
  647. self.Ps.(*BleInfoPageSelector).Width = self.Width - 11
  648. self.Ps.Draw()
  649. for _, v := range self.MyList {
  650. v.Draw()
  651. }
  652. self.Scroller.UpdateSize(len(self.MyList)*NetItemDefaultHeight, self.PsIndex*NetItemDefaultHeight)
  653. self.Scroller.Draw()
  654. } else {
  655. self.Ps.(*BleInfoPageSelector).Width = self.Width
  656. self.Ps.Draw()
  657. for _, v := range self.MyList {
  658. v.Draw()
  659. }
  660. }
  661. }