This is a node addon api package used for connecting other bluetooth through Bluethooth SPP. IMPORTANT! It supports for Windows platform. For now, we can only connect to other bluetooth spp. It means, this package does not act as server.
This library is developed in C++ and support for Node.js.
Install this package into your project by below command
npm install @siva7170/ble-connection
Below code is sample for how to use it. Please see methods and its functionalities below sections.
const bleConnection = require('@siva7170/ble-connection');
const bleConnInstance = new bleConnection.BLEConnection();
successCallback:
Function
failureCallback:
Function
It initializes the necessary things.
bleConnInstance.Initiate(()=>{
console.log("Initiated!");
// rest of the code
},()=>{
console.log("Failed to initiate!");
});
bluetooth_addr:
String
uuid:
String
successCallback:
Function
failureCallback:
Function
This method will try to connect to the given bluetooth address and uuid from bluetooth spp server.
// please use valid bluetooth address and UUID
bleConnInstance.Connect("00:00:00:00:00:E0","aaaaaaaa-aaaa-4444-cccc-999888999888",()=>{
console.log("Connected!");
// rest of the code
},()=>{
console.log("Failed to connect!");
});
data:
String
successCallback:
Function
failureCallback:
Function
With this method, you can send data to client
bleConnInstance.SendData('Hi server!',(res)=>{
console.log("Data sent: "+sData);
}, (err)=>{
});
Function
This method will be triggered when the data sent from bluetooth server
bleConnInstance.OnReceiveData((data)=>{
console.log("Data receivedd: "+data);
});
Please find full example of implementation
const bleConnection = require('@siva7170/ble-connection');
const bleConnInstance = new bleConnection.BLEConnection();
try{
bleConnInstance.Initiate(()=>{
console.log("Initiated!");
bleConnInstance.Connect("00:00:00:00:00:E0","aaaaaaaa-aaaa-4444-cccc-999888999888",()=>{
console.log("Connected!");
let sData="Hi client";
bleConnInstance.SendData(sData,(res)=>{
console.log("Data sent: "+sData);
}, (err)=>{
});
bleConnInstance.OnReceiveData((data)=>{
console.log("Data receivedd: "+data);
});
sData="How are you?";
bleConnInstance.SendData(sData,(res)=>{
console.log("Data sent: "+sData);
}, (err)=>{
});
},()=>{
console.log("Failed to connect!");
});
},()=>{
console.log("Failed to initiate!");
});
}catch(e){
console.error(e.toString());
}
Below code is sample for how to use it. Please see methods and its functionalities below sections.
const bleConnection = require('@siva7170/ble-connection');
const bleConnInstance = new bleConnection.BLEConnection();
bluetooth_addr:
String
uuid:
String
timeout_for_reconnect:
Integer
no_of_attempt_to_reconnect:
Integer
It sets necessary things to BLE Connection before it initialize.
bleConnInstance.SetBtInfo("00:00:00:00:00:E0","aaaaaaaa-aaaa-4444-cccc-999888999888",5000,0);
Function
This method will give current state of BLE Connection when it is changed from one to another.
bleConnInstance.GetStatus((res)=>{
console.log("Status: "+res);
});
successCallback:
Function
failureCallback:
Function
It will make connection to device which is defined in SetBtInfo()
bleConnInstance.MakeConnection(()=>{
console.log("Connected...!");
},
()=>{
console.log("Failed to connect!");
});
successCallback:
Function
failureCallback:
Function
This method will be triggered when connection made to Bluetooth Server.
bleConnInstance.IsConnected(()=>{
console.log("Connection status: Connected!");
},
()=>{
console.log("Failed to connect!");
});
data:
String
successCallback:
Function
failureCallback:
Function
With this method, you can send data to client
bleConnInstance.SendDataToServer('Hi server!',(res)=>{
console.log("Data sent: "+sData);
}, (err)=>{
});
Function
This method will be triggered when the data sent from bluetooth server
bleConnInstance.OnReceiveDataFromServer((data)=>{
console.log("Data receivedd: "+data);
});
Please find full example of implementation
const bleConnection = require('@siva7170/ble-connection');
const bleConnInstance = new bleConnection.BLEConnection();
try{
bleConnInstance.SetBtInfo("00:00:00:00:00:E0","aaaaaaaa-aaaa-4444-cccc-999888999888",5000,0);
bleConnInstance.GetStatus((res)=>{
console.log("Status: "+res);
});
bleConnInstance.MakeConnection(()=>{
console.log("Connected...!");
},
()=>{
console.log("Failed to connect!");
});
bleConnInstance.IsConnected(()=>{
console.log("Connection status: Connected!");
// INITIATE
bleConnInstance.SendDataToServer("INITIATE",(res)=>{
console.log("Initiating...!");
}, (err)=>{
});
bleConnInstance.OnReceiveDataFromServer((data)=>{
bleConnInstance.SendDataToServer("READY_SCAN",(res)=>{
console.log("Ready scan...!");
}, (err)=>{
});
});
},
()=>{
console.log("Failed to connect!");
});
}catch(e){
console.error(e.toString());
}
I created this package for my own usage. I welcome contribution for this package improvement.