Advertisement
Guest User

bluetooth

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