I'm developing android app in cordova where i want read data sent from bluetooth.
I'm testing on Android Lolipop (Sony XPERIA M2) and bluetooth barcode scanner.
For this purpose i tried to use plugins for cordova.
Tested:
https://www.npmjs.com/package/cordova-plugin-bluetoothle
https://www.npmjs.com/package/cordova-plugin-networking-bluetooth
https://github.com/tanelih/phonegap-bluetooth-plugin
https://github.com/don/BluetoothSerial
and many others.
Every time i was possible to find my device but none of these plugins allow me to connect to scanner.
I got error: READ FAILED, SOCKET MIGHT CLOSED OR TIMEOUT, ERROR CODE: 9
My mobile is connected and paired with scanner correctly but i cant figure out why i cant connect via my app.
EDIT:
onDeviceReady: function () {
var dev = {};
app.receivedEvent('deviceready');
window.bluetooth.enable(function () {
alert("SUCCESS");
}, function () {
alert("FAILED");
});
window.bluetooth.startDiscovery(onDeviceDiscovered, onDiscoveryFinished, onError);
function onDeviceDiscovered(device) {
alert("Found device " + device.address);
if (device.address === "40:83:DE:4B:D4:12") { // address of scanner
window.bluetooth.getUuids(onUuidsRetrieved, onError, device.address);
}
}
function onDiscoveryFinished() {
alert("SUCC FINISHED");
if (dev.address === "40:83:DE:4B:D4:12") {
alert("Trying to connect...");
window.bluetooth.connect(onConnected, onErrorConn, {
address: dev.address,
uuid: dev.uuids[0]
});
}
}
function onConnected() {
alert("Connected");
}
function onUuidsRetrieved(device) {
alert(device.address + " UUID: " + device.uuids);
if (device.address === "40:83:DE:4B:D4:12") {
dev = device;
}
}
function onError() {
alert("ERROR");
}
function onErrorConn(error) {
alert(error.code + " " + error.message);
}
}
Was someone facing to same issue ?
Thanks for all your help.
try using the cordova-plugin-networking-bluetooth for the central and for the central I have used cordova-plugin-ble-peripheral.
works great, able to send service and chariteristics changes, and do file transfer
Related
Getting the data from OBD II simulator to android application via Bluetooth successfully.
Now I am trying get the data from ELM327 device to android application.
I have an ELM327 device connected to my Car. Bluetooth connection happens by ECU connection fails with unable to connect error.
getting below error,
D:NetworkManagementSocketTagger: tagSocket(80) with statsTag=0xffffffff, statsUid=-1
E : Failed to run command. -> Error running 01 46, response: ...UNABLETOCONNECT
Could you please help me to resolve this issue.
i am trying to create an ionic obd-2 application for my university project. i am using "BluetoothSerial" in order to connect with obd simulator i have done all the settings required(that i know of) but i have hit a wall and i dont know what to do next. i connected to the bluetooth device and when is send the "ATZ" commands using bluetoothserial.write(obdcommand) i am getting "OK" can you please share with me how you connected with bluetoothserial or am i missing something...
I will post my code dow below.
obdcommands=['ATD','ATZ','ATE0','ATL0','ATS0','ATH0','ATAT2','ATSP0','0100', '0105\r', '010C'];
constructor(private bluetooth:BluetoothSerial,
private DataSrv:DataSrvService,
private action:ActionSheetController,
private permission:AndroidPermissions,
private alert: AlertController,
private toastctrl:ToastController) {
}
connect(dvc)
{
if(dvc.address=="")
this.showError("No Address");
else{
this.bluetooth.connect(dvc.address).subscribe(success=>
{
this.modal.dismiss(null, 'cancel');
this.presentToast("Connected Successfully");
this.blue=false;
this.deviceConnected();
this.bluetooth.available().then(tr=>{alert(tr)})
},error=>{
alert("Connect Error: "+error);
})
}
}
deviceConnected()
{
this.bluetooth.subscribe('\n').subscribe(success=>{
alert("Subscribed Successfully" +success);
})
}
async searchOBD()
{
for(let k=0;k<this.obdcommands.length;k++)
{
await this.bluetooth.write(this.obdcommands[k]).then( (success) => {
alert('Connected: '+this.obdcommands[k]+'Data: '+success );
},
(error) => {
alert('Reading failed: ' + error );
});
}
}
We create an app with React Native WebRTC and Janus Gateway. It works as wanted. Our app is based on push to talk. So when an users in listen mode need to break microphone for other apps. And take back when anyone press to speak button.
Breaking Mic:
if (!globalTrack) {
globalTracks = config.myStream.getTracks();
}
config.myStream.getTracks().forEach(t => {
config.myStream.removeTrack(t);
});
Get Back the Tracks:
globalTracks.forEach(t => {
config.myStream.addTrack(t);
});
pluginHandle.createOffer({
media: { addVideo: true },
success: function(jsep) {
Janus.debug(jsep);
pluginHandle.send({message: {audio: true, video: false}, "jsep": jsep });
},
error: function(error) {
console.log("WebRTC error... " + JSON.stringify(error));
}
});
// also I have try this:
devices = await mediaDevices.getUserMedia({audio: true, video: false})
// devices output https://pastebin.ubuntu.com/p/KQqBq2QRy3/
devices._tracks.forEach(t => {
config.myStream.addTrack(t);
});
pluginHandle.createOffer({
media: {audio: {deviceId: devices._tracks[0]['id']}, replaceAudio: true},
success: function(jsep) {
pluginHandle.send({message: {audio: true, video: false}, "jsep": jsep});
},
error: function(error) {
console.log(("WebRTC error... " + JSON.stringify(error));
}
});
Problem:
Problem: E.g: when a call came to the phone and answered it. The user's voice does not pass anymore even after restarting the app.
On Janus Gateway every things is normal. I think problem is about renegotiation.
Problem occurred only on Android phones. And works after force stopping the app.
Janus Gateway Log which when I speak at room:
There's a message for JANUS AudioBridge plugin
Setting muted property: true (room 20, user 2301490876606211)
Notifying participant 329012611897879 (kardan)
Sending event to transport...
>> 0 (Success)
We can not find a generic solution.
For now restarting app is works with react-native-restart
I'm using Cordova/Phonegap plugin Ble Central to connect to BLE devices using Android Phone.
As per the documentation on github code is :
onConnect = function(device) {
ble.isConnected(deviceId,
function() {
console.log("Connected");
},
function() {
console.error("Disconnected");
}
);
ble.readRSSI(deviceId, function(rssi) {
console.log('read RSSI',rssi,'with device', deviceId);
});
};
ble.connect(deviceId, onConnect, app.onError);
Even though ble.isConnected() fucntion returns true, RSSI value never returns.
What could be the issue here ?? Has anyone tried using cordova-plugin-ble-central to retrieve RSSI ?
Try retrieving rssi from Peripheral data.
onConnect = function(device) {
console.log('rssi' + device.rssi);
};
See if it gives a proper value.
When gps is enabled geolocationsuccess work perfectly. But when gps diabled it not calling geolocationerror in mobile device.Working on ionic framework using phonegap geolocation for checking is gps enable or not. codes are here
var locOptions = {
maximumAge : 10000,
timeout : 5000,
enableHighAccuracy : true
};
function onLocationSuccess(position) {
console.log(position);
// alert('success');
$rootScope.pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': $rootScope.pos}, function address(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
$rootScope.currentposition = results[1].formatted_address;
console.log($rootScope.currentposition);
if($scope.switchToState=='signedIn'){
$ionicLoading.hide();
$http.get($rootScope.url +'api/price/?key=movo1026868hk738hkl')
.success(function(response) {
$rootScope.truckfare=response.datasets;
});
console.log('reached here2');
$state.go('app.home');
}
else{
$ionicLoading.hide();
$state.go('start');
}
}
}
});
}
// onError Callback receives a PositionError object
//
function onLocationError(error) {
alert("Geolocation error: #" + e.code + "\n" + e.message);
}
navigator.geolocation.getCurrentPosition(onLocationSuccess, onLocationError, locOptions);
Geolocation API will not give you an error if the GPS is off because that is not an error state. It will just timeout if it is not able to get a lock in the time limit you specify.
Unfortunately, this is a real problem. If you turn off GPS and Geolocation API times out, you will not be able to make it work again unless you restart a whole application. So even if you turn GPS back online API will not work again unless an application is restarted.
There's a simple workaround here. Before you initialize Geolocation API check if GPS is online. You can do that using this PhoneGap/Cordova plugin:
https://github.com/mablack/cordova-diagnostic-plugin
Essentially, this plugin will solve your initial problem + problem I mentioned you before.
I'm currently trying to get the SoundCloud API working under PhoneGap/Cordova and Android. Here's the code that is working fine within the browser:
var track_url = 'http://soundcloud.com/mymusic/mymusic'
;
SC.get('/resolve', {
url : track_url
}, function(track) {
$("#stream").live("click", function() {
SC.stream("/tracks/" + track.id, function(sound) {
sound.play();
$("#stop").live("click", function() {
sound.stop();
});
});
});
});
On my android debug device however there is no sound. LogCat doesn't give any standard or cordova errors. Has anyone encountered this before?
Many Thanks in advance
Your code looks okay and I was able to test it successfully on Android. The only thing I can think of is that the resolve call is returning a 404 or similar. Have you tried checking for an error?
var track_url = 'http://soundcloud.com/your/track';
SC.initialize({
client_id: 'YOUR_CLIENT_ID'
});
SC.get('/resolve', { url: track_url }, function(track, error) {
if (error) {
alert("Error: " + error.message);
return;
}
// ... rest of your code
});
Try that and see if you get anything, if not, leave a comment.