本文整理汇总了Python中bluetooth.BluetoothSocket方法的典型用法代码示例。如果您正苦于以下问题:Python bluetooth.BluetoothSocket方法的具体用法?Python bluetooth.BluetoothSocket怎么用?Python bluetooth.BluetoothSocket使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bluetooth
的用法示例。
在下文中一共展示了bluetooth.BluetoothSocket方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: import bluetooth [as 别名]
# 或者: from bluetooth import BluetoothSocket [as 别名]
def main():
if (len(sys.argv) < 2):
print("Usage: bl_battery.py <BT_MAC_ADDRESS_1>[.PORT] ...")
print(" Port number is optional (default = 4)")
exit()
else:
for device in sys.argv[1:]:
i = device.find('.')
if i == -1:
port = 4
else:
port = int(device[i+1:])
device = device[:i]
try:
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.connect((device, port))
while getATCommand(s, s.recv(128), device):
pass
s.close()
except OSError as e:
print(f"{device} is offline", e)
示例2: set_bt_name
# 需要导入模块: import bluetooth [as 别名]
# 或者: from bluetooth import BluetoothSocket [as 别名]
def set_bt_name(payload, src_hci, src, dst):
# Create raw HCI sock to set our BT name
raw_sock = bt.hci_open_dev(bt.hci_devid(src_hci))
flt = bt.hci_filter_new()
bt.hci_filter_all_ptypes(flt)
bt.hci_filter_all_events(flt)
raw_sock.setsockopt(bt.SOL_HCI, bt.HCI_FILTER, flt)
# Send raw HCI command to our controller to change the BT name (first 3 bytes are padding for alignment)
raw_sock.sendall(binascii.unhexlify('01130cf8cccccc') + payload.ljust(MAX_BT_NAME, b'\x00'))
raw_sock.close()
#time.sleep(1)
time.sleep(0.1)
# Connect to BNEP to "refresh" the name (does auth)
bnep = bluetooth.BluetoothSocket(bluetooth.L2CAP)
bnep.bind((src, 0))
bnep.connect((dst, BNEP_PSM))
bnep.close()
# Close ACL connection
os.system('hcitool dc %s' % (dst,))
#time.sleep(1)
示例3: start_service
# 需要导入模块: import bluetooth [as 别名]
# 或者: from bluetooth import BluetoothSocket [as 别名]
def start_service(self, service, adapter_addr=''):
print_verbose('Starting service ',service)
server_sock=None
if service['port'] in self.servers:
print('Port',service['port'],'is already binded to')
return server_sock
if service['protocol'].lower() == 'l2cap':
server_sock=BluetoothSocket( L2CAP )
else:
server_sock=BluetoothSocket( RFCOMM )
addrport = (adapter_address(self.master_adapter),service['port'])
print_verbose('Binding to ',addrport)
server_sock.bind(addrport)
self.servers.append(service['port'])
server_sock.listen(1)
port = server_sock.getsockname()[1]
return server_sock
示例4: start
# 需要导入模块: import bluetooth [as 别名]
# 或者: from bluetooth import BluetoothSocket [as 别名]
def start(self):
if self._socket:
return
if usingLightBlue:
for each_try in range(1, 5):
print("[*] Connecting to %s on PSM %d (%d)" % (self.ba_addr, self.port, each_try))
try:
self._socket = lightblue.socket(lightblue.L2CAP)
self._socket.connect((self.ba_addr, self.port))
except socket.error:
self._socket = None
print("Failed.")
print("Wait {} seconds ...".format(self.giveup))
time.sleep(self.giveup)
else:
print("Done.")
break
if usingBluetooth:
for each_try in range(1, 5):
print("[*] Connecting to %s on PSM %d (%d)" % (self.ba_addr, self.port, each_try))
try:
self._socket = bluetooth.BluetoothSocket(bluetooth.L2CAP)
self._socket.connect((self.ba_addr, self.port))
except socket.error:
self._socket = None
print("Failed.")
print("Wait {} seconds ...".format(self.giveup))
time.sleep(self.giveup)
else:
print("Done.")
break
print("")
if not self._socket:
raise PeachException("L2CAP connection attempt failed.")
示例5: connect
# 需要导入模块: import bluetooth [as 别名]
# 或者: from bluetooth import BluetoothSocket [as 别名]
def connect(self, port=None):
'''
Connect to the device
:param port: port used for connection
:type port: int
'''
if self.connected:
return
if not port:
port = self.port
logger.debug('Connecting %s on port %s', self, port)
self.sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM,
bluez.btsocket())
self.sock.connect((self.mac, port))
示例6: connect
# 需要导入模块: import bluetooth [as 别名]
# 或者: from bluetooth import BluetoothSocket [as 别名]
def connect(self):
if self.debug:
print('Connecting via Bluetooth...')
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((self.host, BlueSock.PORT))
self.sock = sock
if self.debug:
print('Connected.')
return Brick(self)
示例7: test_bluetooth_1_connection
# 需要导入模块: import bluetooth [as 别名]
# 或者: from bluetooth import BluetoothSocket [as 别名]
def test_bluetooth_1_connection(self):
socket = blue.BluetoothSocket(blue.RFCOMM)
socket.connect((self.ID, 1))
peer_name = socket.getpeername()
self.assertEqual(self.ID, peer_name[0])
示例8: connectToMindWaveMobile
# 需要导入模块: import bluetooth [as 别名]
# 或者: from bluetooth import BluetoothSocket [as 别名]
def connectToMindWaveMobile(self, mac):
# connecting via bluetooth RFCOMM
self.mindwaveMobileSocket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
mindwaveMobileAddress = mac;
while(True):
try:
self.mindwaveMobileSocket.connect((mindwaveMobileAddress, 1))
return;
except bluetooth.btcommon.BluetoothError as error:
print "Could not connect: ", error, "; Retrying in 5s..."
time.sleep(5)
示例9: connect
# 需要导入模块: import bluetooth [as 别名]
# 或者: from bluetooth import BluetoothSocket [as 别名]
def connect(self, protocol=None, device: str = None, port: int = None, service_uuid: str = None,
service_name: str = None):
"""
Connect to a bluetooth device.
You can query the advertised services through ``find_service``.
:param protocol: Supported values: either 'RFCOMM'/'L2CAP' (str) or bluetooth.RFCOMM/bluetooth.L2CAP
int constants (int)
:param device: Device address or name
:param port: Port number
:param service_uuid: Service UUID
:param service_name: Service name
"""
from bluetooth import BluetoothSocket
addr, port, protocol = self._get_addr_port_protocol(protocol=protocol, device=device, port=port,
service_uuid=service_uuid, service_name=service_name)
sock = self._get_sock(protocol=protocol, device=addr, port=port)
if sock:
self.close(device=addr, port=port)
sock = BluetoothSocket(protocol)
self.logger.info('Opening connection to device {} on port {}'.format(addr, port))
sock.connect((addr, port))
self.logger.info('Connected to device {} on port {}'.format(addr, port))
self._socks[(addr, port)] = sock
示例10: initialize
# 需要导入模块: import bluetooth [as 别名]
# 或者: from bluetooth import BluetoothSocket [as 别名]
def initialize(self):
self.server_socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
self.server_socket.bind(("",bluetooth.PORT_ANY))
self.server_socket.listen(1)
port = self.server_socket.getsockname()[1]
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
bluetooth.advertise_service(
self.server_socket,
"ReachBluetoothSocket",
service_id = uuid,
service_classes = [uuid, bluetooth.SERIAL_PORT_CLASS],
profiles = [bluetooth.SERIAL_PORT_PROFILE],
)
示例11: connect
# 需要导入模块: import bluetooth [as 别名]
# 或者: from bluetooth import BluetoothSocket [as 别名]
def connect(self):
if self.address is None and not self.scandevices():
return False
if not self.scanservices():
return False
logging.info("Service found. Connecting to \"%s\" on %s..." % (self.service["name"], self.service["host"]))
self.sock = BluetoothSocket(RFCOMM)
self.sock.connect((self.service["host"], self.service["port"]))
self.sock.settimeout(60)
logging.info("Connected.")
self.registerCrcKeyToBt()
return True
示例12: pybluez_server_test
# 需要导入模块: import bluetooth [as 别名]
# 或者: from bluetooth import BluetoothSocket [as 别名]
def pybluez_server_test():
s = bluetooth.BluetoothSocket(bluetooth.L2CAP)
s.bind(("", 0x1001))
s.listen(1)
conn, addr = s.accept()
print("Connected by %s" % addr)
data = s.recv(1024)
print("Received: %s" % data)
conn.close()
s.close()
示例13: getRSSI
# 需要导入模块: import bluetooth [as 别名]
# 或者: from bluetooth import BluetoothSocket [as 别名]
def getRSSI(self):
"""Detects whether the device is near by or not using RSSI"""
addr = self.address
# Open hci socket
hci_sock = bt.hci_open_dev()
hci_fd = hci_sock.fileno()
# Connect to device (to whatever you like)
bt_sock = bluetooth.BluetoothSocket(bluetooth.L2CAP)
bt_sock.settimeout(10)
result = bt_sock.connect_ex((addr, 1)) # PSM 1 - Service Discovery
try:
# Get ConnInfo
reqstr = struct.pack("6sB17s", bt.str2ba(addr), bt.ACL_LINK, "\0" * 17)
request = array.array("c", reqstr )
handle = fcntl.ioctl(hci_fd, bt.HCIGETCONNINFO, request, 1)
handle = struct.unpack("8xH14x", request.tostring())[0]
# Get RSSI
cmd_pkt=struct.pack('H', handle)
rssi = bt.hci_send_req(hci_sock, bt.OGF_STATUS_PARAM,
bt.OCF_READ_RSSI, bt.EVT_CMD_COMPLETE, 4, cmd_pkt)
rssi = struct.unpack('b', rssi[3])[0]
# Close sockets
bt_sock.close()
hci_sock.close()
return rssi
except Exception, e:
#self.logger.error("<Bluetooth> (getRSSI) %s" % (repr(e)))
return None
示例14: catch
# 需要导入模块: import bluetooth [as 别名]
# 或者: from bluetooth import BluetoothSocket [as 别名]
def catch(self):
_check_lib_bluetooth()
self.socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
self.socket.connect((self.address, self.port))
示例15: connect
# 需要导入模块: import bluetooth [as 别名]
# 或者: from bluetooth import BluetoothSocket [as 别名]
def connect(self, addr):
sock = bluetooth.BluetoothSocket (bluetooth.L2CAP)
try:
sock.connect((addr, 0x1001))
except bluez.error, e:
self.add_text("\n%s" % str(e))
sock.close()
return