TypeError: undefined is not a function (near '...device.monitorCharacteristicForService...') - react-native

I'm trying to pull data from a glucose monitor. I've connected successfully and have the correct UUIDs for the relevant services and characteristics but I'm getting the error below:
TypeError: undefined is not a function (near '...device.monitorCharacteristicForService...')
Does anyone know why this might be? Code below shows the connecting function and the attempt to monitor the data. Both are being i nitiated by button presses.
Note I'm trying to run on android.
const connectDevice = device => {
manager.stopDeviceScan();
manager.connectToDevice(device.id).then(async device => {
await device.discoverAllServicesAndCharacteristics();
manager.stopDeviceScan();
setDisplayText(`Device connected\n with ${device.name}`);
setConnectedDevice(device);
setDevices([]);
device.services().then(async service => {
console.log(service)
})
}
);
}
const getData = async device => { await
device.monitorCharacteristicForService(
'00001808-0000-1000-8000-00805f9b34fb',
'00002A18-0000-1000-8000-00805f9b34fb',
(error, characteristic) =>{
if (error) {
console.error("Error at receiving data from device", error);
return;
} else {
console.log('Characteristic', characteristic)
}},
)};

Related

Expo Geofence exit event is always triggered upon startup

I am developing an app that would send data to my server based on the user entering and exiting a region using Expo Location. Currently testing on an Android emulator.
However, the exit event is triggered for all regions upon the app startup. Is this the expected behavior or am I doing something wrong?
Geolocation:
useEffect(() => {
(async () => {
await Location.startGeofencingAsync(GEO_LOC, region)
})();
return async() =>{
await Location.stopGeofencingAsync(GEO_LOC)
}
}, []);
TaskManager :
TaskManager.defineTask(GEO_LOC, ({ data: { eventType, region }, error }) => {
if (error) {
// check `error.message` for more details.
return;
}
if (eventType === Location.GeofencingEventType.Enter) {
console.log("You've entered region:", region.identifier);
socket.emit('court:increment', region.identifier)
}
if (eventType === Location.GeofencingEventType.Exit) {
console.log("You've left region:", region.identifier);
socket.emit('court:decrement', region.identifier)
}
});

Uncaught (in promise) : NotSupportedError: GATT Error Unknown

I am trying to write data which is 490 in length to a device using Web Bluetooth API. When I try to write I get NotSupportedError: GATT Error Unknown.
I am using chrome browser on android.
My app is in Angular 7 and I am using #types/web-bluetooth.
Below is the code:
navigator.bluetooth
.requestDevice({
filters: [{ name: this.deviceName }],
optionalServices: [this.GATT_SERVICE],
})
.then((device) => {
return device.gatt.connect();
})
.then((server) => {
return server.getPrimaryService(this.GATT_SERVICE);
})
.then((service) => {
this.gattService = service;
return this.gattService.getCharacteristic(this.GATT_CHAR);
})
.then((characteristic) => {
characteristic.writeValueWithResponse(this.arraybufferdata);
})
.catch(async (err) => {
console.log(err);
});
Can someone please help?
Is the issue related to the length? Can you reproduce error with less bytes in this.arraybufferdata?
Nit: You may want to return promise so that error gets propagated.
.then((characteristic) => {
return characteristic.writeValueWithResponse(this.arraybufferdata);
})

React-Native/Expo Location status if it user don't want to allow location after giving permission to app

After allowing permission to access GPS in react native App. If the user rejected to turn on the gps. It will show errors like
Unhandled promise rejection: Error: Location request failed due to unsatisfied device settings."
I want to avoid if the user rejects the Gps turn on option It will return something. so I need If condition for the location whether it is On or Off. (I'm using expo-location)
You're seeing this error because the Location.getCurrentPositionAsync is async method and it returns a promise and if it fails it throws an error (the error you're seeing).
You could wrap your code inside a try and catch block to catch the error and do something about it.
Example:
_getLocationAsync = async () => {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
alert('The request was denied');
}else{
try{
let location = await Location.getCurrentPositionAsync({});
// do something with location
}catch(e){
alert('We could not find your position. Please make sure your location service provider is on');
console.log('Error while trying to get location: ', e);
}
}
}
// call this._getLocationAsync();
you will need to check the status from expo-location and redirect user to settings to allow the permission for that you can use android intents for android and for ios you can use Linking to redirect the user to device settings and give permissions
requestLocationPermission = async () => {
const { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status === 'granted) {
navigation.navigate('screen_name');
} else {
// Alert Message if user does not allow permissions
Alert.alert("alert Message", "Instructions based on OS", [
{
text: 'Open Settings',
onPress: () => goToSettings(),
style: 'cancel',
},
{ text: Languages.DENY, onPress: () => navigation.goback()},
]);
}
};
go to settings
goToSettings = () => {
if (Platform.OS == 'ios') {
// Linking for iOS
Linking.openURL('app-settings:');
} else {
// IntentLauncher for Android
IntentLauncher.startActivityAsync(
IntentLauncher.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS
);
}
};
NOTE Intent launcher is a separate package for android
use this to turn gps on:
Location.enableNetworkProviderAsync()
It's a simple popup. (without redirection to a setting)

What is this error that I'm capturing here, why does it show line, columns, for React Native app?

The code inside api.login(), when there is a and error like a network error, I get the following at the top layer, is this the right way to capture that? I'm beginning react native development, so unsure if there is anything else I should be improving, any help is appreciated, thank you!!
loggedIn = await api.ping();
const state = store.getState();
if (!loggedIn) {
try {
await api.login(state.User.email, state.User.password);
} catch (error) {
console.log('GOT AN ERROR' + JSON.stringify(error));
}
}
If there is no network connection it returns the following...:
GOT AN ERROR{"line":139075,"column":38,"sourceURL":"http://127.0.0.1:19001/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&minify=false&hot=false"}
The line inside api.login is a catch(error) as well that returns the following:
const login = (email: string, password: string): Promise<Object> =>
axios
.post('/sign_in', { email, password })
.then(res => { ... })
.catch(error) {
return Promise.reject(new Error(error));
}
}
Am I doing this right? Why is that error just a hash with a line, column, etc?

How can I get the cause of a sql error from Expo SQLite API?

Having the following code:
import { SQLite } from 'expo';
const db = SQLite.openDatabase('mydb.db')
db.transaction( tx => {
tx.executeSql('insert into invalidTable values (?,?)', [1,2], null, (transact, err) => {
//I can't find error description in the objects below
console.log({transact, err})
})
})
How can I get the sqlite error message, to determine what exactly was the cause of this error (in this case, an invalid table)?
The API documentation says my error function "Takes two parameters: the transaction itself, and the error object", but I couldn't find the error description in none of these.
I did a snack simulating this scenario.
This logs the errors for me
import { SQLite } from 'expo';
const db = SQLite.openDatabase('mydb.db')
db.transaction(tx => {
tx.executeSql(sql, params, (_, { rows }) => {
console.log(rows._array)
}, (t, error) => {
console.log(error);
})
})