ssb-mobile-bluetooth-manager
Advanced tools
Comparing version 1.0.3 to 1.1.0
165
index.js
@@ -1,6 +0,9 @@ | ||
const rn_bridge = require('rn-bridge'); | ||
const net = require('net'); | ||
const toPull = require('stream-to-pull-stream'); | ||
const fs = require('fs'); | ||
const pull = require('pull-stream'); | ||
const Pushable = require('pull-pushable'); | ||
const pullJson = require('pull-json-doubleline') | ||
function makeManager () { | ||
@@ -22,18 +25,106 @@ | ||
let controlSocketSource = Pushable(); | ||
const nearbyDevicesSource = Pushable(); | ||
function connect(bluetoothAddress, cb) { | ||
awaitingConnection.push(cb); | ||
console.log("Attempting outgoing connection to bluetooth address: " + bluetoothAddress); | ||
awaitingConnection.push({ | ||
address: bluetoothAddress, | ||
cb | ||
}); | ||
// Tell the native android code to make the outgoing bluetooth connection and then connect back | ||
// on the socket | ||
var bridgeMsg = { | ||
type: "connectBt", | ||
params: { | ||
remoteAddress: bluetoothAddress | ||
controlSocketSource.push({ | ||
"command": "connect", | ||
"arguments": { | ||
"remoteAddress": bluetoothAddress | ||
} | ||
}) | ||
} | ||
let controlSocketEstablished = false; | ||
function makeControlSocket() { | ||
if (controlSocketEstablished) return; | ||
var address = "/data/data/se.manyver/files/manyverse_bt_control.sock"; | ||
try { | ||
fs.unlinkSync(address); | ||
} catch (error) { | ||
} | ||
console.log("bt: Asking to connect over bridge to " + bluetoothAddress); | ||
rn_bridge.channel.send(JSON.stringify(bridgeMsg)); | ||
var controlSocket = net.createServer(function(stream){ | ||
var duplexConnection = toPull.duplex(stream); | ||
// Send commands to the control server | ||
pull(controlSocketSource, | ||
pullJson.stringify(), | ||
pull.map(logOutgoingCommand), | ||
duplexConnection.sink | ||
); | ||
// Receive and process commands from the control server | ||
pull(duplexConnection.source, pullJson.parse(), pull.drain(doCommand)); | ||
}).listen(address); | ||
controlSocketEstablished = true; | ||
controlSocket.on('closed', function() { | ||
console.log("Control socket closed"); | ||
}) | ||
console.log("Created control socket"); | ||
} | ||
function logOutgoingCommand(command) { | ||
console.log("Sending outgoing command to control server"); | ||
console.log(command); | ||
return command; | ||
} | ||
function doCommand (command) { | ||
console.log("Received command: "); | ||
console.dir(command); | ||
let commandName = command.command; | ||
if (commandName === "connected" && !command.arguments.isIncoming) { | ||
// The initial stream connection is just to the Unix socket. We don't know if that socket is proxying | ||
// the bluetooth connection successfully until we receive an event to tell us it's connected. | ||
var awaiting = awaitingConnection.shift(); | ||
awaiting.cb(null, awaiting.stream); | ||
} else if (commandName === "connectionFailure") { | ||
var awaiting = awaitingConnection.shift(); | ||
var reason = command.arguments.reason; | ||
awaiting.cb(new Error(reason), null); | ||
} else if (commandName === "disconnected") { | ||
} else if (commandName === "discovered") { | ||
var currentTime = Date.now(); | ||
var arguments = command.arguments; | ||
console.log("Updating nearby source"); | ||
console.log(arguments); | ||
nearbyDevicesSource.push({ | ||
lastUpdate: currentTime, | ||
discovered: arguments.devices | ||
}) | ||
} | ||
} | ||
function listenForOutgoingEstablished() { | ||
@@ -49,9 +140,8 @@ var address = "/data/data/se.manyver/files/manyverse_bt_outgoing.sock"; | ||
var server = net.createServer(function(stream){ | ||
console.log("bluetooth: Outgoing connection established, calling back.") | ||
var cb = awaitingConnection.shift(); | ||
cb(null, toPull.duplex(stream)); | ||
console.log("bluetooth: Outgoing connection established proxy connection, adding to awaiting object.") | ||
awaitingConnection[0].stream = toPull.duplex(stream); | ||
}).listen(address); | ||
} | ||
// For some reason, .server gets called twice... | ||
@@ -75,20 +165,2 @@ var started = false | ||
server.on('error', function (e) { | ||
if (e.code == 'EADDRINUSE') { | ||
var clientSocket = new net.Socket() | ||
clientSocket.on('error', function(e) { | ||
if (e.code == 'ECONNREFUSED') { | ||
fs.unlinkSync(socket) | ||
server.listen(socket) | ||
} | ||
}) | ||
clientSocket.connect({ path: socket }, function() { | ||
console.log("bt-bridge: someone else is listening on socket!") | ||
}) | ||
} else { | ||
console.log("bt_bridge: " + e); | ||
} | ||
}) | ||
server.on('close', function (e) { | ||
@@ -105,7 +177,38 @@ console.log("bt_bridge socket closed: " + e); | ||
function refreshNearbyDevices() { | ||
// Tell the native android code to discover nearby devices. When it responds, we'll update the | ||
// 'nearBy devices' pull-stream source | ||
controlSocketSource.push({ | ||
"command": "discoverDevices", | ||
"arguments": { | ||
} | ||
}); | ||
} | ||
function nearbyDevices() { | ||
return nearbyDevicesSource; | ||
} | ||
function makeDeviceDiscoverable(forTime) { | ||
console.log("Making device discoverable"); | ||
controlSocketSource.push({ | ||
"command": "makeDiscoverable", | ||
"arguments": { | ||
"forTime": forTime | ||
} | ||
}); | ||
} | ||
listenForOutgoingEstablished(); | ||
makeControlSocket(); | ||
return { | ||
connect, | ||
listenForIncomingConnections | ||
listenForIncomingConnections, | ||
refreshNearbyDevices, | ||
nearbyDevices, | ||
makeDeviceDiscoverable | ||
} | ||
@@ -112,0 +215,0 @@ |
{ | ||
"name": "ssb-mobile-bluetooth-manager", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "A module for managing bluetooth connections over a react native bridge.", | ||
@@ -16,4 +16,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"pull-abortable": "^4.1.1", | ||
"pull-pushable": "^2.2.0", | ||
"pull-json-doubleline": "^2.0.0", | ||
"pull-stream": "^3.6.9", | ||
@@ -20,0 +19,0 @@ "stream-to-pull-stream": "^1.7.2" |
7407
3
155
+ Addedpull-json-doubleline@^2.0.0
+ Addeddefined@1.0.1(transitive)
+ Addedis-pull-stream@0.0.0(transitive)
+ Addedpull-json-doubleline@2.0.0(transitive)
+ Addedpull-split@0.2.1(transitive)
+ Addedpull-stringify@2.0.0(transitive)
+ Addedpull-through@1.0.18(transitive)
- Removedpull-abortable@^4.1.1
- Removedpull-pushable@^2.2.0
- Removedpull-abortable@4.1.1(transitive)
- Removedpull-pushable@2.2.0(transitive)