React hook that provides methods to control Bluetooth functionality and monitor its state.
Import
import { useBluetooth } from 'node-fullykiosk';
Usage
import { useBluetooth } from 'node-fullykiosk';
function BluetoothControl() {
const { enabled, enable, disable, openSettings } = useBluetooth();
return (
<div>
<h2>Bluetooth: {enabled ? 'Enabled' : 'Disabled'}</h2>
<button onClick={enable}>Enable</button>
<button onClick={disable}>Disable</button>
<button onClick={openSettings}>Settings</button>
</div>
);
}
Return Value
Indicates whether Bluetooth is currently enabled on the device. Updates automatically every second.
Function to enable Bluetooth on the device.
Function to disable Bluetooth on the device.
Function to open the system Bluetooth settings page.
Behavior
- Polls Bluetooth state every second for automatic updates
- Calling
enable() or disable() immediately updates the local state
- The
openSettings() function opens the Android Bluetooth settings screen
- Automatically cleans up the polling interval on unmount
Example: Bluetooth Toggle
import { useBluetooth } from 'node-fullykiosk';
function BluetoothToggle() {
const { enabled, enable, disable } = useBluetooth();
const handleToggle = () => {
if (enabled) {
disable();
} else {
enable();
}
};
return (
<button
onClick={handleToggle}
style={{
padding: '12px 24px',
backgroundColor: enabled ? '#2196F3' : '#757575',
color: 'white',
border: 'none',
borderRadius: '8px',
cursor: 'pointer',
fontSize: '16px'
}}
>
{enabled ? '📶 Bluetooth On' : '📵 Bluetooth Off'}
</button>
);
}
Example: Bluetooth Settings Panel
import { useBluetooth } from 'node-fullykiosk';
function BluetoothPanel() {
const { enabled, enable, disable, openSettings } = useBluetooth();
return (
<div style={{
padding: '20px',
backgroundColor: '#f5f5f5',
borderRadius: '12px'
}}>
<div style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
marginBottom: '16px'
}}>
<div>
<h3 style={{ margin: 0 }}>Bluetooth</h3>
<p style={{
margin: '4px 0 0 0',
color: enabled ? '#4CAF50' : '#757575'
}}>
{enabled ? 'Connected and discoverable' : 'Turned off'}
</p>
</div>
<div style={{
width: '48px',
height: '48px',
borderRadius: '50%',
backgroundColor: enabled ? '#2196F3' : '#ccc',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
fontSize: '24px'
}}>
📶
</div>
</div>
<div style={{ display: 'flex', gap: '8px' }}>
<button
onClick={enabled ? disable : enable}
style={{
flex: 1,
padding: '10px',
backgroundColor: enabled ? '#f44336' : '#4CAF50',
color: 'white',
border: 'none',
borderRadius: '6px',
cursor: 'pointer'
}}
>
{enabled ? 'Turn Off' : 'Turn On'}
</button>
<button
onClick={openSettings}
style={{
flex: 1,
padding: '10px',
backgroundColor: '#2196F3',
color: 'white',
border: 'none',
borderRadius: '6px',
cursor: 'pointer'
}}
>
Settings
</button>
</div>
</div>
);
}
Example: Automatic Bluetooth Management
import { useBluetooth } from 'node-fullykiosk';
import { useEffect, useState } from 'react';
function AutoBluetoothManager() {
const { enabled, enable, disable } = useBluetooth();
const [autoMode, setAutoMode] = useState(true);
useEffect(() => {
if (!autoMode) return;
// Example: Enable Bluetooth during business hours
const now = new Date();
const hour = now.getHours();
const isBusinessHours = hour >= 9 && hour < 17;
if (isBusinessHours && !enabled) {
console.log('Enabling Bluetooth for business hours');
enable();
} else if (!isBusinessHours && enabled) {
console.log('Disabling Bluetooth outside business hours');
disable();
}
}, [enabled, autoMode]);
return (
<div>
<h3>Automatic Bluetooth Management</h3>
<label>
<input
type="checkbox"
checked={autoMode}
onChange={(e) => setAutoMode(e.target.checked)}
/>
Enable automatic mode
</label>
<p>
Status: Bluetooth is {enabled ? 'enabled' : 'disabled'}
</p>
{autoMode && (
<p style={{ color: '#666', fontSize: '14px' }}>
Bluetooth will be enabled during business hours (9 AM - 5 PM)
</p>
)}
</div>
);
}
Notes
- Requires appropriate Android permissions for Bluetooth control
- The polling interval (1 second) ensures the state stays synchronized
- State changes may take a moment to reflect on the actual device