React Native Bluetooth Transfer for Android
npm install react-native-bluetooth-message``sh`
npm install react-native-bluetooth-message`
or sh`
yarn add react-native-bluetooth-message
And run this command to link this library
`sh`
react-native link react-native-bluetooth-messageUsage
Add following permissions in AndroidManifest.xml
`xml
`
Available functions are:
`js`
enable // Enables Bluetooth and returns btState Promise
discover // Discovers nearbby devices
discoverable // Makes the device discoverable return Promise
ConnListener // Listens to connection status returns string
DvcListener // Listens to discovered device list and returns Array of Objects
connect // connect(deviceAddress)
send // send(msgToSend)
MsgListener // Listens to incoming Messages (String)
disconnect // Closes all open connections
isConnected // forcefully return connection State as Promise
Example Code:
`js
import React, { useState } from 'react';
import {
StyleSheet,
View,
Text,
SafeAreaView,
Button,
TouchableOpacity,
ScrollView,
TextInput,
} from 'react-native';
import {
enable,
discover,
discoverable,
ConnListener,
DvcListener,
connect,
send,
MsgListener,
disconnect,
isConnected,
} from 'react-native-bluetooth-message';
function renderDeviceList() {
let dvcList: String[] = [];
return Object.entries(DvcListener()).map(([_, dvc]) => {
return Object.entries(dvc).map(([name, addr]) => {
if (dvcList.includes(name)) {
return <>>;
} else {
dvcList.push(name);
}
return (
connect(addr);
}}
style={{
backgroundColor: '#212121',
marginVertical: 8,
marginHorizontal: 10,
padding: 20,
borderRadius: 10,
}}
key={addr}
>
);
});
});
}
export default function App() {
const [msgTxt, setMsgTxt] = useState
return (
title="Enable"
onPress={async () => {
enable().then((btState) => {
console.info(btState);
});
}}
/>
title="discoverable"
onPress={async () => {
discoverable(15).then((discoverability) => {
console.info(discoverability);
});
}}
/>
title="discover"
onPress={async () => {
discover().catch((err) => {
console.log('ok', err);
});
}}
/>
{renderDeviceList()}
onChangeText={(newText: any) => setMsgTxt(newText)}
value={msgTxt}
/>
title="Send"
onPress={() => {
send(msgTxt);
}}
/>
title="Disconnect"
onPress={() => {
disconnect();
}}
/>
title="is Connected"
onPress={() => {
isConnected().then((isCon) => {
console.log(isCon);
});
}}
/>
);
}
const styles = StyleSheet.create({
heading: {
fontSize: 40,
},
alignCenter: {
alignItems: 'center',
},
});
``
See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT
---
Made with create-react-native-library