Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = { ... }
- for k,v in pairs({"right","left","top","bottom","front","back"}) do
- if peripheral.getType(v)=="modem" then
- rednet.open(v)
- isOpen=true
- end
- end
- local function printUsage()
- print("Usage:")
- print(" bluetooth receive")
- print(" bluetooth send <file> <computerID>")
- print(" bluetooth scan")
- return
- end
- 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 either does not exist 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.2)
- rednet.send(computerID, "bluetooth.filename:" .. file)
- sleep(0.1)
- print("Sending file...")
- data = sr.readAll()
- rednet.send(computerID, "bluetooth.file:" .. data)
- sleep(0)
- 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
- rednet.send(id, "bluetooth.scan.reply:" .. os.getComputerLabel())
- sleep(0)
- end
- if msg == "bluetooth.connect" then
- print("Connected to " .. id .. ".")
- local remoteID = id
- rednet.send(id, "bluetooth.accept")
- local fileName
- 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. Overwrite existing file? (Y/N)")
- a = read()
- if a == "y" then
- fs.delete(fileName)
- else
- return
- end
- end
- 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)
- file.writeLine(subbedData)
- elseif msg == "bluetooth.done" then
- file.flush()
- file.close()
- if term.isColor() then
- term.setTextColor(colors.white)
- end
- print("Received file '" .. fileName .. "'.")
- return true
- 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