本文整理匯總了Python中socket.BTPROTO_HCI屬性的典型用法代碼示例。如果您正苦於以下問題:Python socket.BTPROTO_HCI屬性的具體用法?Python socket.BTPROTO_HCI怎麽用?Python socket.BTPROTO_HCI使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類socket
的用法示例。
在下文中一共展示了socket.BTPROTO_HCI屬性的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: open_dev
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import BTPROTO_HCI [as 別名]
def open_dev(bt_device_id):
"""Open hci device socket."""
# pylint: disable=no-member
sock = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI)
# Unlike Linux, FreeBSD has separate numbering depending on hardware
# (ubt - USB bluetooth - is the most common, so convert numbers to that)
if not isinstance(bt_device_id, str):
bt_device_id = 'ubt{}hci'.format(bt_device_id)
# Python's BTPROTO_HCI address parsing is busted: https://bugs.python.org/issue41130
adr = SockaddrHci(ctypes.sizeof(SockaddrHci), socket.AF_BLUETOOTH, bt_device_id.ljust(32, '\0').encode('utf-8'))
if libc.bind(sock.fileno(), ctypes.pointer(adr), ctypes.sizeof(SockaddrHci)) != 0:
raise ConnectionError(ctypes.get_errno(), os.strerror(ctypes.get_errno()))
if libc.connect(sock.fileno(), ctypes.pointer(adr), ctypes.sizeof(SockaddrHci)) != 0:
raise ConnectionError(ctypes.get_errno(), os.strerror(ctypes.get_errno()))
# pylint: enable=no-member
fltr = HciRawFilter(0, NG_HCI_EVENT_MASK_LE)
if libc.setsockopt(sock.fileno(),
SOL_HCI_RAW, SOL_HCI_RAW_FILTER,
ctypes.pointer(fltr), ctypes.sizeof(HciRawFilter)) != 0:
raise ConnectionError(ctypes.get_errno(), os.strerror(ctypes.get_errno()))
return sock
示例2: __init__
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import BTPROTO_HCI [as 別名]
def __init__(self, device_id=0):
self.device_id = device_id
self._keep_running = True
self._socket = None
self._socket_on_data_user_callback = None
self._socket_on_started = None
self._socket_poll_thread = None
self._l2sockets = {}
self._socket = BluetoothSocket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI)
#self._socket = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI)
#self._socket = BluetoothUserSocket()
#self._socket = bluetooth.bluez._gethcisock(0)
self._socket.setblocking(0)
self.__r, self.__w = os.pipe()
self._r = os.fdopen(self.__r, 'rU')
self._w = os.fdopen(self.__w, 'w')
示例3: hci_devinfo
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import BTPROTO_HCI [as 別名]
def hci_devinfo(adapter=0):
# Get Bluetooth device information
# Takes an adapter number, returns raw data from ioctl
# FIXME(MR) this function is a complete hack
# but Python doesn't even dream of offering Bluetooth ioctl's
dd = BluetoothSocket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI)
dev_info = struct.pack('<H', adapter) + "\x00" * 90
try:
r = fcntl.ioctl(dd, 0x800448d3, dev_info) # HCIGETDEVINFO, tested on x64 Linux only
return r
except IOError as e:
# XXX is there a more Pythonic way of doing this?
if e[0] == 19:
raise TypeError("No such Bluetooth adapter")
else:
raise e
示例4: create_bt_socket
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import BTPROTO_HCI [as 別名]
def create_bt_socket(interface=0):
exceptions = []
sock = None
try:
sock = socket.socket(family=socket.AF_BLUETOOTH,
type=socket.SOCK_RAW,
proto=socket.BTPROTO_HCI)
sock.setblocking(False)
sock.setsockopt(socket.SOL_HCI, socket.HCI_FILTER, pack("IIIh2x", 0xffffffff,0xffffffff,0xffffffff,0)) #type mask, event mask, event mask, opcode
try:
sock.bind((interface,))
except OSError as exc:
exc = OSError(
exc.errno, 'error while attempting to bind on '
'interface {!r}: {}'.format(
interface, exc.strerror))
exceptions.append(exc)
except OSError as exc:
if sock is not None:
sock.close()
exceptions.append(exc)
except:
if sock is not None:
sock.close()
raise
if len(exceptions) == 1:
raise exceptions[0]
elif len(exceptions) > 1:
model = str(exceptions[0])
if all(str(exc) == model for exc in exceptions):
raise exceptions[0]
raise OSError('Multiple exceptions: {}'.format(
', '.join(str(exc) for exc in exceptions)))
return sock
###########
示例5: __init__
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import BTPROTO_HCI [as 別名]
def __init__(self, iface=0x10000, type=None):
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI)
s.setsockopt(socket.SOL_HCI, socket.HCI_DATA_DIR,1)
s.setsockopt(socket.SOL_HCI, socket.HCI_TIME_STAMP,1)
s.setsockopt(socket.SOL_HCI, socket.HCI_FILTER, struct.pack("IIIh2x", 0xffffffffL,0xffffffffL,0xffffffffL,0)) #type mask, event mask, event mask, opcode
s.bind((iface,))
self.ins = self.outs = s
# s.connect((peer,0))
示例6: toggle_device
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import BTPROTO_HCI [as 別名]
def toggle_device(dev_id, enable):
"""
Power ON or OFF a bluetooth device.
:param dev_id: Device id.
:type dev_id: ``int``
:param enable: Whether to enable of disable the device.
:type enable: ``bool``
"""
hci_sock = socket.socket(socket.AF_BLUETOOTH,
socket.SOCK_RAW,
socket.BTPROTO_HCI)
# print("Power %s bluetooth device %d" % ('ON' if enable else 'OFF', dev_id))
# di = struct.pack("HbBIBBIIIHHHH10I", dev_id, *((0,) * 22))
# fcntl.ioctl(hci_sock.fileno(), bluez.HCIGETDEVINFO, di)
req_str = struct.pack("H", dev_id)
request = array.array("b", req_str)
try:
fcntl.ioctl(hci_sock.fileno(),
bluez.HCIDEVUP if enable else bluez.HCIDEVDOWN,
request[0])
except IOError as e:
if e.errno == EALREADY:
pass
# print("Bluetooth device %d is already %s" % (
# dev_id, 'enabled' if enable else 'disabled'))
else:
raise
finally:
hci_sock.close()
# Types of bluetooth scan
示例7: set_scan
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import BTPROTO_HCI [as 別名]
def set_scan(dev_id, scan_type):
"""
Set scan type on a given bluetooth device.
:param dev_id: Device id.
:type dev_id: ``int``
:param scan_type: One of
``'noscan'``
``'iscan'``
``'pscan'``
``'piscan'``
:type scan_type: ``str``
"""
hci_sock = socket.socket(socket.AF_BLUETOOTH,
socket.SOCK_RAW,
socket.BTPROTO_HCI)
if scan_type == "noscan":
dev_opt = SCAN_DISABLED
elif scan_type == "iscan":
dev_opt = SCAN_INQUIRY
elif scan_type == "pscan":
dev_opt = SCAN_PAGE
elif scan_type == "piscan":
dev_opt = SCAN_PAGE | SCAN_INQUIRY
else:
raise ValueError("Unknown scan type %r" % scan_type)
req_str = struct.pack("HI", dev_id, dev_opt)
# print("Set scan type %r to bluetooth device %d" % (scan_type, dev_id))
try:
fcntl.ioctl(hci_sock.fileno(), bluez.HCISETSCAN, req_str)
finally:
hci_sock.close()
示例8: __init__
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import BTPROTO_HCI [as 別名]
def __init__(self, iface=0x10000, type=None):
if WINDOWS:
warning("Not available on Windows")
return
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI) # noqa: E501
s.setsockopt(socket.SOL_HCI, socket.HCI_DATA_DIR, 1)
s.setsockopt(socket.SOL_HCI, socket.HCI_TIME_STAMP, 1)
s.setsockopt(socket.SOL_HCI, socket.HCI_FILTER, struct.pack("IIIh2x", 0xffffffff, 0xffffffff, 0xffffffff, 0)) # type mask, event mask, event mask, opcode # noqa: E501
s.bind((iface,))
self.ins = self.outs = s
# s.connect((peer,0))
示例9: toggle_device
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import BTPROTO_HCI [as 別名]
def toggle_device(dev_id, enable):
"""
Power ON or OFF a bluetooth device.
:param dev_id: Device id.
:type dev_id: ``int``
:param enable: Whether to enable of disable the device.
:type enable: ``bool``
"""
hci_sock = socket.socket(socket.AF_BLUETOOTH,
socket.SOCK_RAW,
socket.BTPROTO_HCI)
print("Power %s bluetooth device %d" % ('ON' if enable else 'OFF', dev_id))
# di = struct.pack("HbBIBBIIIHHHH10I", dev_id, *((0,) * 22))
# fcntl.ioctl(hci_sock.fileno(), bluez.HCIGETDEVINFO, di)
req_str = struct.pack("H", dev_id)
request = array.array("b", req_str)
try:
fcntl.ioctl(hci_sock.fileno(),
bluez.HCIDEVUP if enable else bluez.HCIDEVDOWN,
request[0])
except IOError as e:
if e.errno == EALREADY:
print("Bluetooth device %d is already %s" % (
dev_id, 'enabled' if enable else 'disabled'))
else:
raise
finally:
hci_sock.close()
# Types of bluetooth scan
示例10: set_scan
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import BTPROTO_HCI [as 別名]
def set_scan(dev_id, scan_type):
"""
Set scan type on a given bluetooth device.
:param dev_id: Device id.
:type dev_id: ``int``
:param scan_type: One of
``'noscan'``
``'iscan'``
``'pscan'``
``'piscan'``
:type scan_type: ``str``
"""
hci_sock = socket.socket(socket.AF_BLUETOOTH,
socket.SOCK_RAW,
socket.BTPROTO_HCI)
if scan_type == "noscan":
dev_opt = SCAN_DISABLED
elif scan_type == "iscan":
dev_opt = SCAN_INQUIRY
elif scan_type == "pscan":
dev_opt = SCAN_PAGE
elif scan_type == "piscan":
dev_opt = SCAN_PAGE | SCAN_INQUIRY
else:
raise ValueError("Unknown scan type %r" % scan_type)
req_str = struct.pack("HI", dev_id, dev_opt)
print("Set scan type %r to bluetooth device %d" % (scan_type, dev_id))
try:
fcntl.ioctl(hci_sock.fileno(), bluez.HCISETSCAN, req_str)
finally:
hci_sock.close()
示例11: bind_l2
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import BTPROTO_HCI [as 別名]
def bind_l2(self, psm, bdaddr, cid=0, addr_type=BDAddr.TYPE_BREDR):
sa_str = self._sockaddr_l2(psm, bdaddr, cid, addr_type)
self._bind(sa_str)
# bind to BTPROTO_HCI socket
示例12: connect_l2
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import BTPROTO_HCI [as 別名]
def connect_l2(self, psm, bdaddr, cid=0, addr_type=BDAddr.TYPE_BREDR):
sa_str = self._sockaddr_l2(psm, bdaddr, cid, addr_type)
self._connect(sa_str)
# connect to BTPROTO_HCI socket
示例13: __init__
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import BTPROTO_HCI [as 別名]
def __init__(self, iface=0x10000, type=None):
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI)
s.setsockopt(socket.SOL_HCI, socket.HCI_DATA_DIR,1)
s.setsockopt(socket.SOL_HCI, socket.HCI_TIME_STAMP,1)
s.setsockopt(socket.SOL_HCI, socket.HCI_FILTER, struct.pack("IIIh2x", 0xffffffff,0xffffffff,0xffffffff,0)) #type mask, event mask, event mask, opcode
s.bind((iface,))
self.ins = self.outs = s
# s.connect((peer,0))
示例14: open
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import BTPROTO_HCI [as 別名]
def open(self):
self._socket = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI)
self._socket.bind((self.device_id,))
self._socket_poll_thread = threading.Thread(target=self._socket_poller, name='HCISocketPoller')
self._socket_poll_thread.setDaemon(True)
self._socket_poll_thread.start()
self.device = self.get_device_info()