Advertisement
LDDestroier

Bluetooth REMIX

Oct 29th, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local tArgs = { ... }
  2.  
  3. for k,v in pairs({"right","left","top","bottom","front","back"}) do
  4.         if peripheral.getType(v)=="modem" then
  5.                 rednet.open(v)
  6.                 isOpen=true
  7.         end
  8. end
  9.  
  10. local function printUsage()
  11.     print("Usage:")
  12.     print("  bluetooth receive")
  13.     print("  bluetooth send <file> <computerID>")
  14.     print("  bluetooth scan")
  15.     return
  16. end
  17.  
  18. local function scan()
  19.     print("Scanning for Bluetooth devices...")
  20.     rednet.broadcast("bluetooth.scan")
  21.     local id,msg = "",""
  22.     local foundComputers = { }
  23.     while not msg == nil do
  24.         local id,msg = rednet.receive()
  25.         if msg == string.sub(msg, 1, #"bluetooth.scan.reply:") == "bluetooth.scan.reply:" then
  26.             --foundComputers[#foundComputers + 1] = {string.sub(msg, #"bluetooth.scan.reply:" + 1), id}
  27.            
  28.             foundComputers[#foundComputers + 1] = id
  29.             print("Found: " .. string.sub(msg, #"bluetooth.scan.reply:" + 1) .. " - ID: " .. tostring(id))
  30.         end
  31.     end
  32.  
  33. end
  34.  
  35. local function sendFile(file, computerID)
  36.     if not fs.exists(file) or fs.isDir(file) then
  37.         print("File can't be sent.")
  38.         print("It either does not exist or is a directory.")
  39.         return
  40.     end
  41.  
  42.     computerID = tonumber(computerID)
  43.  
  44.     local sr = fs.open(file, "r")
  45.     print("Connecting to bluetooth device.")
  46.    
  47.     while true do
  48.         rednet.send(computerID, "bluetooth.connect")
  49.         os.startTimer(1)
  50.         local e,p1,p2,p3 = os.pullEvent()
  51.         if e == "rednet_message" then
  52.             local id,msg = p1,p2
  53.             if msg == "bluetooth.accept" then
  54.                 print("Bluetooth device connected!")
  55.                 break
  56.             end
  57.         end
  58.     end
  59.    
  60.     sleep(0.2)
  61.     rednet.send(computerID, "bluetooth.filename:" .. file)
  62.    
  63.     sleep(0.1)
  64.    
  65.     print("Sending file...")
  66.     data = sr.readAll()
  67.     rednet.send(computerID, "bluetooth.file:" .. data)
  68.     sleep(0)
  69.     rednet.send(computerID, "bluetooth.done")
  70.     print("File sent!")
  71.     sr.close()
  72. end
  73.  
  74. local function receiveFile()
  75.     print("Waiting for a bluetooth connection...")
  76.    
  77.     while true do
  78.         local id,msg
  79.         local e,p1,p2,p3 = os.pullEvent()
  80.         if e == "rednet_message" then
  81.             id,msg = p1,p2
  82.         end
  83.         if e == "key" then
  84.             break
  85.         end
  86.        
  87.         if msg == "bluetooth.scan" then
  88.             rednet.send(id, "bluetooth.scan.reply:" .. os.getComputerLabel())
  89.             sleep(0)
  90.         end
  91.        
  92.         if msg == "bluetooth.connect" then
  93.             print("Connected to " .. id .. ".")
  94.             local remoteID = id
  95.             rednet.send(id, "bluetooth.accept")
  96.            
  97.             local fileName
  98.            
  99.             local id,msg = "",""
  100.             while true do
  101.                 id,msg = rednet.receive()
  102.                
  103.                 if id == remoteID and string.sub(msg, 1, #"bluetooth.filename:") == "bluetooth.filename:" then
  104.                     fileName = string.sub(msg, #"bluetooth.filename:" + 1)
  105.                     break
  106.                 end
  107.             end
  108.            
  109.             if fs.exists(fileName) then
  110.                 print("File already exists. Overwrite existing file? (Y/N)")
  111.                                 a = read()
  112.                                 if a == "y" then
  113.                                         fs.delete(fileName)
  114.                                 else
  115.                                         return
  116.                                 end
  117.             end
  118.            
  119.             local file = fs.open(fileName, "w")
  120.            
  121.             local id,msg
  122.             while true do
  123.                 id,msg = rednet.receive()
  124.                 if id == remoteID then
  125.                     if string.sub(msg, 1, #"bluetooth.file:") == "bluetooth.file:" then
  126.                         local subbedData = string.sub(msg, #"bluetooth.file:" + 1)
  127.                        
  128.                         file.writeLine(subbedData)
  129.                     elseif msg == "bluetooth.done" then
  130.                         file.flush()
  131.                         file.close()
  132.                    
  133.                         if term.isColor() then
  134.                             term.setTextColor(colors.white)
  135.                         end
  136.                    
  137.                         print("Received file '" .. fileName .. "'.")
  138.                        
  139.                         return true
  140.                     end
  141.                 end
  142.             end
  143.         end
  144.     end
  145. end
  146.  
  147. if tArgs[1] == "receive" then
  148.     receiveFile()
  149. elseif tArgs[1] == "send" and #tArgs == 3 then
  150.     sendFile(tArgs[2], tArgs[3])
  151. elseif tArgs[1] == "scan" then
  152.     scan()
  153. else
  154.     printUsage()
  155. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement