当前位置: 首页>>代码示例>>Python>>正文


Python bluetooth.BluetoothSocket方法代码示例

本文整理汇总了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) 
开发者ID:TheWeirdDev,项目名称:Bluetooth_Headset_Battery_Level,代码行数:23,代码来源:bluetooth_battery.py

示例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) 
开发者ID:ArmisSecurity,项目名称:blueborne,代码行数:25,代码来源:doit.py

示例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 
开发者ID:conorpp,项目名称:btproxy,代码行数:23,代码来源:mitm.py

示例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.") 
开发者ID:MozillaSecurity,项目名称:peach,代码行数:39,代码来源:bluetooth.py

示例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)) 
开发者ID:Thor77,项目名称:Blueproximity,代码行数:17,代码来源:device.py

示例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) 
开发者ID:Eelviny,项目名称:nxt-python,代码行数:11,代码来源:bluesock.py

示例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]) 
开发者ID:felipessalvatore,项目名称:self_driving_pi_car,代码行数:7,代码来源:test_bluetooth.py

示例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) 
开发者ID:JoePrezioso,项目名称:NeuroPi,代码行数:13,代码来源:MindwaveMobileRawReader.py

示例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 
开发者ID:BlackLight,项目名称:platypush,代码行数:28,代码来源:__init__.py

示例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],
        ) 
开发者ID:emlid,项目名称:ReachView,代码行数:17,代码来源:tcp_bridge.py

示例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 
开发者ID:ihciah,项目名称:miaomiaoji-tool,代码行数:14,代码来源:message_process.py

示例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() 
开发者ID:MozillaSecurity,项目名称:peach,代码行数:12,代码来源:bluetooth.py

示例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 
开发者ID:rkoshak,项目名称:sensorReporter,代码行数:37,代码来源:bluetoothScanner.py

示例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)) 
开发者ID:base4sistemas,项目名称:pyescpos,代码行数:6,代码来源:bt.py

示例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 
开发者ID:kuri65536,项目名称:python-for-android,代码行数:10,代码来源:bluezchat.py


注:本文中的bluetooth.BluetoothSocket方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。