def set_bt_name(name, src_hci, src, dst): """ Sets the name of the local bluetooth device to 'name'. Params: - 'name' - The new name of the device - 'src_hci' - Name of the bluetooth device to change the name of - 'src' - The bluetooth address of the local device - 'dst' - Bluetooth address of a remote device to temporarily connect with (sets the REMOTE_NAME in '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 controller (first 3 bytes are padding for alignment) raw_sock.sendall( binascii.unhexlify('01130cf8cccccc') + name.ljust(MAX_BT_NAME, b'\x00')) raw_sock.close() time.sleep(0.1) # Connect to BNEP to "refresh" the name bnep = bluetooth.BluetoothSocket(bluetooth.L2CAP) bnep.bind((src, 0)) bnep.connect((dst, BNEP_PSM)) bnep.close() # Close ACL connection again util.exec_command_block(["hcitool", "dc", dst])
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,))
import bluetooth._bluetooth as bt # Open hci socket sock = bt.hci_open_dev(0) # Get data direction information sock.setsockopt(bt.SOL_HCI, bt.HCI_DATA_DIR, 1) # Get timestamps sock.setsockopt(bt.SOL_HCI, bt.HCI_TIME_STAMP, 1) # Construct and set filter to sniff all hci events # and all packet types filter = bt.hci_filter_new() bt.hci_filter_all_events(filter) bt.hci_filter_all_ptypes(filter) sock.setsockopt(bt.SOL_HCI, bt.HCI_FILTER, filter) # Start sniffing while True: # Read first 3 byte header = sock.recv(3) if header: # Decode them and read the rest of the packet ptype, event, plen = struct.unpack("BBB", header) packet = sock.recv(plen) print "Ptype: " + str(ptype) + " Event: " + str(event) print "Packet: "