Advertisement
Guest User

bluetooth

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