當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。