def get_local_bdaddr(self): id = self.statusbar.get_context_id("device_info") try: hci_sock = _bt.hci_open_dev() old_filter = hci_sock.getsockopt(_bt.SOL_HCI, _bt.HCI_FILTER, 14) flt = _bt.hci_filter_new() opcode = _bt.cmd_opcode_pack(_bt.OGF_INFO_PARAM, _bt.OCF_READ_BD_ADDR) _bt.hci_filter_set_ptype(flt, _bt.HCI_EVENT_PKT) _bt.hci_filter_set_event(flt, _bt.EVT_CMD_COMPLETE) _bt.hci_filter_set_opcode(flt, opcode) hci_sock.setsockopt(_bt.SOL_HCI, _bt.HCI_FILTER, flt) _bt.hci_send_cmd(hci_sock, _bt.OGF_INFO_PARAM, _bt.OCF_READ_BD_ADDR) pkt = hci_sock.recv(255) status, raw_bdaddr = struct.unpack("xxxxxxB6s", pkt) assert status == 0 t = ["%02X" % ord(b) for b in raw_bdaddr] t.reverse() bdaddr = ":".join(t) # restore old filter hci_sock.setsockopt(_bt.SOL_HCI, _bt.HCI_FILTER, old_filter) self.statusbar.push(id, "Local bt device address: %s" % (bdaddr)) self.DEV_BLUE = True except: self.statusbar.push(id, "No local bt device found!")
def read_local_bdaddr(): try: hci_sock = _bt.hci_open_dev(0) old_filter = hci_sock.getsockopt(_bt.SOL_HCI, _bt.HCI_FILTER, 14) flt = _bt.hci_filter_new() opcode = _bt.cmd_opcode_pack(_bt.OGF_INFO_PARAM, _bt.OCF_READ_BD_ADDR) _bt.hci_filter_set_ptype(flt, _bt.HCI_EVENT_PKT) _bt.hci_filter_set_event(flt, _bt.EVT_CMD_COMPLETE) _bt.hci_filter_set_opcode(flt, opcode) hci_sock.setsockopt(_bt.SOL_HCI, _bt.HCI_FILTER, flt) _bt.hci_send_cmd(hci_sock, _bt.OGF_INFO_PARAM, _bt.OCF_READ_BD_ADDR) pkt = hci_sock.recv(255) status, raw_bdaddr = struct.unpack("xxxxxxB6s", pkt) assert status == 0 t = ["%02X" % get_byte(b) for b in raw_bdaddr] t.reverse() bdaddr = ":".join(t) # restore old filter hci_sock.setsockopt(_bt.SOL_HCI, _bt.HCI_FILTER, old_filter) return [bdaddr] except _bt.error as e: raise BluetoothError(*e.args)
def read_local_bdaddr(): hci_sock = _bt.hci_open_dev(0) old_filter = hci_sock.getsockopt( _bt.SOL_HCI, _bt.HCI_FILTER, 14) flt = _bt.hci_filter_new() opcode = _bt.cmd_opcode_pack(_bt.OGF_INFO_PARAM, _bt.OCF_READ_BD_ADDR) _bt.hci_filter_set_ptype(flt, _bt.HCI_EVENT_PKT) _bt.hci_filter_set_event(flt, _bt.EVT_CMD_COMPLETE); _bt.hci_filter_set_opcode(flt, opcode) hci_sock.setsockopt( _bt.SOL_HCI, _bt.HCI_FILTER, flt ) _bt.hci_send_cmd(hci_sock, _bt.OGF_INFO_PARAM, _bt.OCF_READ_BD_ADDR ) pkt = hci_sock.recv(255) status,raw_bdaddr = struct.unpack("xxxxxxB6s", pkt) assert status == 0 t = [ "%X" % get_byte(b) for b in raw_bdaddr ] t.reverse() bdaddr = ":".join(t) # restore old filter hci_sock.setsockopt( _bt.SOL_HCI, _bt.HCI_FILTER, old_filter ) return [bdaddr]
def _gethcisock(device_id=-1): try: sock = _bt.hci_open_dev(device_id) except _bt.error as e: raise BluetoothError(e.args[0], "error accessing bluetooth device: " + e.args[1]) return sock
def _write_flush_timeout(addr, timeout): hci_sock = _bt.hci_open_dev() # get the ACL connection handle to the remote device handle = _get_acl_conn_handle(hci_sock, addr) # XXX should this be "<HH" pkt = struct.pack("HH", handle, _bt.htobs(timeout)) response = _bt.hci_send_req(hci_sock, _bt.OGF_HOST_CTL, 0x0028, _bt.EVT_CMD_COMPLETE, 3, pkt) status = struct.unpack("B", response[0])[0] rhandle = struct.unpack("H", response[1:3])[0] assert rhandle == handle assert status == 0
def write_flush_timeout( addr, timeout ): hci_sock = bt.hci_open_dev() # get the ACL connection handle to the remote device handle = __get_acl_conn_handle(hci_sock, addr) pkt = struct.pack("HH", handle, bt.htobs(timeout)) response = bt.hci_send_req(hci_sock, bt.OGF_HOST_CTL, 0x0028, bt.EVT_CMD_COMPLETE, 3, pkt) status = struct.unpack("B", response[0])[0] rhandle = struct.unpack("H", response[1:3])[0] assert rhandle == handle assert status == 0
def _read_flush_timeout(addr): hci_sock = _bt.hci_open_dev() # get the ACL connection handle to the remote device handle = _get_acl_conn_handle(hci_sock, addr) # XXX should this be "<H"? pkt = struct.pack("H", handle) response = _bt.hci_send_req(hci_sock, _bt.OGF_HOST_CTL, 0x0027, _bt.EVT_CMD_COMPLETE, 5, pkt) status = struct.unpack("B", response[0])[0] rhandle = struct.unpack("H", response[1:3])[0] assert rhandle == handle assert status == 0 fto = struct.unpack("H", response[3:5])[0] return fto
def read_flush_timeout( addr ): hci_sock = bt.hci_open_dev() # get the ACL connection handle to the remote device handle = __get_acl_conn_handle(hci_sock, addr) pkt = struct.pack("H", handle) response = bt.hci_send_req(hci_sock, bt.OGF_HOST_CTL, 0x0027, bt.EVT_CMD_COMPLETE, 5, pkt) status = struct.unpack("B", response[0])[0] rhandle = struct.unpack("H", response[1:3])[0] assert rhandle == handle assert status == 0 fto = struct.unpack("H", response[3:5])[0] return fto
def get_proximity_by_mac(self,dev_mac): sock = bluez.hci_open_dev(dev_id) old_filter = sock.getsockopt( bluez.SOL_HCI, bluez.HCI_FILTER, 14) # perform a device inquiry on bluetooth device #0 # The inquiry should last 8 * 1.28 = 10.24 seconds # before the inquiry is performed, bluez should flush its cache of # previously discovered devices flt = bluez.hci_filter_new() bluez.hci_filter_all_events(flt) bluez.hci_filter_set_ptype(flt, bluez.HCI_EVENT_PKT) sock.setsockopt( bluez.SOL_HCI, bluez.HCI_FILTER, flt ) duration = 4 max_responses = 255 cmd_pkt = struct.pack("BBBBB", 0x33, 0x8b, 0x9e, duration, max_responses) bluez.hci_send_cmd(sock, bluez.OGF_LINK_CTL, bluez.OCF_INQUIRY, cmd_pkt) results = [] done = False while not done: pkt = sock.recv(255) ptype, event, plen = struct.unpack("BBB", pkt[:3]) if event == bluez.EVT_INQUIRY_RESULT_WITH_RSSI: pkt = pkt[3:] nrsp = struct.unpack("B", pkt[0])[0] for i in range(nrsp): addr = bluez.ba2str( pkt[1+6*i:1+6*i+6] ) rssi = struct.unpack("b", pkt[1+13*nrsp+i])[0] results.append( ( addr, rssi ) ) print "[%s] RSSI: [%d]" % (addr, rssi) elif event == bluez.EVT_INQUIRY_COMPLETE: done = True elif event == bluez.EVT_CMD_STATUS: status, ncmd, opcode = struct.unpack("BBH", pkt[3:7]) if status != 0: print "uh oh..." printpacket(pkt[3:7]) done = True else: print "unrecognized packet type 0x%02x" % ptype # restore old filter sock.setsockopt( bluez.SOL_HCI, bluez.HCI_FILTER, old_filter ) sock.close() return results
def _gethcisock(device_id=-1): try: sock = _bt.hci_open_dev(device_id) except: raise BluetoothError("error accessing bluetooth device") return sock
def _gethcisock(devid=-1): try: sock = _bluetooth.hci_open_dev(devid) except Exception, e: raise _lightbluecommon.BluetoothError("Cannot access local device: " + str(e))
if status != 0: print "uh oh..." printpacket(pkt[3:7]) done = True else: print "unrecognized packet type 0x%02x" % ptype # restore old filter sock.setsockopt( bluez.SOL_HCI, bluez.HCI_FILTER, old_filter ) return results dev_id = 0 try: sock = bluez.hci_open_dev(dev_id) except: print "error accessing bluetooth device..." sys.exit(1) try: mode = read_inquiry_mode(sock) except Exception, e: print "error reading inquiry mode. " print "Are you sure this a bluetooth 1.2 device?" print e sys.exit(1) print "current inquiry mode is %d" % mode if mode != 1: print "writing inquiry mode..."
bluez.hci_send_cmd(sock, bluez.OGF_HOST_CTL, bluez.OCF_WRITE_INQ_ACTIVITY, struct.pack("HH", interval, window)) pkt = sock.recv(255) status = struct.unpack("xxxxxxB", pkt)[0] # restore old filter sock.setsockopt(bluez.SOL_HCI, bluez.HCI_FILTER, old_filter) if status != 0: return -1 return 0 dev_id = 0 try: sock = bluez.hci_open_dev(dev_id) except: print "error accessing bluetooth device..." sys.exit(1) try: interval, window = read_inquiry_scan_activity(sock) except Exception, e: print "error reading inquiry scan activity. " print e sys.exit(1) print "current inquiry scan interval: %d (0x%X) window: %d (0x%X)" % \ (interval, interval, window, window) if len(sys.argv) == 3: interval = int(sys.argv[1])
def _gethcisock (device_id = -1): try: sock = _bt.hci_open_dev (device_id) except: raise BluetoothError ("error accessing bluetooth device") return sock
def _gethcisock(devid=-1): try: sock = _bluetooth.hci_open_dev(devid) except Exception, e: raise _lightbluecommon.BluetoothError( "Cannot access local device: " + str(e))
def read_local_bdaddr(hci_sock): old_filter = hci_sock.getsockopt( _bt.SOL_HCI, _bt.HCI_FILTER, 14) flt = _bt.hci_filter_new() opcode = _bt.cmd_opcode_pack(_bt.OGF_INFO_PARAM, _bt.OCF_READ_BD_ADDR) _bt.hci_filter_set_ptype(flt, _bt.HCI_EVENT_PKT) _bt.hci_filter_set_event(flt, _bt.EVT_CMD_COMPLETE); _bt.hci_filter_set_opcode(flt, opcode) hci_sock.setsockopt( _bt.SOL_HCI, _bt.HCI_FILTER, flt ) _bt.hci_send_cmd(hci_sock, _bt.OGF_INFO_PARAM, _bt.OCF_READ_BD_ADDR ) pkt = hci_sock.recv(255) status,raw_bdaddr = struct.unpack("xxxxxxB6s", pkt) assert status == 0 t = [ "%X" % ord(b) for b in raw_bdaddr ] t.reverse() bdaddr = ":".join(t) # restore old filter hci_sock.setsockopt( _bt.SOL_HCI, _bt.HCI_FILTER, old_filter ) return bdaddr if __name__ == "__main__": dev_id = 0 hci_sock = _bt.hci_open_dev(dev_id) bdaddr = read_local_bdaddr(hci_sock) print bdaddr