edison 上执行SPP-loopback.py
root@edison:~/my_python# python SPP-loopback.py &
#!/usr/bin/python
from __future__ import absolute_import, print_function, unicode_literals
from optparse import OptionParser, make_option
import os
import sys
import socket
import uuid
import dbus
import dbus.service
import dbus.mainloop.glib
try:
from gi.repository import GObject
except ImportError:
import gobject as GObject
class Profile(dbus.service.Object):
fd = -1
@dbus.service.method("org.bluez.Profile1",
in_signature="", out_signature="")
def Release(self):
print("Release")
mainloop.quit()
@dbus.service.method("org.bluez.Profile1",
in_signature="", out_signature="")
def Cancel(self):
print("Cancel")
@dbus.service.method("org.bluez.Profile1",
in_signature="oha{sv}", out_signature="")
def NewConnection(self, path, fd, properties):
self.fd = fd.take()
print("NewConnection(%s, %d)" % (path, self.fd))
server_sock = socket.fromfd(self.fd, socket.AF_UNIX, socket.SOCK_STREAM)
server_sock.setblocking(1)
server_sock.send("This is Edison SPP loopback test\nAll data will be loopback\nPlease start:\n")
try:
while True:
data = server_sock.recv(1024)
print("received: %s" % data)
server_sock.send("looping back: %s\n" % data)
except IOError:
pass
server_sock.close()
print("all done")
@dbus.service.method("org.bluez.Profile1",
in_signature="o", out_signature="")
def RequestDisconnection(self, path):
print("RequestDisconnection(%s)" % (path))
if (self.fd > 0):
os.close(self.fd)
self.fd = -1
if __name__ == '__main__':
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
manager = dbus.Interface(bus.get_object("org.bluez",
"/org/bluez"), "org.bluez.ProfileManager1")
option_list = [
make_option("-C", "--channel", action="store",
type="int", dest="channel",
default=None),
]
parser = OptionParser(option_list=option_list)
(options, args) = parser.parse_args()
options.uuid = "1101"
options.psm = "3"
options.role = "server"
options.name = "Edison SPP Loopback"
options.service = "spp char loopback"
options.path = "/foo/bar/profile"
options.auto_connect = False
options.record = ""
profile = Profile(bus, options.path)
mainloop = GObject.MainLoop()
opts = {
"AutoConnect" : options.auto_connect,
}
if (options.name):
opts["Name"] = options.name
if (options.role):
opts["Role"] = options.role
if (options.psm is not None):
opts["PSM"] = dbus.UInt16(options.psm)
if (options.channel is not None):
opts["Channel"] = dbus.UInt16(options.channel)
if (options.record):
opts["ServiceRecord"] = options.record
if (options.service):
opts["Service"] = options.service
if not options.uuid:
options.uuid = str(uuid.uuid4())
manager.RegisterProfile(options.path, options.uuid, opts)
mainloop.run()
手机上安装蓝牙串口 App
终端
root@edison:~/my_python# rfkill unblock bluetooth
root@edison:~/my_python# bluetoothctl
[bluetooth]# show
Controller 98:4F:EE:04:2E:7D
Name: edison
Alias: edison
Class: 0x2c0110
Powered: yes
Discoverable: no
Pairable: yes
UUID: PnP Information (00001200-0000-1000-8000-00805f9b34fb)
UUID: Generic Access Profile (00001800-0000-1000-8000-00805f9b34fb)
UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control (0000110e-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
UUID: Handsfree (0000111e-0000-1000-8000-00805f9b34fb)
UUID: Serial Port (00001101-0000-1000-8000-00805f9b34fb)
UUID: Audio Source (0000110a-0000-1000-8000-00805f9b34fb)
UUID: Audio Sink (0000110b-0000-1000-8000-00805f9b34fb)
Modalias: usb:v1D6Bp0246d0518
Discovering: no
手机蓝牙打开开放检测
终端
[bluetooth]# scan on
Discovery started
[CHG] Controller 98:4F:EE:04:2E:7D Discovering: yes
[NEW] Device 74:51:BA:6D:40:4C MI4找到手机蓝牙
[bluetooth]# scan off
[CHG] Device 74:51:BA:6D:40:4C RSSI is nil
[CHG] Controller 98:4F:EE:04:2E:7D Discovering: no设置配对方式
[bluetooth]# agent DisplayYesNo
Agent registered
[bluetooth]# default-agent
Default agent request successful[bluetooth]# pair 74:51:BA:6D:40:4C
Attempting to pair with 74:51:BA:6D:40:4C
[CHG] Device 74:51:BA:6D:40:4C Connected: yes
Request confirmation
[agent] Confirm passkey 769356 (yes/no): yes
[CHG] Device 74:51:BA:6D:40:4C Modalias: bluetooth:v001Dp1200d1436
[CHG] Device 74:51:BA:6D:40:4C UUIDs:
00001105-0000-1000-8000-00805f9b34fb
0000110a-0000-1000-8000-00805f9b34fb
0000110c-0000-1000-8000-00805f9b34fb
0000110e-0000-1000-8000-00805f9b34fb
00001112-0000-1000-8000-00805f9b34fb
00001115-0000-1000-8000-00805f9b34fb
00001116-0000-1000-8000-00805f9b34fb
0000111f-0000-1000-8000-00805f9b34fb
0000112d-0000-1000-8000-00805f9b34fb
0000112f-0000-1000-8000-00805f9b34fb
00001132-0000-1000-8000-00805f9b34fb
00001200-0000-1000-8000-00805f9b34fb
00001800-0000-1000-8000-00805f9b34fb
00001801-0000-1000-8000-00805f9b34fb
[CHG] Device 74:51:BA:6D:40:4C Paired: yes
Pairing successful
[CHG] Device 74:51:BA:6D:40:4C Connected: no[bluetooth]# trust 74:51:BA:6D:40:4C
[CHG] Device 74:51:BA:6D:40:4C Trusted: yes
Changing 74:51:BA:6D:40:4C trust succeeded
打开蓝牙串口,连接edison
终端
[bluetooth]# NewConnection(/org/bluez/hci0/dev_74_51_BA_6D_40_4C, 10)