设备
免费插件
此插件免费开源,采用 MIT 许可证。
概述
Device API 提供对设备硬件功能和信息的访问,包括振动、闪光灯、设备信息和电池状态。
安装
shell
composer require nativephp/mobile-device使用
PHP (Livewire/Blade)
php
use Native\Mobile\Facades\Device;
// 设备振动
Device::vibrate();
// 切换闪光灯
$result = Device::toggleFlashlight();
// 返回: ['success' => true, 'state' => true|false]
// 获取设备 ID
$result = Device::getId();
// 返回: ['id' => 'unique-device-id']
// 获取设备信息
$result = Device::getInfo();
// 返回: ['info' => '{"name":"iPhone","model":"iPhone","platform":"ios",...}']
// 获取电池信息
$result = Device::getBatteryInfo();
// 返回: ['info' => '{"batteryLevel":0.85,"isCharging":false}']JavaScript (Vue/React/Inertia)
js
import { device } from '#nativephp';
// 设备振动
await device.vibrate();
// 切换闪光灯
const flashResult = await device.toggleFlashlight();
console.log('闪光灯状态:', flashResult.state);
// 获取设备 ID
const idResult = await device.getId();
console.log('设备 ID:', idResult.id);
// 获取设备信息
const infoResult = await device.getInfo();
const info = JSON.parse(infoResult.info);
console.log('平台:', info.platform);
// 获取电池信息
const batteryResult = await device.getBatteryInfo();
const battery = JSON.parse(batteryResult.info);
console.log('电池电量:', battery.batteryLevel * 100 + '%');方法
vibrate(): array
使设备振动。
返回: { success: true }
toggleFlashlight(): array
切换设备闪光灯开/关。
返回: { success: boolean, state: boolean }
getId(): array
获取唯一设备标识符。
返回: { id: string }
- iOS: 使用
identifierForVendorUUID - Android: 使用
ANDROID_ID
getInfo(): array
获取详细设备信息。
返回: { info: string } (JSON 字符串)
设备信息包括:
name- 设备名称model- 设备型号platform- "ios" 或 "android"operatingSystem- 操作系统名称osVersion- 操作系统版本字符串manufacturer- 设备制造商isVirtual- 是否在模拟器/仿真器中运行memUsed- 内存使用量(字节)webViewVersion- WebView 版本
getBatteryInfo(): array
获取电池电量和充电状态。
返回: { info: string } (JSON 字符串)
电池信息包括:
batteryLevel- 电池电量,范围 0.0 到 1.0isCharging- 设备是否正在充电
权限
Android
android.permission.VIBRATE- 用于振动android.permission.FLASHLIGHT- 用于闪光灯控制
iOS
无需特殊权限。