Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = { ... }
- local function printUsage()
- print("Usage:")
- print(" bluetooth receive")
- print(" bluetooth send <file> <computerID>")
- print(" bluetooth scan")
- return
- end
- rednet.open("right")
- local function scan()
- print("Scanning for Bluetooth devices...")
- rednet.broadcast("bluetooth.scan")
- local id,msg = "",""
- local foundComputers = { }
- while not msg == nil do
- local id,msg = rednet.receive()
- if msg == string.sub(msg, 1, #"bluetooth.scan.reply:") == "bluetooth.scan.reply:" then
- --foundComputers[#foundComputers + 1] = {string.sub(msg, #"bluetooth.scan.reply:" + 1), id}
- foundComputers[#foundComputers + 1] = id
- print("Found: " .. string.sub(msg, #"bluetooth.scan.reply:" + 1) .. " - ID: " .. tostring(id))
- end
- end
- end
- local function sendFile(file, computerID)
- if not fs.exists(file) or fs.isDir(file) then
- print("File can't be sent!")
- print("It is either non existant or is a directory!")
- return
- end
- computerID = tonumber(computerID)
- local sr = fs.open(file, "r")
- print("Connecting to bluetooth device.")
- while true do
- rednet.send(computerID, "bluetooth.connect")
- os.startTimer(1)
- local e,p1,p2,p3 = os.pullEvent()
- if e == "rednet_message" then
- local id,msg = p1,p2
- if msg == "bluetooth.accept" then
- print("Bluetooth device connected!")
- break
- end
- end
- end
- sleep(0.8)
- rednet.send(computerID, "bluetooth.filename:" .. file)
- sleep(0.5)
- print("Sending file...")
- local data
- while true do
- data = sr.readLine()
- if data == nil then
- break
- end
- rednet.send(computerID, "bluetooth.file:" .. data)
- sleep(0.1)
- end
- rednet.send(computerID, "bluetooth.done")
- print("File sent!")
- sr.close()
- end
- local function receiveFile()
- print("Waiting for a bluetooth connection...")
- while true do
- local id,msg
- local e,p1,p2,p3 = os.pullEvent()
- if e == "rednet_message" then
- id,msg = p1,p2
- end
- if e == "key" then
- break
- end
- if msg == "bluetooth.scan" then
- sleep(0.1)
- rednet.send(id, "bluetooth.scan.reply:" .. os.getComputerLabel())
- end
- if msg == "bluetooth.connect" then
- print("Connected to " .. id .. "!")
- local remoteID = id
- rednet.send(id, "bluetooth.accept")
- local fileName
- print("Getting the file name...")
- local id,msg = "",""
- while true do
- id,msg = rednet.receive()
- if id == remoteID and string.sub(msg, 1, #"bluetooth.filename:") == "bluetooth.filename:" then
- fileName = string.sub(msg, #"bluetooth.filename:" + 1)
- break
- end
- end
- if fs.exists(fileName) then
- print("File already exists! Deleting old file...")
- fs.delete(fileName)
- end
- print("Receiving file: " .. fileName)
- local file = fs.open(fileName, "w")
- local id,msg
- while true do
- id,msg = rednet.receive()
- if id == remoteID then
- if string.sub(msg, 1, #"bluetooth.file:") == "bluetooth.file:" then
- local subbedData = string.sub(msg, #"bluetooth.file:" + 1)
- if term.isColor() then
- term.setTextColor(colors.lime)
- end
- print(subbedData)
- file.writeLine(subbedData)
- elseif msg == "bluetooth.done" then
- file.flush()
- file.close()
- if term.isColor() then
- term.setTextColor(colors.white)
- end
- print("Received file!")
- break
- end
- end
- end
- end
- end
- end
- if tArgs[1] == "receive" then
- receiveFile()
- elseif tArgs[1] == "send" and #tArgs == 3 then
- sendFile(tArgs[2], tArgs[3])
- elseif tArgs[1] == "scan" then
- scan()
- else
- printUsage()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement